从组件中获取变量

问题描述 投票:0回答:3

我想从组件中获取变量。这是我的代码:

<!-- Gallery -->
<div class="galeryWraper">
<div class="row" >
   <h3>{{ galeria1.post.title }} {{ galeria1.post.description }}</h3>
        <div class="col-sm-8">

            {% component 'galeria1' %}

        </div>
      </div>
 </div>

 <!-- End Gallery -->

Galery工作正常。但画廊标题和描述没有显示出来。

有人知道为什么吗?

octobercms
3个回答
0
投票

要从组件的Twig partials访问该属性,请使用引用Component对象的__SELF__变量:

{{ __SELF__.property('title') }}


2
投票

post必须在该组件中定义

public $post = null;

现在在onRun或某些你需要为它分配一个值

public function onRun() {
    $this->post = 'value-text';
}

那么只有像它一样可用

{{ galeria1.post }} 
// output 'value-text' 

所以可能是你的post变量直接传递给未分配给组件的页面

例如 :

public function onRender()
{
    $this->page['post'] = 'some data';
}

要么

public function onRun()
{
    $this->page['post'] = 'some data';
}

所以此帖子直接分配给页面而不是组件本身。

方案:

你可以做这样的事情

public $post= '';
public function onRender OR onRun() // choose method where data is added to page
{
    $this->post = 'some data';
    $this->page['post'] = $this->post;
}

2
投票

您可以像这样直接访问这些变量

{{ post.title }} {{ post.description }}

如果它们在您的组件上设置如下

public function onRun(){
    $post = array();
    $post['title'] = 'some-title';
    $post['description'] = 'some-description';
    $this->page['post'] = $post;
}
© www.soinside.com 2019 - 2024. All rights reserved.