Understanding getChildHtml in Magento - php

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.

Related

Trying to add a child block to adminhtml form

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));

How do I access methods like $this->getChildHtml() in other directories/pages in Magento?

All I want to do is move a search box. This search box is currently displayed in the header, directly next to the logo, and is generated by the following code:
<?php echo $this->getChildHtml('topSearch') ?>
in /app/design/frontend/MYTHEME/default/template/page/html/header.phtml
I would like this search box inline with the navigation links, which are located in top.phtml which is located in another directory. But when I use the code
<?php echo $this->getChildHtml('topSearch') ?>
the search box does not appear. I understand that this is because the meaning of $this has changed, but what I don't understand is how to display the search box? What do I replace $this with?
I tried replacing it with Mage_Page_Block_Html_Header since that was the definition of $this in header.phtml, but to no avail. Can anybody point me in the right direction, or provide an explanation as to how I access methods once the definition of $this has changed?
You need to make layout update and include block topSearch into block containing top.phtml.
Look into app/design/frontend/.../layouts/...xml files, find how topSearch is declared and then find where the block using top.phtml template is declared. Then move topSearch block as a child of top block. I mean add layout xml update like this:
<default>
<reference name="catalog.topnav">
<block type="core/template" name="top.search" as="topSearch" template="catalogsearch/form.mini.phtml"/>
</reference>
</default>
Another solution is try next in your template:
echo $this->getLayout()->getBlock('top.search')->toHtml()
If it would not work then find in layouts topSearch block and try to use in code above name of block instead of alias.
You can read more about Magento view level here: http://www.magentocommerce.com/wiki/4_-_themes_and_template_customization/0_-_theming_in_magento/designing-for-magento
Good luck!

Magento searchbox - How to add the Searchbox on the left side of a 3columns.phtml page?

