In magento development how to access one ".phtml" file in one or more other ".phtml" files?
For example:
In my theme template folder there is one folder info. It contains info.phtml.This info.phtml display data from database.I want to use this info.phtml in my other .phtml files by using getChildHtml() how to do this?
Try this Code in your phtml file to call another phtml file
<?php
echo $this->getLayout()->createBlock('core/template')->setTemplate('test/test.phtml')->toHtml();
?>
To do that using getChildHtml() the other phtml file needs to be child block inside of the block you are working on.
That piece of xml came from app/design/frontend/yourtheme/layout/catalog.xml, inside that block declaration you will see more blocks that can be called via getChildHtml().
<block type="catalog/product_view" name="product.info" template="catalog/product/view.phtml">
<block type="catalog/product_list_related" name="product.related.products" as="related_products" template="catalog/product/view/related-products.phtml"/>
</block>
In this case you can call related-products.phtml like that getChildHtml('related_products'), inside of the parent block view.phtml.
Related
I am creating a magento 2 theme. I just want to know how can I add .phtml file in xml layout, static block, cms page or in another .phtml file. Thank You.
For improving documentation/answer
Custom file path
app/design/frontend/{Package}/{theme}/Magento_Theme/templates/html/test.phtml
calling in xml layout file
<block class="Magento\Framework\View\Element\Template" name="test_file" template="Magento_Theme::html/test.phtml"/>
Calling in blocks and cms pages
{{block class="Magento\Framework\View\Element\Template" name="test_file" template="Magento_Theme::html/test.phtml"}}
Calling in any phtml file
<?php include ($block->getTemplateFile('Magento_Theme::html/test.phtml')) ?>
OR, as before
<?php echo $this->getLayout()->createBlock("Magento\Framework\View\Element\Template")->setTemplate("Magento_Theme::html/test.phtml")->toHtml();?>
General convention is
<VendorName_ModuleName>::relative/path/to/phtml/in/templates/
Examples:
in xml layout
<block class="Magento\Framework\View\Element\Template" name="default_home_page" template="Magento_Cms::default/home.phtml"/>
in phtml
<?php include ($block->getTemplateFile('Magento_Catalog::product/list/toolbar/viewmode.phtml')) ?>
Your custom file path
app/code/{vendor_name}/{module_name}/view/frontend/templates/custom.phtml
calling in phtml file into cms block and pages:-
{{block class="Magento\Framework\View\Element\Template" template="Vendor_Module::custom.phtml"}}
OR
{{block class="Vendor\Module\Block\your_file_name" template="Vendor_Module::custom.phtml"}}
calling in xml layout file:-
<block class="Magento\Framework\View\Element\Template" template="Vendor_Module::custom.phtml">
calling in another phtml file:-
<?php echo $this->getLayout()->createBlock("Magento\Framework\View\Element\Template")->setTemplate("Vendor_Module::custom.phtml")->toHtml();?>
Call phtml template file from within another phtml template file:
<?php echo $this->getLayout()->createBlock("Magento\Framework\View\Element\Template")->setTemplate("Magento_Theme::test.phtml")->toHtml(); ?>
test.phtml will be located in app/design/frontend/Vendor/themename/Magento_Theme/templates
I am stuck a little with following problem:
I created a Fom in the adminhtml area using the pure php from Magento and standard template.
This all works as expected, but I cannot dynamically add and remove fields, I need Javascript and my own phtml file for that.
My Idea now was to simply include a childblock which is using a custom template at the end of the form.
So far I have added
$cblock =$this->getLayout()
->createBlock('netcon_konmod/adminhtml_caps_edit_mat')
->setTemplate('netcon_konmod_mat.phtml');
$this->_addContent($this->getLayout()
->createBlock('netcon_konmod/adminhtml_caps_edit')->setChild($cblock));
to my controller, created an empty Netcon_Konmod_Block_Adminhtml_Caps_Edit_Mat class which extends Mage_Adminhtml_Block_Widget
and created template file mat.phtml in .../template/netcon/konmod/
I also have my konmod.xml which updates my layout and which includes
<adminhtml_caps_edit>
<reference name="content">
<block type="netcon_konmod/adminhtml_caps_edit" name="netcon_konmod_caps_edit">
<block type="netcon_konmod/adminhtml_caps_edit_mat" name="netcon_konmod_caps_mat" template="netcon_konmod_mat.phtml" />
</block>
</reference>
</adminhtml_caps_edit>
I am rather new to Magento, and until now I only found ways to include a child block by calling getChildHtml from an already existing phtml file of the parent block.
However, since I create my form completely with the form and fieldset methods, I don't have my own phtml file in which I could include that call.
The way I have it set up right now, it is displaying the form normally, but does not include the childblock.
Any help would be appreciated, if it is at all possible to do this, I would like to avoid recoding my whole form as template.
Try
$childBlock = $this->getLayout()
->createBlock('konmod/adminhtml_caps_edit_mat')
->setTemplate('netcon_konmod_mat.phtml');
$this->_addContent($this->getLayout()
->createBlock('konmod/adminhtml_caps_edit')
//->setTemplate('netcon_konmod_mat.phtml')
->append($childBlock));
From the following line in 2columns-right.phtml
<div class="col-main">
<?php echo $this->getChildHtml('global_messages') ?>
<?php echo $this->getChildHtml('content') ?>
</div>
I am not able to understand where the content in <?php echo $this->getChildHtml('content') ?> is coming from.
Which .phtml file is called to display the data by <?php echo $this->getChildHtml('content') ?>?
If we're discussing the frontend of the website, the particular line you've asked about....
<?php echo $this->getChildHtml('content') ?>
is added to the Magento layout XML in app/design/frontend/base/default/layout/page.xml. In Magento version 1.8, you'll find it defined in lines 92-94.
<block type="core/text_list" name="content" as="content" translate="label">
<label>Main Content Area</label>
</block>
By looking at the "type" attribute of this block tag, we can know what object class this section of the layout is. It comes from the "Core" module, and is of the block type Text List. The class name for this Mage_Core_Block_Text_List. (app/code/core/Mage/Core/Block/Text/List.php). Text Lists are simply block containers which purpose is to store additional child blocks inside them. You can add any number of child blocks to the text list and they will be rendered out either in the order they were added or the order they've been assigned.
So, to answer your question, there is no view script (.phtml file) that renders the contents of $this->getChildHtml('content'). The blocks which have been added to this block, may themselves have view scripts associated with them. To find out what view scripts those are, you'd have to find the layout XML which has added the block.
For example, if I had the following layout file added to the frontend of my website's theme:
<?xml version="1.0"?>
<layout>
<default>
<reference name="content">
<block type="core/template" name="my_view_script" template="hello/world.phtml" />
</reference>
</default>
</layout>
The code above, would add the block with an object class of Mage_Core_Block_Template to the block with the name 'content' (which happens to be the one you asked about). Magento will then look for the view script in the following locations, in this order:
app/design/frontend/PACKAGE_NAME/THEME_NAME/template/hello/world.phtml
app/design/frontend/PACKAGE_NAME/default/template/hello/world.phtml
app/design/frontend/base/default/template/hello/world.phtml
First one that is found, is the one it will use. If no view script is found Magento will log an error in var/logs/system.log (default log file setting) stating that the view script was not found. No output from the block will occur.
Note that depending on your settings in System -> Configuration -> (General) Design, there may be additional package/theme locations Magento will look in. There are also other scenarios such as if the "Custom Theme" is field is changed for individual CMS Pages, Catalog Categories, or Catalog Products, these individual model's view page may have an additional view script location (that will match the selected theme) that takes precedence over your site's default settings.
Magento will follow this same fallback logic when looking for translation files as well as layout XML files.
Please note, that it is perfectly acceptable to copy individual view scripts (avoid copying entire directories, copy over only view scripts you actually intend to modify) from app/design/frontend/base/default/template/ to your local theme, and customize them for the purposes of your website's theme. However, in order to have an upgrade compatible site, layout files should not be copied from base to your local theme. Doing so, does not follow upgrade compatible practices. Instead, XML Layout updates for your theme should be contained in app/design/frontend/PACKAGE_NAME/THEME_NAME/layout/local.xml. There is no layout instructions from app/design/frontend/base/default/layout/*, that cannot be removed/added-to/changed, what-have-you, with the proper XML instructions in local.xml.
I'm using Magento.
I want display and call one phtml file as a link in another phtml fileā¦
I have the new.phtml file on the home page. On that I put one link CHECK ALL which display all new products as category page.. It looks like category page. For that I create another phtml file named newproductpage.phtml which has same code of new.phtml. Now I try to call this newproductpage.phtml file #homepage as CHECK ALL link for that I write this code....
CHECK ALL
But its not working....
thnx..
you call newproductpage.phtml in any phtml file using below code
<?php echo $this->getLayout()->createBlock('core/template')->setTemplate('custom/newproductpage.phtml')->toHtml(); ?>
Use the below code for rendering your phtml file in another phtml file.
<?php echo $this->getLayout()->createBlock('core/template')->setTemplate('templatefolderpath/filename.phtml')->toHtml(); ?>
For more refinement of your code, you can use the block name and block file name instead of core/template because core/template use the core resources.
You can not call directly one phtml file to another phtml file.
But there are two way to call your phtml file either create one controller and create one action and call action from your anchor tag or create one cms page which call your phtml file.
if you create one module, so in your layout file something you can write
<modulename_controllername_controlleraction>
<reference name="content">
<block type="catalog/product_new" template="custom/newproductpage.phtml" />
</reference>
</module_controllername_controlleraction>
Or you can directly put this code in your cms page content area
{{block type="catalog/product_new" template="custom/newproductpage.phtml"}}
and in anchor tag give cms page link.
<?php echo $this->getLayout()->createBlock('core/template')->setTemplate('catalog/product/new.phtml')->toHtml(); ?>
you can also check the link.
Display .phtml page in another .phtml page
I believe you and I want to do precisely the same thing. I'm creating a modal from bootstrap and I need to call in a partial using the href attr.
So far, I think this might be possible by creating a page in the CMS and then using something like this:
<li>Home</li>
But honestly, I'm just starting out with Magento and know very little.
you can use iframe for same and load this another page content using AJAX call
I am trying to change Top links structure. I don't want to mess with xml files in default theme. I have only one xml file with everything inside. I don't want to create multiple xml files in my theme layout folder. I am trying to unset action inside top.links block.
This is the way I am trying to do:
<reference name="top.links">
<action method="unsetChild"><child>addLink</child></action>
<!-- <remove name="checkout_cart_link" /> -->
</reference>
When I put remove tag it works perfectly. But when I try to do unsetChild for addLink action it doesn't work. Do you know maybe how I should figure this out. I don't want separate xml files with the same names as in default theme.
The unsetChild function is from the Abstract block model and is used for removing a block that is a child of the current block. The block with the name top.links doesn't have a child block that is called addLink, hence the fact your configuration doesn't do anything. The action nodes with the method addLink call the addLink function on the Mage_Page_Block_Template_Links block. If the objective is to remove one of the links, you will need to use the action removeLinkByUrl.
<reference name="top.links">
<action method="removeLinkByUrl"><url>link/here</url></action>
</reference>
The exact arguments will obviously depend on what link you are attempting to remove.