十月 CMS。如何将变量从页面传递到组件?
OctoberCMS. How to pass variable from page to component?
我在页面中定义了一个变量:
{% set service = builderDetails.record %}
我想将这个变量传递给一个组件:
{% component 'Variations' service=service %}
但是这个方法不行。我该怎么做?
我猜您需要将 builderDetails.record
传递给您的 component 'Variations'
然后您想访问 component 'Variations'
的 default.htm
中的变量并显示有关它的一些详细信息。
为此你需要利用组件的 onRender
方法
在你的component 'Variations'
里面
public function onRender()
{
$this->page['record'] = $this->page->components['builderDetails']->record;
}
$this->page->components
将页面中所有可用的组件作为数组保存。
builderDetails
是添加到页面中的组件 Record Details
(Builder Plugin) 的别名。
现在在你的 default.htm
里面
您可以访问 record
变量并根据需要使用它
{{ record.name }}
we assume here that your record has attribute name
如有任何需要请留言。
在 Twig .htm 部分中
<div class="sidebar-widget">
{% component "postBlock" score=55 name="Johan" %}
<div class="nonsense meta">
{% partial 'nonsense' score=75 name="Marga" %}
</div>
</div>
在废话 twig 中是前一个 .htm 部分的子
<div class="sidebar-element">
<div>My name is: {{ name|default('John Doe') }}</div>
<div>My score is: {{ score|default('No score') }}</div>
</div>
在组件postBlock、onRender函数
function onRender() {
$score = $this->property('score'); // 55
$name = $this->property('name'); // Johan
}
要从父树枝传递到子树枝,您不必使用 onRender 函数
在组件中,只有当你想修改东西时,才会从那里传递到树枝。
您不必将其分配给页面。您可以通过以下方式访问它:
__SELF__.property('property_name')
在组件中定义 属性 之后:
{% component 'Variations' service=service %}
在你的组件中default.htm
{{__SELF__.property('service')}}
我在页面中定义了一个变量:
{% set service = builderDetails.record %}
我想将这个变量传递给一个组件:
{% component 'Variations' service=service %}
但是这个方法不行。我该怎么做?
我猜您需要将 builderDetails.record
传递给您的 component 'Variations'
然后您想访问 component 'Variations'
的 default.htm
中的变量并显示有关它的一些详细信息。
为此你需要利用组件的 onRender
方法
在你的component 'Variations'
public function onRender()
{
$this->page['record'] = $this->page->components['builderDetails']->record;
}
$this->page->components
将页面中所有可用的组件作为数组保存。
builderDetails
是添加到页面中的组件 Record Details
(Builder Plugin) 的别名。
现在在你的 default.htm
您可以访问 record
变量并根据需要使用它
{{ record.name }}
we assume here that your record has attribute
name
如有任何需要请留言。
在 Twig .htm 部分中
<div class="sidebar-widget">
{% component "postBlock" score=55 name="Johan" %}
<div class="nonsense meta">
{% partial 'nonsense' score=75 name="Marga" %}
</div>
</div>
在废话 twig 中是前一个 .htm 部分的子
<div class="sidebar-element">
<div>My name is: {{ name|default('John Doe') }}</div>
<div>My score is: {{ score|default('No score') }}</div>
</div>
在组件postBlock、onRender函数
function onRender() {
$score = $this->property('score'); // 55
$name = $this->property('name'); // Johan
}
要从父树枝传递到子树枝,您不必使用 onRender 函数 在组件中,只有当你想修改东西时,才会从那里传递到树枝。
您不必将其分配给页面。您可以通过以下方式访问它:
__SELF__.property('property_name')
在组件中定义 属性 之后:
{% component 'Variations' service=service %}
在你的组件中default.htm
{{__SELF__.property('service')}}