what is the correct way to add block as a child of an another block in xml in magento? - magento

I have two custom magento blocks called "exe2" and "example".
The exe2 block gets the contents of the example block using the getChildHtml function, but it keeps just returning empty strings to me and only the exe2 contents make it to the screen.
Here is my code my code:
exe2 xml file:
<layout version = "0.1.0">
<test_example_view>
<block type = "exemplum_mod2/exe2" name = "exemplum.mod2.exe2" template="exemplum/mod2/exe2.phtml">
<reference name = "exemplum.mod2.exe2">
<block type="exemplum/example" name="exemplum.example" template="exemplum/example1/example.phtml" />
</reference>
</block>
</test_example_view>
example xml file
<layout version="0.1.0">
<default>
<block type="exemplum/example" name="exemplum.example" template="exemplum/example1/example.phtml" />
</default>
Here is the exe2 phtml file that has the getChildHtml call in it:
<h1> 2nd </h1>
<?php
echo $this->getMessage();
echo $this->getChildHtml("exemplum.mod2.exe2");
?>
hello
example.phtml file:
<h1>Hello there</h1>
And finally heres the controller file that loads the blocks:
<?php
class exemplum_example1_ExampleController extends Mage_Core_Controller_Front_Action{
public function viewAction(){
$this->loadLayout();
$block = $this->getLayout()->createBlock('exemplum_mod2/exe2');
$block->setTemplate("exemplum/mod2/exe2.phtml");
$this->getLayout()->getBlock('content')->append($block);
$this->renderLayout();
}
}
What is the correct way to add one block as the child block of a another block in the xml file? Every solution that i found on google didn't seem to work in my case so what am i doing wrong here?

Related

Adding title to cms footer block

