树枝 - 将功能传递到模板中 [英] twig - pass function into template

查看:106
本文介绍了树枝 - 将功能传递到模板中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前,我将我的函数放在一个类中,并将此类的一个实例传递给模板,并将我所需的函数作为一个类方法调用。

  {{unneededclass.blah()}} 

我需要像下面这样做 p>

  {{blah()}} 

是否有可能?

解决方案

更新5/14/2015



评论者指出我大部分都是错的。如果你真的需要一个函数,而不是过滤器或宏,你可以按照Twig文档中的建议来做:

  $ twig =新的Twig_Environment($ loader); 
$ function = new Twig_SimpleFunction('blah',function(){
// ...
});
$ twig-> addFunction($ function);

并使用像

  {{blah()}} 






总之,不,这是不可能的。



但是,希望不会丢失!

过滤器



如果您的函数 blah()用于修改现有变量,则它是<



 

// $ in your PHP
function format_date($ date_string,$ format_string){
return date($ format_string,strtotime($ date_string));
}
$ twig_env-> addFilter('format_date',new Twig_Filter_Function('format_date'));

{#您的模板#}
{{some_date | format_date('n / j / Y')}}

(第一个参数是您正在过滤的变量,第二个参数是通过正常方式提供的)





如上所述,如果您的函数只输出HTML,那么它是的理想选择。 b
$ b

举例:

  {#模板#} 
{宏say_hello()%}
< p>哦! Hello,world!< / p>
{%endmacro%}

{#...稍后...#}
{{_self.say_hello()}}



或带参数:

  { %宏输入(名称,值,类型)%} 
< input type ={{type | default('text')}}name ={{name}}value ={{value }}>
{%endmacro%}

{{_self.input('phone_number','867-5309')}}
{{_self.input('subscribe','是','checkbox')}}



为什么?



需要记住的是,根据MVC,Twig模板表示视图。这意味着它们在环境方面是孤立的,并且只能表示通过在 $ template-> render()中传递的数据数组传递它们的 context / code>方法。

这是一件好事,因为它可以将您的演示文稿与逻辑和数据分离。如果你可以任意调用函数,那么你突然增加了这种耦合,这是一件坏事。



另一个原因是这个是PHP处理回调的方式。想想你将如何将该功能传递到你的模板...可能是这样的:

 函数blah(){
return< p>哦!Hello,world!< / p>;
}

$ template = $ twig_env-> loadTemplate('template.html');
echo $ template-> render(array('blah'=>'blah'));

在您的模板中,上下文变量 blah 现在只是一个包含'blah'的字符串。

In vanilla PHP,当你使用像这样的变量函数(尝试像函数一样使用字符串变量)时,它(或多或少)执行该函数的查找,然后调用它。你是不是传递函数,只是它的名字。
$ b

问题是,你不可能传递一个函数到一个模板中,因为PHP的唯一机制是通过名称字符串,并且一旦在模板中,该名称就不再是函数名称,而只是一个字符串。



有点长啰嗦,但我希望有帮助!



如果您需要更多文档,官方文档是这里


Currently I place my function in a class and pass an instance of this class into template and call my required function as a class method.

{{ unneededclass.blah() }}

I need to do like below

{{ blah() }}

Is it possible?

解决方案

Update 5/14/2015

Commenters point out that I'm mostly wrong. If you really need a function, and not a filter or macro, you can do it as suggested in the Twig docs:

$twig = new Twig_Environment($loader);
$function = new Twig_SimpleFunction('blah', function () {
   // ...
});
$twig->addFunction($function);

And use like

{{ blah() }}


In short, no, this is not possible.

However, hope is not lost!

Filters

If this function blah() of yours is meant to modify an existing variable, then it is a filter.

An example:

//in your PHP
function format_date($date_string,$format_string) {
    return date($format_string,strtotime($date_string));
}
$twig_env->addFilter('format_date',new Twig_Filter_Function('format_date'));

{# in your template #}
{{ some_date|format_date('n/j/Y') }}

(The first argument is the variable you are filtering, the second is supplied by normal means)

Macros

If, as you have indicated above, your function simply outputs HTML, then it is a good candidate for a macro.

An example:

{# in your template #}
{% macro say_hello() %}
<p>Oh! Hello, world!</p>
{% endmacro %}

{# ... later on ... #}
{{ _self.say_hello() }}

Or with parameters:

{% macro input(name,value,type) %}
<input type="{{ type|default('text') }}" name="{{ name }}" value="{{ value }}">
{% endmacro %}

{{ _self.input('phone_number','867-5309') }}
{{ _self.input('subscribe','yes','checkbox') }}

Why?

The thing to remember is that Twig templates represent a view, in terms of MVC. This means they are isolated in terms of their environment, and can only represent the context you pass them via the data array you pass in the $template->render() method.

This is a good thing, as it decouples your presentation from your logic and data. If you can arbitrarily call functions, then you suddenly increase that coupling, which is a bad thing.

The other reason for this is the way PHP handles callbacks. Think about how you would have to pass that function into your template... Probably something like this:

function blah() {
    return "<p>Oh! Hello, world!</p>";
}

$template = $twig_env->loadTemplate('template.html');
echo $template->render(array('blah'=>'blah'));

In your template, the context variable blah is now just a string containing 'blah'.

In vanilla PHP, when you use variable functions like this (try to use a string variable like a function), it (more or less) performs a lookup for that function, then calls it. You are not passing the function, just it's name.

The thing is, you cannot possibly pass a function into a template, because PHP's only mechanism for doing this is by name-string, and once inside a template, that name is no longer a function name and just a string.

A little bit long winded, but I hope that helps!

If you want more documentation, the official docs are here.

这篇关于树枝 - 将功能传递到模板中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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