I have a custom block in my layout file like this:
<block type="xxx/xxx" name="xxx" template = "bar.phtml">
<label>Foo</label>
</block>
How do I get the value of label from bar.phtml ?
Please note I do not want to use setData function to set my variable and pass it. I want to extract the value inside tags from the phtml (or anywhere else). I hope its clear.
I don't think there is a really classy Magento way of doing it since the label of a block purpose is not to be displayed as far as we speak of the frontend.
label : This element is introduced since Magento 1.4. It defines the label of the handle which is shown as a descriptive reference in some areas of the admin panel.
I would really warmly recommend you to stay away from the code below . But if that is really what you want to achieve, this is a way :
First we get the layout = a big xml concatenation of the layout for that page containing the xml where the block is defined, and thus our label
$layout = $this->getLayout();
Then we get the current block name in the layout
$currentBlockNameInLayout = $this->getNameInLayout();
We can, then get the XML node representing the current block in the template.
getXpath()
does returns an array, so that is why I used list()
to get the first item off of this array
list($currentBlockInLayout) = $layout->getXpath("//block[@name='".$currentBlockNameInLayout."']");
We have what we want and can echo its label element
echo $currentBlockInLayout->label;
Take care though, this is an object of type Mage_Core_Model_Layout_Element
so if you want to do anything else than displaying it, you would have to use the __toString()
method
var_dump( $currentBlockInLayout->label->__toString() );
Full code :
$layout = $this->getLayout();
$currentBlockNameInLayout = $this->getNameInLayout();
list($currentBlockInLayout) = $layout->getXpath("//block[@name='".$currentBlockNameInLayout."']");
echo $currentBlockInLayout->label;
var_dump( $currentBlockInLayout->label->__toString() );
In your XML, use the action method setData
<block type="xxx/xxx" name="xxx" template = "bar.phtml">
<action method="setData">
<label>Foo</label>
</action>
</block>
Then in your bar.phtml
file, you can retrieve it using $this->getData('label')
:
<?php echo $this->getData('label') ?>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.