I added a second footer block with links by adding the following code to my default.xml in my theme:
(app/design/frontend///Magento_Theme/layout/default.xml)
<referenceContainer name="footer">
<block class="Magento\Framework\View\Element\Html\Links" name="footer_links_custom">
<arguments>
<argument name="css_class" xsi:type="string">footer links</argument>
</arguments>
</block>
</referenceContainer>
<referenceBlock name="footer_links_custom">
<block class="Magento\Framework\View\Element\Html\Link\Current" name="2custom-link">
<arguments>
<argument name="label" xsi:type="string">Custom Links</argument>
<argument name="path" xsi:type="string">page-url</argument>
</arguments>
</block>
</referenceBlock>
What is the easiest way to add a title to the my footer_links_custom block, is there any way to do this in a simple manner? I've tried setting an argument "title" but that didn't work obviously. Is there any way we can know all the attributes there are for a certain block? (css_class, label, path, ...)
Is there no .phtml file for the footer links block?
Magento 2 leaves me behind with a lot of questions...
Thanks for the help!
In /magento/app/design/frontend/theme/theme/Magento_Theme/ You can set up the structure needed for this. and documentation to help can be found at:
http://devdocs.magento.com/guides/v2.0/frontend-dev-guide/bk-frontend-dev-guide.html
In Magento_Theme/layout/default.xml you will find your block declarations, such as what you listed above already.
In Magento_Theme/Block/Html/Footer.php you can set up your controller with your Mage interface, such as...
class Footer extends \Magento\Framework\View\Element\Template implements \Magento\Framework\DataObject\IdentityInterface
{
protected $_copyright;
....
public function getCopyright()
{
if (!$this->_copyright) {
$this->_copyright = $this->_scopeConfig->getValue(
'design/footer/copyright',
\Magento\Store\Model\ScopeInterface::SCOPE_STORE
);
}
return $this->_copyright;
}
...
Magento_Theme/templates/html is where you can put your .phtml that your used to working with in Magento 1. Its not the exact same code obviously, but the idea is still the same here. for example, here is a footer that isn't code but stored in a db:
$om = \Magento\Framework\App\ObjectManager::getInstance();
$manager = $om->get('Magento\Store\Model\StoreManagerInterface');
$store = $manager->getStore(null)->getName();
$connection = $om->create('\Magento\Framework\App\ResourceConnection');
$conn = $connection->getConnection();
$select = $conn->select()
->from(
['o' => 'xyz_sitedata_items'],
['footer']
)
->where('o.storename=?', $store);
$data = $conn->fetchAll($select);
echo $data[0]['footer'];
for the record, using the object manager like this isn't recommended.
for a specific answer to the question posed, i would look to adding code to your footer.php. you can add data in with the construct, as well as reading all the data for the associated block there.

Magento second header

I have a Magento store with two separate headers. Let's say out of 20 pages 15 use header.phtml and 5 use headerGreen.phtml. I've already created my custom layout module however I can't seem to get the new header to appear on select pages. The new headerGreen.phtml file has been created as well. The issue lies when I try to call the new header in the newly created layout page.
<?php echo $this->getChildHtml('headerGreen') ?>
I dont think I've added it correctly to the local.xml and was hoping someone could help.
<default>
<reference name="header">
<block type="core/template" name="header" template="page/headerGreen.phtml" />
</reference>
</default>
The function getChildHtml is expecting you to send a name of a child in the layout as first parameter
app/code/core/Mage/Core/Block/Abstract.php defines it :
/**
* Retrieve child block HTML
*
* #param string $name
* #param boolean $useCache
* #param boolean $sorted
* #return string
*/
public function getChildHtml($name = '', $useCache = true, $sorted = false)
{
// the code is does not really matter here
}
So based on your layout you should actually do
<?php echo $this->getChildHtml('header') ?>
Because header is the name attribute in this xml node of your layout :
<block type="core/template" name="header" template="page/headerGreen.phtml" />
As said in the comments, your layout is also looking strange because you are actually telling that your new header is going to be a child of the current header.
Two thinks you can do :
Use the powerfulness of the action nodes :
<default>
<reference name="header">
<action method="setTemplate">
<template>page/headerGreen.phtml</template>
</action>
</reference>
</default>
Or redefine the whole header block
<default>
<reference name="root">
<block type="core/template" name="header" as="header" template="page/headerGreen.phtml" />
</reference>
</default>

Magento custom module admin block display

struggling getting access to an admin block ive created.
Ive created a module...it has many elements, all working. Ive got header includes added to certain admin pages no problem, using my adminhtml layout update xml file.
The issue seems to be it cant access/see my block...so muct be referencing wrong, even though ive been following the 'module creator' extension files.
Another silly issue i think, been at this too long! :)
First the code:
Mworkz/MyModuleName/Block/Adminhtml/MyBlock.php
class Mworkz_MyModuleName_Block_Adminhtml_MyModuleName extends Mage_Adminhtml_Block_Widget_Grid_Container
{
public function __construct()
{
var_dump('WE ARE IN THE ADMIN BLOCK!');exit;
$this->_controller = 'adminhtml_mymodulename';
$this->_blockGroup = 'mymodulename';
$this->_headerText = Mage::helper('mymodulename')->__('Item Manager');
$this->_addButtonLabel = Mage::helper('mymodulename')->__('Add Item');
parent::__construct();
}
}
My layout xml (this file works, and is referenced right, as my admin header includes work)
Should point out i have a custom tab and controller...all working.
<?xml version="1.0"?>
<layout version="0.1.0">
<mymodulename_adminhtml_mymodulename_index>
<reference name="head">
<action method="addJs"><script>Mworkz/MyModuleName.js</script></action>
</reference>
<reference name="content">
<block type="mymodulename/adminhtml_mymodulename" name="mymodulename" ></block>
</reference>
</mymodulename_adminhtml_mymodulename_index>
</layout>
I expect to see the var_dump stmt ive inserted....but it doesnt display.
Thanks in advance...
file naming! Simple caps issue...
My block file was called '...Adminhtml/MyModuleName.php',
My block identifier inside the file was '...Adminhtml_Mymodulename {'
Another set of working code snippets for adminhtml block users i suppose!
Thanks

Magento Adding a Block to an index Action Page

I am creating a module that will have its own page at mysite.com/memymodule/index/index
I want to add functionality from a template file from at templates/me/template.phtml
I have an index controller like this:
<?php
class me_mymodule_IndexController extends Mage_Core_Controller_Front_Action {
public function indexAction() {
$this->loadLayout();
$this->renderLayout();
}
}
?>
I have a layout update mymodule.xml that looks like this:
<?xml version="1.0"?>
<layout version="0.1.0">
<mymodule_index_index>
<reference name="content">
<block type="core/template" name="me.mymodule" template="me/template.phtml" />
</reference>
</mymodule_index_index>
</layout>
The frontName of the module is mymodule
When the page renders the content block is completely empty and the content of template.phtml is completely ignored.
Help much appreciated :-)
Try this Code within your Controller's Action -
var_dump(Mage::getSingleton('core/layout')->getUpdate()->getHandles());
exit("Your Layout Path is ".__LINE__." in ".__FILE__);
This code tells you about the Tag which you need to create within Layout.xml.
Also check Config.xml that the Layout Update Section is correctly defined or not.
Hope it'll be beneficial for you.
THANKS

Magento XML layout

In the code above, what is the meaning of placing "as="footer_links"?
The as="x" syntax defines a name that templates can use to invoke a block. So, for the following:
<block type="text/html" name="outer_block" as="outer_block" template="outer_block.phtml">
<block type="text/html" name="inner_block" as="inner_block" template="inner_block.phtml">
</block>
Inside of outer_block.phtml, you can invoke this to render inner_block.phtml:
<?php print $this->getChildHtml("inner_block"); ?>
Hope that helps!
Thanks,
Joe

Resources