Twig事件侦听器(hook)在模板变量中 [英] Twig event listener (hook) in template variable

查看:160
本文介绍了Twig事件侦听器(hook)在模板变量中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我心目中有一些需要在当前项目中实施的东西。我将在下面的例子中解释一下:



我将在许多模板中使用以下变量结构:

  {%if article.tags is iterable%} 
{article for article.tags%}
{{tag.title}}
{%endfor%}
{%endif%}

正如你可以看到的在上面的#1行中,我将通常将使用从PHP的标签数据列表的article变量数据分配给Twig模板加载器。但是,我不想从数据库中加载标签数据并分配给模板,因为我认为即使使用标签数据也将使用我的服务器连接和资源。



所以我认为最好的方法是使用事件侦听器(hook)的概念,在Twig内部实现,下面是进程:



1 )我正在准备一个监听器,在我的PHP文件的某个地方加载模板:$ this-> twig-> addListenerMethod($ this,'tags');



2)当Twig解析tags变量时,它将检查是否有一个具有与变量相同的方法名称的侦听器。



3)当侦听器发现,Twig将通过传递父对象(article)作为第一个参数来调用指定的方法。



4)当调用该方法时,将按照它应该并返回所需的标签数据列表。



我的问题是:



1)其中的Twig源文件我可以开始在Twig中查看和实现事件监听器(hook)概念?



2)有没有人已经使用如上所述的Twig有相同的实现? / p>

3)或有人有一些建议?



我希望我可以有很多建议,而



谢谢。



请问。

解决方案

我已经找到了我自己的问题的解决方案:



我只需要分配

  $ template-> render(array(
'article'=> new Article()
);

下面是对象文章如下所示:

 类文章
{
public function tags($ args)
{
return array (
数组(
'title'=>'oke',
'desc'=>'lorem ipsum'
),
数组(
'title'=>'oke 1',
'desc'=>'lorem ipsum 1'

);
}
}

如果你想传递和参数的方法标签,只需使用以下示例:

  {%if article.tags('param_1' ,'param_n')是可迭代的%} 
{%for article in article.tags('param_1','param_n')%}
{{tag.title}}
{%endfor %}
{%endif%}


I'm having something in my mind that need to be implemented in the current project. I'll explain with a example below:

I will be having below variables structure used in a lot of templates:

{% if article.tags is iterable %}
    {% for tag in article.tags %}
        {{ tag.title }}
    {% endfor %}
{% endif %}

As you can see at the #1 line above, i will then assign "article" variable data that having a list of "tags" data from PHP to the Twig template loader, in common usage. But, I don't want to load the "tags" data from database and assign to the template, because i think it will use my server connection and resource even the "tags" data used or not.

So i think the best approach is to using event listener (hook) concept, implemented inside the Twig, below is the process:

1) I'm preparing a listener at somewhere in my PHP file before the template loaded: $this->twig->addListenerMethod($this, 'tags');

2) When the Twig parse the "tags" variable, it will check if there are a listener that having the same method name attached to the variable.

3) When listeners found, the Twig will call the method specified by passing the parent object (article) as it first parameter.

4) When the method called, it will process as it should and return the "tags" data list needed.

My question are:

1) In which Twig source file that I can start to look and implement the event listener (hook) concept in Twig?

2) Is there someone already having the same implementation using Twig as i described above?

3) or somebody having some suggestions?

I wish i can have a lot of suggestions about this, while i'm digging into the Twig source also.

Thanks.

Kind regards.

解决方案

I have found the solution for my own question:

I just need to assign a "Article" object to the template:

$template->render(array(
  'article' => new Article()
);

Below is how the object "Article" look like:

class Article
{
  public function tags($args)
  {
    return array(
      array(
        'title' => 'oke',
        'desc' => 'lorem ipsum'
      ),
      array(
        'title' => 'oke 1',
        'desc' => 'lorem ipsum 1'
      )
    );
  }
}

And if you want to pass and arguments to the method "tags", just use below example:

{% if article.tags('param_1', 'param_n') is iterable %}
  {% for tag in article.tags('param_1', 'param_n') %}
    {{ tag.title }}
  {% endfor %}
{% endif %}

这篇关于Twig事件侦听器(hook)在模板变量中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