how to call phtml inside phtml





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ margin-bottom:0;
}







1















I have a custom phtml inside I called attribute.phtml but it's not working:



echo $block->getTemplateFile('Magento_Catalog::product/view/attribute.phtml');
echo $this->getLayout()
->createBlock("MagentoFrameworkViewElementTemplate")
->setTemplate("Magento_Catalog::product/view/attribute.phtml")
->toHtml();


Both showing error only.










share|improve this question
















bumped to the homepage by Community 13 mins ago


This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.






















    1















    I have a custom phtml inside I called attribute.phtml but it's not working:



    echo $block->getTemplateFile('Magento_Catalog::product/view/attribute.phtml');
    echo $this->getLayout()
    ->createBlock("MagentoFrameworkViewElementTemplate")
    ->setTemplate("Magento_Catalog::product/view/attribute.phtml")
    ->toHtml();


    Both showing error only.










    share|improve this question
















    bumped to the homepage by Community 13 mins ago


    This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.


















      1












      1








      1


      1






      I have a custom phtml inside I called attribute.phtml but it's not working:



      echo $block->getTemplateFile('Magento_Catalog::product/view/attribute.phtml');
      echo $this->getLayout()
      ->createBlock("MagentoFrameworkViewElementTemplate")
      ->setTemplate("Magento_Catalog::product/view/attribute.phtml")
      ->toHtml();


      Both showing error only.










      share|improve this question
















      I have a custom phtml inside I called attribute.phtml but it's not working:



      echo $block->getTemplateFile('Magento_Catalog::product/view/attribute.phtml');
      echo $this->getLayout()
      ->createBlock("MagentoFrameworkViewElementTemplate")
      ->setTemplate("Magento_Catalog::product/view/attribute.phtml")
      ->toHtml();


      Both showing error only.







      magento2 layout blocks template






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited May 19 '17 at 11:33









      Siarhey Uchukhlebau

      10.1k93058




      10.1k93058










      asked May 19 '17 at 11:05









      Magento 2Magento 2

      2,41172877




      2,41172877





      bumped to the homepage by Community 13 mins ago


      This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.







      bumped to the homepage by Community 13 mins ago


      This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.
























          2 Answers
          2






          active

          oldest

          votes


















          0














          Pass Block file as, MagentoCatalogBlockProductViewDescription
          Just keep below line in your phtml file,



          <?php echo $this->getLayout()->createBlock("MagentoCatalogBlockProductViewDescription")->setTemplate("Magento_Catalog::product/view/attribute.phtml")->toHtml();?>





          share|improve this answer
























          • please let me know if you have any issue

            – Rakesh Jesadiya
            May 19 '17 at 12:45



















          0














          If you want use the Magento_Catalog::product/view/attribute.phtml you should add important data to the block before layout render it (from the template):



          $_product = $block->getProduct();
          $_call = $block->getAtCall();
          $_code = $block->getAtCode();
          $_className = $block->getCssClass();
          $_attributeLabel = $block->getAtLabel();
          $_attributeType = $block->getAtType();
          $_attributeAddAttribute = $block->getAddAttribute();


          So according to your needs you can pass block arguments to the createBlock method using third argument:



          /**
          * Block Factory
          *
          * @param string $type
          * @param string $name
          * @param array $arguments
          * @return ElementBlockInterface
          */
          public function createBlock($type, $name = '', array $arguments = );


          Your code should looks like this (I use the SKU-attribute as an example):



          $block->getLayout()
          ->createBlock(
          'MagentoFrameworkViewElementTemplate',
          '',
          [
          "at_call" => 'getSku',
          "at_code" => 'sku',
          "css_class" => 'sku',
          "at_label" => 'default',
          "add_attribute" => 'itemprop="sku"',
          "product" => $block->getProduct() // Here should be real current product, you should load it in your main block!
          ])
          ->setTemplate("Magento_Catalog::product/view/attribute.phtml")
          ->toHtml();


          PS: Try to change $this to the $block in your code:



          <?php echo ($block->getTemplateFile('Magento_Catalog::product/view/attribute.phtml')) ;?>
          <?php echo $block->getLayout()->createBlock("MagentoFrameworkViewElementTemplate")->setTemplate("Magento_Catalog::product/view/attribute.phtml")->toHtml();?>


          In Magento 2 you should use the $block variable inside a template, not $this. Here you can find additional info.






          share|improve this answer


























          • After addin this it break the site

            – Magento 2
            May 19 '17 at 11:10











          • @Kothari see updated answer. Please, add error from the log to your question.

            – Siarhey Uchukhlebau
            May 19 '17 at 11:14











          • @Kothari still have same issues?

            – Siarhey Uchukhlebau
            May 30 '17 at 8:55












          Your Answer








          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "479"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: false,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f175255%2fhow-to-call-phtml-inside-phtml%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          2 Answers
          2






          active

          oldest

          votes








          2 Answers
          2






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          0














          Pass Block file as, MagentoCatalogBlockProductViewDescription
          Just keep below line in your phtml file,



          <?php echo $this->getLayout()->createBlock("MagentoCatalogBlockProductViewDescription")->setTemplate("Magento_Catalog::product/view/attribute.phtml")->toHtml();?>





          share|improve this answer
























          • please let me know if you have any issue

            – Rakesh Jesadiya
            May 19 '17 at 12:45
















          0














          Pass Block file as, MagentoCatalogBlockProductViewDescription
          Just keep below line in your phtml file,



          <?php echo $this->getLayout()->createBlock("MagentoCatalogBlockProductViewDescription")->setTemplate("Magento_Catalog::product/view/attribute.phtml")->toHtml();?>





          share|improve this answer
























          • please let me know if you have any issue

            – Rakesh Jesadiya
            May 19 '17 at 12:45














          0












          0








          0







          Pass Block file as, MagentoCatalogBlockProductViewDescription
          Just keep below line in your phtml file,



          <?php echo $this->getLayout()->createBlock("MagentoCatalogBlockProductViewDescription")->setTemplate("Magento_Catalog::product/view/attribute.phtml")->toHtml();?>





          share|improve this answer













          Pass Block file as, MagentoCatalogBlockProductViewDescription
          Just keep below line in your phtml file,



          <?php echo $this->getLayout()->createBlock("MagentoCatalogBlockProductViewDescription")->setTemplate("Magento_Catalog::product/view/attribute.phtml")->toHtml();?>






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered May 19 '17 at 11:11









          Rakesh JesadiyaRakesh Jesadiya

          30.4k1577125




          30.4k1577125













          • please let me know if you have any issue

            – Rakesh Jesadiya
            May 19 '17 at 12:45



















          • please let me know if you have any issue

            – Rakesh Jesadiya
            May 19 '17 at 12:45

















          please let me know if you have any issue

          – Rakesh Jesadiya
          May 19 '17 at 12:45





          please let me know if you have any issue

          – Rakesh Jesadiya
          May 19 '17 at 12:45













          0














          If you want use the Magento_Catalog::product/view/attribute.phtml you should add important data to the block before layout render it (from the template):



          $_product = $block->getProduct();
          $_call = $block->getAtCall();
          $_code = $block->getAtCode();
          $_className = $block->getCssClass();
          $_attributeLabel = $block->getAtLabel();
          $_attributeType = $block->getAtType();
          $_attributeAddAttribute = $block->getAddAttribute();


          So according to your needs you can pass block arguments to the createBlock method using third argument:



          /**
          * Block Factory
          *
          * @param string $type
          * @param string $name
          * @param array $arguments
          * @return ElementBlockInterface
          */
          public function createBlock($type, $name = '', array $arguments = );


          Your code should looks like this (I use the SKU-attribute as an example):



          $block->getLayout()
          ->createBlock(
          'MagentoFrameworkViewElementTemplate',
          '',
          [
          "at_call" => 'getSku',
          "at_code" => 'sku',
          "css_class" => 'sku',
          "at_label" => 'default',
          "add_attribute" => 'itemprop="sku"',
          "product" => $block->getProduct() // Here should be real current product, you should load it in your main block!
          ])
          ->setTemplate("Magento_Catalog::product/view/attribute.phtml")
          ->toHtml();


          PS: Try to change $this to the $block in your code:



          <?php echo ($block->getTemplateFile('Magento_Catalog::product/view/attribute.phtml')) ;?>
          <?php echo $block->getLayout()->createBlock("MagentoFrameworkViewElementTemplate")->setTemplate("Magento_Catalog::product/view/attribute.phtml")->toHtml();?>


          In Magento 2 you should use the $block variable inside a template, not $this. Here you can find additional info.






          share|improve this answer


























          • After addin this it break the site

            – Magento 2
            May 19 '17 at 11:10











          • @Kothari see updated answer. Please, add error from the log to your question.

            – Siarhey Uchukhlebau
            May 19 '17 at 11:14











          • @Kothari still have same issues?

            – Siarhey Uchukhlebau
            May 30 '17 at 8:55
















          0














          If you want use the Magento_Catalog::product/view/attribute.phtml you should add important data to the block before layout render it (from the template):



          $_product = $block->getProduct();
          $_call = $block->getAtCall();
          $_code = $block->getAtCode();
          $_className = $block->getCssClass();
          $_attributeLabel = $block->getAtLabel();
          $_attributeType = $block->getAtType();
          $_attributeAddAttribute = $block->getAddAttribute();


          So according to your needs you can pass block arguments to the createBlock method using third argument:



          /**
          * Block Factory
          *
          * @param string $type
          * @param string $name
          * @param array $arguments
          * @return ElementBlockInterface
          */
          public function createBlock($type, $name = '', array $arguments = );


          Your code should looks like this (I use the SKU-attribute as an example):



          $block->getLayout()
          ->createBlock(
          'MagentoFrameworkViewElementTemplate',
          '',
          [
          "at_call" => 'getSku',
          "at_code" => 'sku',
          "css_class" => 'sku',
          "at_label" => 'default',
          "add_attribute" => 'itemprop="sku"',
          "product" => $block->getProduct() // Here should be real current product, you should load it in your main block!
          ])
          ->setTemplate("Magento_Catalog::product/view/attribute.phtml")
          ->toHtml();


          PS: Try to change $this to the $block in your code:



          <?php echo ($block->getTemplateFile('Magento_Catalog::product/view/attribute.phtml')) ;?>
          <?php echo $block->getLayout()->createBlock("MagentoFrameworkViewElementTemplate")->setTemplate("Magento_Catalog::product/view/attribute.phtml")->toHtml();?>


          In Magento 2 you should use the $block variable inside a template, not $this. Here you can find additional info.






          share|improve this answer


























          • After addin this it break the site

            – Magento 2
            May 19 '17 at 11:10











          • @Kothari see updated answer. Please, add error from the log to your question.

            – Siarhey Uchukhlebau
            May 19 '17 at 11:14











          • @Kothari still have same issues?

            – Siarhey Uchukhlebau
            May 30 '17 at 8:55














          0












          0








          0







          If you want use the Magento_Catalog::product/view/attribute.phtml you should add important data to the block before layout render it (from the template):



          $_product = $block->getProduct();
          $_call = $block->getAtCall();
          $_code = $block->getAtCode();
          $_className = $block->getCssClass();
          $_attributeLabel = $block->getAtLabel();
          $_attributeType = $block->getAtType();
          $_attributeAddAttribute = $block->getAddAttribute();


          So according to your needs you can pass block arguments to the createBlock method using third argument:



          /**
          * Block Factory
          *
          * @param string $type
          * @param string $name
          * @param array $arguments
          * @return ElementBlockInterface
          */
          public function createBlock($type, $name = '', array $arguments = );


          Your code should looks like this (I use the SKU-attribute as an example):



          $block->getLayout()
          ->createBlock(
          'MagentoFrameworkViewElementTemplate',
          '',
          [
          "at_call" => 'getSku',
          "at_code" => 'sku',
          "css_class" => 'sku',
          "at_label" => 'default',
          "add_attribute" => 'itemprop="sku"',
          "product" => $block->getProduct() // Here should be real current product, you should load it in your main block!
          ])
          ->setTemplate("Magento_Catalog::product/view/attribute.phtml")
          ->toHtml();


          PS: Try to change $this to the $block in your code:



          <?php echo ($block->getTemplateFile('Magento_Catalog::product/view/attribute.phtml')) ;?>
          <?php echo $block->getLayout()->createBlock("MagentoFrameworkViewElementTemplate")->setTemplate("Magento_Catalog::product/view/attribute.phtml")->toHtml();?>


          In Magento 2 you should use the $block variable inside a template, not $this. Here you can find additional info.






          share|improve this answer















          If you want use the Magento_Catalog::product/view/attribute.phtml you should add important data to the block before layout render it (from the template):



          $_product = $block->getProduct();
          $_call = $block->getAtCall();
          $_code = $block->getAtCode();
          $_className = $block->getCssClass();
          $_attributeLabel = $block->getAtLabel();
          $_attributeType = $block->getAtType();
          $_attributeAddAttribute = $block->getAddAttribute();


          So according to your needs you can pass block arguments to the createBlock method using third argument:



          /**
          * Block Factory
          *
          * @param string $type
          * @param string $name
          * @param array $arguments
          * @return ElementBlockInterface
          */
          public function createBlock($type, $name = '', array $arguments = );


          Your code should looks like this (I use the SKU-attribute as an example):



          $block->getLayout()
          ->createBlock(
          'MagentoFrameworkViewElementTemplate',
          '',
          [
          "at_call" => 'getSku',
          "at_code" => 'sku',
          "css_class" => 'sku',
          "at_label" => 'default',
          "add_attribute" => 'itemprop="sku"',
          "product" => $block->getProduct() // Here should be real current product, you should load it in your main block!
          ])
          ->setTemplate("Magento_Catalog::product/view/attribute.phtml")
          ->toHtml();


          PS: Try to change $this to the $block in your code:



          <?php echo ($block->getTemplateFile('Magento_Catalog::product/view/attribute.phtml')) ;?>
          <?php echo $block->getLayout()->createBlock("MagentoFrameworkViewElementTemplate")->setTemplate("Magento_Catalog::product/view/attribute.phtml")->toHtml();?>


          In Magento 2 you should use the $block variable inside a template, not $this. Here you can find additional info.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited May 19 '17 at 11:26

























          answered May 19 '17 at 11:08









          Siarhey UchukhlebauSiarhey Uchukhlebau

          10.1k93058




          10.1k93058













          • After addin this it break the site

            – Magento 2
            May 19 '17 at 11:10











          • @Kothari see updated answer. Please, add error from the log to your question.

            – Siarhey Uchukhlebau
            May 19 '17 at 11:14











          • @Kothari still have same issues?

            – Siarhey Uchukhlebau
            May 30 '17 at 8:55



















          • After addin this it break the site

            – Magento 2
            May 19 '17 at 11:10











          • @Kothari see updated answer. Please, add error from the log to your question.

            – Siarhey Uchukhlebau
            May 19 '17 at 11:14











          • @Kothari still have same issues?

            – Siarhey Uchukhlebau
            May 30 '17 at 8:55

















          After addin this it break the site

          – Magento 2
          May 19 '17 at 11:10





          After addin this it break the site

          – Magento 2
          May 19 '17 at 11:10













          @Kothari see updated answer. Please, add error from the log to your question.

          – Siarhey Uchukhlebau
          May 19 '17 at 11:14





          @Kothari see updated answer. Please, add error from the log to your question.

          – Siarhey Uchukhlebau
          May 19 '17 at 11:14













          @Kothari still have same issues?

          – Siarhey Uchukhlebau
          May 30 '17 at 8:55





          @Kothari still have same issues?

          – Siarhey Uchukhlebau
          May 30 '17 at 8:55


















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Magento Stack Exchange!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f175255%2fhow-to-call-phtml-inside-phtml%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          What other Star Trek series did the main TNG cast show up in?

          Berlina muro

          Berlina aerponto