如何从Magento的phtml中获取xml中的块标签?

时间:2023-01-24 11:05:24

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 ?

如何从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.

请注意我不想使用setData函数来设置我的变量并传递它。我想从phtml(或其他任何地方)中提取标签内的值。我希望它清楚。

2 个解决方案

#1


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.

我不认为有一种非常优雅的Magento方式,因为就我们所说的前端而言,目标的标签不会显示。

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.

label:这个元素是从Magento 1.4开始引入的。它定义了句柄的标签,在管理面板的某些区域显示为描述性参考。

Source

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

首先,我们得到布局=该页面布局的一个大的xml串联,其中包含定义块的xml,因此我们的标签

$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

然后我们可以获得表示模板中当前块的XML节点。 getXpath()确实返回一个数组,这就是我使用list()从这个数组中获取第一个项目的原因

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

请注意,这是Mage_Core_Model_Layout_Element类型的对象,因此如果您想要执行除显示之外的任何操作,则必须使用__toString()方法

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

#2


In your XML, use the action method setData

在XML中,使用action方法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'):

然后在bar.phtml文件中,您可以使用$ this-> getData('label')检索它:

<?php echo $this->getData('label') ?>

#1


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.

我不认为有一种非常优雅的Magento方式,因为就我们所说的前端而言,目标的标签不会显示。

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.

label:这个元素是从Magento 1.4开始引入的。它定义了句柄的标签,在管理面板的某些区域显示为描述性参考。

Source

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

首先,我们得到布局=该页面布局的一个大的xml串联,其中包含定义块的xml,因此我们的标签

$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

然后我们可以获得表示模板中当前块的XML节点。 getXpath()确实返回一个数组,这就是我使用list()从这个数组中获取第一个项目的原因

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

请注意,这是Mage_Core_Model_Layout_Element类型的对象,因此如果您想要执行除显示之外的任何操作,则必须使用__toString()方法

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

#2


In your XML, use the action method setData

在XML中,使用action方法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'):

然后在bar.phtml文件中,您可以使用$ this-> getData('label')检索它:

<?php echo $this->getData('label') ?>