Is it possible to add the search box within the 3columns.phtml (like my categories.)
So for example i’d like to have on my products page on the left side:
So in the 3columns.phtml I have this line , the left div.
<div class="col-left sidebar"><?php echo $this->getChildHtml('left') ?></div>
So *what kind of code and where should I include In order that the search box appears on the left ?*
Any suggestion?
This is a good use for the file local.xml in your design layout folder. If you haven't created one yet, it goes:
/app/design/frontend/your_package/your_theme/layout/local.xml
local.xml
<?xml version="1.0"?>
<layout version="0.1.0">
<default>
<reference name="left">
<block type="core/template" name="searchbox" template="path/to/searchbox.phtml" after="sibling.blockName"/>
</reference>
</default>
</layout>
Refresh your cache and that should work on all pages set to have the left column.
Explanation
This is one of the few methods that Magento has built in to load layout blocks. The first couple of tags are required on all layout files, <default> marks the update handle to apply to all pages. <reference name="left"> means we are altering the left bar, "left" being its block name. Now lets look at the block below:
type="core/template" - This type signifies which classes to use, as tied to app/code/core/Mage/* typically, but can be overwritten by copying the class files over to the app/code/local/Mage/* directory, as well as signifying classes of extensions like app/code/community/another_extension.
name="searchbox" - This is the name it will be referenced by in the layout. You can give it something else, but make sure it doesn't conflict with existing block names.
template="path/to/searchbox" - This is where you enter the path to the template file that will be loaded.
after="sibling.blockName" - This marks where to place it in the layout order. This only really needs to be set when the parent block (in this case, the referenced "left" block) is basically set to <?php echo $this->getChildHtml(''); ?>. No arguments in the ('') means it will load all child blocks in order as the xml defines.
In the case of <?php echo $this->getChildHtml('left') ?>, it's calling up the block that has the name="left". After giving it a name, you can alternatively give it another alias by using as="search.box.alias" within the <block /> tag, search.box.alias also being anything you want, provided it doesn't cause any conflicts.

Magento Contact form in phtml file of custom created widget

I'm developing a widget for Magento, in this widget i need to load the magento contact form. I've tried several options, but none of them seem to work.
the widget is located in
app/
code/
local/
CompanyName/
WidgetName/
and the phtml files are located in
app/
design/
frontend/
default/
default/
template/
templatename/
templatefile1.pthml
templatefile2.pthml
templatefile3.phtml
to load the magento contact form i've added this in templatefile3.phtml
<?php echo $this->getChildHtml('contactForm') ?>
but it's not showing up, even after adding xml to 2 files, i've added this line into app/design/frontend/base/default/layout/catalog.xml, right below <reference name="content">
<block type="core/template" name="contactForm" form_action="/contacts/index/post" template="contacts/form.phtml"/>
and also added this code to app/design/frontend/base/default/layout/default.xml (this file didn't even exist)
<default>
<cms_page>
<reference name="content">
<block type="core/template" name="contactForm" as="contactForm" template="contacts/form.phtml">
<action method='setBlockId'><block_id>contactForm</block_id></action>
</block>
</reference>
</cms_page>
I have the idea i'm not putting the xml in the right files, but i have no idea witch files to use otherwise, and can't find any other tips than these two on the internet.
Can someone help me out?
You can add the contact form to any CMS page using CMS tags like this inside the CMS content box:
{{block type="core/template" name="contactForm" template="contacts/form.phtml"}}
You could create a new page, and then inssert that snippet to show the contact form on this new page.
you could also make a copy of form.phtml and modify it for your needs, and update the tag code to use the new template.
Please use the following function to display inside templates.
<?php echo $this->getLayout()->createBlock("Magento\Framework\View\Element\Template")->setTemplate("Magento_Contact::form.phtml")->toHtml(); ?>
You can't just create a default.xml - to let Magento know that that a layout xml file exists you'd have to go through and create a module and specify the layout file's location. that is a very long winded way round of trying to achieve what you need.
Also I believe the handle <cms_page> is not supported. Referencing a block in a CMS page is best done by placing your xml in the layout update section under the 'design' tab for that CMS page.
So it's fairly straight forward to put the contact form into a CMS page - again use the layout update facility. Simply enter the following xml...
<reference name="content">
<block type="core/template" name="contactForm" template="contacts/form.phtml"/>
</reference>
Note you don't need a layout handle - Magento already knows of course the specific CMS page you are inserting content into because this is a layout update for that page. So in the above you are just referencing the structural block you want to add to (in this case content) and telling Magento to add the contactForm block along with its associated template location.
I've worked on a site using this method before. you will likely find that once you have called the contactform into your CMS page, and despite appearing successfully in a browser, the form doesn't actually post. In fact the form will only send if used from the intended page yoursite.com/contacts
Simple fix. Create the file app/design/frontend/YOUR-PACKAGE/YOUR-THEME/template/contacts/form.phtml and duplicate the required content from the default form.phtml into it.
Change the script at the bottom of the form.phtml file from:
<script type="text/javascript">
var contactForm = new VarienForm('contactForm', true);
</script>
To:
<script type="text/javascript">
elem = $("contactForm");
elem.writeAttribute('action', '/contacts/index/post');
</script>
And your CMS page contact form will now post as required.
Just as an addition (you may or may not find this useful).
I had a need for the contact form on my CMS page to be different from the standard form which appears on contacts. This is really simple too using the method above.
Duplicate the file app/design/frontend/YOUR-PACKAGE/YOUR-THEME/template/contacts/form.phtml and rename as something like app/design/frontend/YOUR-PACKAGE/YOUR-THEME/template/contacts/customForm.phtml - then make your required amends like different fields etc...
now in the layout update for the CMS page you want it to appear in simply add the following xml:
<reference name="content">
<block type="core/template" name="customContactForm" template="contacts/customForm.phtml"/>
</reference>
And that's it.
Note how the block name has changed - the block name can literally be anything - Magento doesn't care what you call it but I would obviously recommend something logical!

Change position of currency selector in Magento

Currently, the currency selector is at the top, here’s my development site:
http://nordschleife.metaforix.net/118/118/index.php/kyocera.html
However, I would like to switch the currency selector to just under “Price” heading of the table.
I tried
echo $this->getCurrency();
but there’s nothing. I guess I need some method like getCurrencyHtml(), but it seems that there’s no such a method.
Or must I edit layout files, and how should I go about doing this?
I can show you a way to do this, but in order to understand what's going on, you'll need to have at least a basic grasp of how Magento's layout files work. For that you should read the designer's guide here and a basic explanation of how it all works here.
Now there are several way of handling this, but I think the easiest way is to simply use the existing currency block. Seeing as you'll be putting it in that tiny cell I assume you wont be needing the "Select your Currency" headline. So we'll need a new template.
A block in Magento consists of two files, a block class that does all the work of generating dynamic content and a template file which uses the block class' methods along with some html to create the final result. The heavy lifting of getting the currency options is already done by the block class so if we can use that paired with a new template file we'll be set.
The existing declaration in the layout files and specifically directory.xml is
<block type="directory/currency" name="currency" before="catalog.leftnav" template="directory/currency.phtml"/>
So the template file is app\design\frontend[interface][theme]\template\directory\currency.phtml
Copy that to currency2.phtml and in there remove the heading.
Now to create a new block named "currency2" consisting of the old block class and the new template file we write
<block type="directory/currency" name="currency2" as="currency2" template="directory/currency2.phtml"/>
We'll be using that in /template/catalog/product/list.phtml so open catalog.xml and put the new block declaration under
<block type="catalog/product_list" name="product_list" template="catalog/product/list.phtml">
in the appropriate section (I assume catalog_category_default).
Finally open /template/catalog/product/list.phtml and add
<?php echo $this->getChildHtml('currency2'); ?>
where you want the block to appear.

Categories