I'm just ramping up with Magento and somethings that appear rather intuitive on the surface, don't seem to be working as expected. Here is a very basic template example that I am trying to get working at a "hello world" level before digging deeper.
In the "header" block definition in my page.xml layout I have the following block included:
<block type="core/text_list" name="helloRob" as="helloRob" template="page/html/hellorob.phtml" />
Then, in my header.phtml file, I have added...
<?php echo $this->getChildHtml('helloRob'); ?>
The contents of the hellorob.phtml file are:
<h1>Hello Rob</h1>
Yet, when displaying the page, I don't get the expected H1 element output in the header. If I remove the "getChildHtml" call and replace it with the actual HTML, then it displays the H1. So I know that my custom header.phtml file is getting loaded. I've also been able to confirm that I am adding my block to the correct layout XML file, because when I comment out another block in the same file, the commented out block no longer appears on my page.
I've checked over the code looking for something obvious, like a typo. I've also checked for any naming conflicts -- hence "helloRob" rather than "helloWorld".
I feel like I am missing something incredibly obvious, like looking for your glasses only to find them on top of your head.
Any help -- with the code -- not my glasses -- would be appreciated!
--Rob
The problem here is your understanding of block types. Blocks in Magento do different things, the one you've used here core/text_list serves a specific purpose. You can view what a block does by looking at it's code Block file, core/text_list is located in app/code/core/Mage/Core/Block/Text/List.php
protected function _toHtml()
{
$this->setText('');
foreach ($this->getSortedChildren() as $name) {
$block = $this->getLayout()->getBlock($name);
if (!$block) {
Mage::throwException(Mage::helper('core')->__('Invalid block: %s', $name));
}
$this->addText($block->toHtml());
}
return parent::_toHtml();
}
As you can see it's purpose is to just print out child blocks. This block type is used for text menus' and as blocks purely as containers for other blocks.
As you are looking for a block to just print the contents of a template so type="core/template" will do just fine, it's _toHtml() method is
protected function _toHtml()
{
if (!$this->getTemplate()) {
return '';
}
$html = $this->renderView();
return $html;
}
Related
I have an extension that overrides getSkinUrl() and intercepts images coming through it, makes changes, then continues. This works for any images referenced in PHTML files.
However, This isn't catching images in CMS blocks. In the CMS blocks, I'm using
{{skin url="images/your_amazing_image.gif"}}
Magento's template variables are defined in the following file.
app/code/core/Mage/Core/Email/Template/Filter.php
For the {{skin}} variable, the following code is used around line 264 in filter.php
public function skinDirective($construction)
{
$params = $this->_getIncludeParameters($construction[2]);
$params['_absolute'] = $this->_useAbsoluteLinks;
$url = Mage::getDesign()->getSkinUrl($params['url'], $params);
return $url;
}
I'm not sure how you are overriding getSkinUrl, but make sure that the getSkinUrl method is the same method you are overriding.
I have researched this topic pretty thoroughly but can't find an answer.
I am trying to include a Magneto head into a WordPress page. I created a new wordpress template and added the following code to it.
try {
require_once ABSPATH . '/app/Mage.php';
if (Mage::isInstalled()) {
$mage = Mage::app();
Mage::getSingleton('core/session', array('name' => 'frontend'));
if(Mage::getDesign()->designPackageExists('xxx')) {
Mage::getDesign()->setPackageName('xxx');
Mage::getDesign()->setTheme('xxx');
}
// init translator
$mage->getTranslator()->init('frontend');
// init head
$head = $mage->getLayout()->getBlockSingleton('page/html_head');
} }
Then a bit further down in the template I have
echo $head->toHtml();
Now what is happening is some parts of the head are being echoed and some parts are not.
When I go into head.phtml and try to figure out what is happening I notice that any line that contains
$this->getChildHtml()
does not get echoed.
I looked at this example and noticed that the author is manually adding the html and CSS. Why is this? Why don't they get added automatically? Is this a related problem
Thanks
To show a block that is generated inside the header block, you need to first create it, then set it as child of the header block.
eg. Here is how I display within Wordpress a Magento header block, including the currency drop-down block that was generated by getChildHtml() inside the original header.phtml:
Mage::getSingleton('core/session', array('name' => 'frontend'));
$session = Mage::getSingleton("customer/session");
$layout = Mage::getSingleton('core/layout');
$headerBlock = $layout->createBlock('page/html_header')->setTemplate('page/html/header.phtml');
$currencyBlock = $layout->createBlock('directory/currency')->setTemplate('currency/currency.phtml');
$headerBlock->setChild('currency_selector', $currencyBlock);
$headerBlock = $headerBlock->toHtml();
Then you can write the block where you need it on the page:
echo $headerBlock;
I know it's a little late but hopefully this helps others with this issue.
Are you familiar with how magento layouts are rendered? With $head = $mage->getLayout()->getBlockSingleton('page/html_head'); you create a new block without any children. That's why the author needs to add JS and CSS again. To load the default head block have a look at this thread Load block outside Magento. You can load it with $layout->getBlock('head')->toHtml();.
I'm programming an ajax extension to Magento and I'm having trouble when I try to make checkout/cart work with it.
I want to reload the entire block, including the item and also the cart totals after a change in any product in the cart.
I've modified the layout as many sites said and I can't get the checkout/cart block render well.
My two solutions were:
1° Tryed to update the layout via an xml file, and I get a false in $block_cart.
I've a class
Mati_Ajax_CartController extends Mage_Checkout_CartController
{
...
public function updateShoppingCartAction()
{
...
$block_cart = $this->getLayout()->getBlock('checkout/cart');
...
}
}
and a xml file (which I'm sure i'ts being loaded beacause the js pointed there is loading)
<checkout_cart_updateshoppingcart>
<update handle="checkout_cart_index" />
</checkout_cart_updateshoppingcart>
2° Tryed to create the block
public function updateShoppingCartAction()
{
...
$block_cart = $this->getLayout()->getBlockSingleton('checkout/cart')->setTemplate("checkout/cart.phtml")->toHtml();
...
}
And here I get the block, but when the template executes $this->getChildHtml('totals');
It gets a false anwer, so the webpage has some differences with the previousone
Does anybody knows how to make this work ?
In your first attempt just append ->toHtml();
Alternatively you could have included that as a command in the xml output="toHtml"
Check how the checkout/onepage/review functions if you are looking for advice.
There they access the response object and then set the body of the response to the html variable, in your case $block_cart
The key was in the xml file
<ajax_cart_updateshoppingcart>
<update handle="checkout_cart_index" />
</ajax_cart_updateshoppingcart>
Is there a way in magento to create a block and call its toHtml() method without having to load the entire layout object.
For example, in one of my ajax controllers i want to send a certain block as json output and i am not interested in any other blocks but i am forced to do the following:
$this->loadLayout();
$this->getLayout()->getBlock('my_block_name')->toHtml();
Which loads the entire layout which seems unnecessary.
I just accomplished this like so:
In my layout XML for my module:
<mymodule_ajax_action>
<block type="core/template_facade" name="root" template="path/to/template/file.phtml"/>
</mymodule_ajax_action>
By naming the block "root", it replaced the entire layout with just this template file.
So in my controller:
public function actionAction() {
$this->loadLayout();
$this->renderLayout();
}
returns just that block.
You can try the following:
$layout = Mage::getSingleton('core/layout');
$html = $layout
->createBlock('module/block_type')
->setTemplate('template/file.phtml')
->toHtml();
From your code, it looks as though you are in a controller so you could shorten the code a little to the following (absolutely no difference in functionality, simply 1 less line of code)...
$html = $this->getLayout()
->createBlock('module/block_type')
->setTemplate('template/file.phtml')
->toHtml();
In Magento CE 1.5.1, why is getLogoAlt not available for the content section though it is available for the header section?
Inside the content section of the home page, my theme's home.phtml has
<h1 class="no-display"><?php echo $this->getLogoAlt() ?></h1>
which outputs as
<h1 class="no-display"></h1>
But header.phtml,
<h4><strong><?php echo $this->getLogoAlt() ?></strong></h4>
properly outputs as
<h4><strong>Buy widgets here!</strong></h4>
Puzzled...
The "header section" is a block added with the following layout update XML
<block type="page/html_head" name="head" as="head">
The block's type is page/html_head, which translated to the class Mage_Page_Block_Html_Header. If you look at the class definition, you can see the header.phtml template being set.
#File: app/code/core/Mage/Page/Block/Html/Header.php
public function _construct()
{
$this->setTemplate('page/html/header.phtml');
}
When you use $this->someMethod() from a template, you're calling a method on the template's block class. Each template "belongs" to a block. If we look at the header class again
#File: app/code/core/Mage/Page/Block/Html/Header.php
public function getLogoAlt()
{
if (empty($this->_data['logo_alt'])) {
$this->_data['logo_alt'] = Mage::getStoreConfig('design/header/logo_alt');
}
return $this->_data['logo_alt'];
}
we can see the definition of getLogoAlt.
The other template you mentioned, home.phtml, is added with the following layout update xml
<block type="core/template" name="default_home_page" template="cms/default/home.phtml"/>
Its block is a core/template block, which translates to Mage_Core_Block_Template. This block does not have a getLogoAlt method. However, like all blocks, it does have Magento's magic getters and setters. You can "set" and "get" data properties on Magento blocks like this
$this->setFooBar('setting a value for the foo_bar data property');
echo $this->getFooBar();
even if the block doesn't have those methods defined. So, this means you can call getLogoAlt on any block without an error being thrown, but only the header block is going to return a value. If you want that value in any template, you can just call
$logo_alt = Mage::getStoreConfig('design/header/logo_alt');
at the top of your template, and then use $logo_alt wherever you'd like.