3

In a module I have installed I am trying to create a phtml page but can't work out how to pull through information(Like an image) to my new custom .phtml

File containing information:

view.phtml

<?php
            /**
             * Blog post view template
             *
             * @var $block \Magefan\Blog\Block\Post\View
             */
            ?>
            <?php
                $_post = $this->getPost();
                $_postUrl = $_post->getPostUrl();
                $_postName = $block->escapeHtml($_post->getTitle(), null, true);
            ?>
            <div class="post-view">
                <div class="post-holder post-holder-<?php echo $_post->getId() ?>">
                    <div class="post-header clearfix">
                        <div class="addthis_toolbox addthis_default_style" addthis:url="<?php echo $_postUrl ?>">
                            <a class="addthis_button_facebook"></a>
                            <a class="addthis_button_twitter"></a>
                            <a class="addthis_button_email"></a>
                            <a class="addthis_button_compact"></a>
                        </div>
                        <?php echo $this->getInfoHtml() ?>
                    </div>

                    <div class="post-content">
                        <div class="post-description clearfix">
                            <?php if ($featuredImage = $_post->getFeaturedImage()) { ?>
                            <div class="post-ftimg-hld">
                                <img src="<?php echo $featuredImage ?>" alt="<?php echo $_postName ?>" />
                            </div>
                            <?php } ?>
                            <div class="post-text-hld">
                                <?php echo $block->getContent() ?>
                                <div class="clear"></div>
                            </div>
                        </div>
                    </div>

                    <div class="post-bottom">
                        <?php echo $this->getChildHtml('blog.post.bottom') ?>
                    </div>
                </div>
            </div>

My custom .phtml

<?php
/**
* Blog post view template
*
* @var $block \Magefan\Blog\Block\Post\View
*/
?>
<?php
$_post = $this->getPost();
$_postUrl = $_post->getPostUrl();
?>

<h2>Hello world</h2>
<p><?php echo $_post->getFeaturedImage() ?></p>

Obviously this doesn't work so how can I get it to work? Both files are in the same location

Edit XML added

<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<update handle="blog_default"/>
<update handle="blog_post_list"/>
<body>
    <referenceContainer name="content">
        <container name="landing.page">
            <block class="Magefan\Blog\Block\Post\Info" name="landing.content" as="landingcontent" template="Magefan_Blog::post/landing.phtml" />
        </container>
        <block class="Magefan\Blog\Block\Index" name="blog.posts.list" template="post/list.phtml" />
    </referenceContainer>
</body>
</page>
  • if your block file is same and $_post->getFeaturedImage() works for view.phtml then it should also work for your custom.phtml. Can you please share your xml file where you have assigned the block to the templates – Ekta Puri Sep 11 '16 at 23:09
  • plz share your xml code file to more debug your functionality. – Rakesh Jesadiya Sep 12 '16 at 8:57
  • plz let me know if any issue using below xml file – Rakesh Jesadiya Sep 13 '16 at 6:27
  • You should try my answer below. Not the best, but can use. – Khoa TruongDinh Sep 13 '16 at 12:48
  • you have not assigned custom.phtml in your xml to any block, So how can your phtml load functions from your block file? – Ekta Puri Sep 14 '16 at 23:55
4
+50

One more way is, if you wants to call that phtml file in same page you can do it as below way.

Set image in one variable and pass through calling phtml file and then you can access it in your custom phtml file.

Step1. In view.phtml

$data = array('featuredimage'=> $imagearray);

<?php echo $this->getLayout()->createBlock("your_custom_phtml_block_type_goeshere")->setData($data)->setTemplate("Vebdorname_themename::path/custom.phtml")->toHtml(); ?>

Step2: Custom.phtml

$featured_images = $this->getData('featuredimage');

Possibly according to your file path and block name you will need to change coding in above lines. But this will solve your purpose if you wants to call one phtml file from another passing variable.

1

You have to write your code inside below xml file, app/code/Magefan/Blog/view/frontend/layout/blog_post_view.xml

<?xml version="1.0"?>

<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <update handle="blog_default"/>
    <update handle="blog_post_opengraph" />
    <update handle="blog_post_richsnippets" />
    <body>
        <referenceContainer name="content">                
            <block class="Magefan\Blog\Block\Post\View" name="blog.post.customs" template="post/custom.phtml">
                <block class="Magefan\Blog\Block\Post\Info" name="blog.post.info.customs" template="post/info.phtml" />
                <action method="setPostInfoBlockName">
                    <argument name="name" xsi:type="string">blog.post.info.customs</argument>
                </action>
                <block class="\Magento\Framework\View\Element\Text\ListText" name="blog.post.bottom.customs" template="sidebar.phtml">
                    <block class="Magefan\Blog\Block\Post\View\RelatedPosts" name="blog.post.relatedposts.customs" as="relatedpostscustom"  template="Magefan_Blog::post/view/relatedposts.phtml" />
                    <block class="Magefan\Blog\Block\Post\View\RelatedProducts" name="blog.post.relatedproducts.customs" as="relatedproductscustom"  template="Magefan_Blog::post/view/relatedproducts.phtml" />
                    <block class="Magefan\Blog\Block\Post\View\Comments" name="blog.post.comments.customs" template="post/view/comments.phtml" />
                    <block class="Magefan\Blog\Block\Social\AddThis" name="addthis.js.init.customs" as="addthisJsCustom" template="Magefan_Blog::addthis-js.phtml"/>
                </block>
            </block>
        </referenceContainer>
    </body>
</page>

After Changes in above xml file, now your new custom.phtml file is called instead of view.phtml file.

please let me know if you have any issue.

1

You cannot access local variables from another template, because they are local.

But this should not be needed and if I understand your example correctly, you want access to getPost() in your new template.

Luckily you have access to any block that has been defined in layout XML if you know its name.

So find out the block name of \Magefan\Blog\Block\Post\View from the layout XML of the blog module. Let's say it is "post_view", then you can retrieve the post with this code in your custom template:

$_post = $block->getLayout()->getBlock("post_view")->getPost();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.