php 树枝访问对象
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14413550/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Twig access object
提问by user1766080
i want to access the value of a object inside a twig template.
我想访问树枝模板中对象的值。
Normally i would get the return like that:
通常我会得到这样的回报:
echo $lang->get("test");
But how can i do the same in the template with twig?
但是我怎么能用树枝在模板中做同样的事情呢?
I tried so many methods but no one worked.
我尝试了很多方法,但没有一个奏效。
For example i tried:
例如我试过:
{{ attribute(lang, get, 'test') }}
with the result
结果
Catchable fatal error: Argument 3 passed to Twig_Node_Expression_GetAttr::__construct() must be an instance of Twig_Node_Expression_Array, instance of Twig_Node_Expression_Constant given
可捕获的致命错误:传递给 Twig_Node_Expression_GetAttr::__construct() 的参数 3 必须是 Twig_Node_Expression_Array 的实例,给定的 Twig_Node_Expression_Constant 实例
thanks
谢谢
回答by deceze
What you're trying to do is call a method on an object with parameters in a Twig template. I do not think this is supported, as it's probably viewed as a bad idea. Twig supports the notion of getters on an object though, which are called without parameters:
您要做的是使用 Twig 模板中的参数调用对象上的方法。我不认为这是支持的,因为它可能被视为一个坏主意。不过,Twig 支持对象上的 getter 的概念,它是不带参数调用的:
{{ lang.test }}
will try to invoke one of the following, in this order:
将尝试按以下顺序调用以下之一:
$lang->test$lang->test()$lang->getTest()$lang->isTest()
$lang->test$lang->test()$lang->getTest()$lang->isTest()
If the object implements any of these accessors and conventions, Twig will find it. Anything outside of this convention, like get('test'), is not part of the Twig philosophy. And it's not a widely used idiom in general, so you should probably stick to one of the above methods.
如果对象实现了这些访问器和约定中的任何一个,Twig 会找到它。这个约定之外的任何东西,比如get('test'),都不是 Twig 哲学的一部分。而且它通常不是一个广泛使用的习语,因此您可能应该坚持使用上述方法之一。
See http://twig.sensiolabs.org/doc/templates.html#variables.
请参阅http://twig.sensiolabs.org/doc/templates.html#variables。
You can implement __isset, __getor __callmagic methods to support one of these accessor methods.
您可以实现__isset,__get或者__call魔术方法,以支持这些访问方法之一。
回答by Daniel Israel
I know this is an old question, but after 3 hours of scouring the internet and finding no examples, I wanted to make sure it got documented.
我知道这是一个老问题,但是在搜索互联网 3 个小时并没有找到任何示例之后,我想确保它被记录在案。
Going back to one of your original attempts:
回到你最初的尝试之一:
{{ attribute(lang, get, 'test') }}
I'm trying to do the same thing, and this should work according to the documentation. Unfortunately, there are no examples of using this. All I found was that the method name (get) had to be a string ('get'), so I changed that, but it still didn't work. What I ended up doing was this:
我正在尝试做同样的事情,这应该根据文档工作。不幸的是,没有使用它的例子。我发现方法名称 (get) 必须是字符串 ('get'),所以我更改了它,但它仍然不起作用。我最终做的是这样的:
{% set myText = lang.get('test') %}
{{ myText }}
This worked great, but it's a lot of code to write when I have to do this all over. So I made a simple template with both methods and examined the compiled output. The original was compiled to this:
这很有效,但是当我必须全部完成时,需要编写很多代码。所以我用这两种方法制作了一个简单的模板并检查了编译后的输出。原文是这样编译的:
echo twig_escape_filter($this->env, $this->getAttribute((isset($context["lang"]) ? $context["lang"] : null), "get", "test"), "html", null, true);
and the second (2 liner) to this:
和第二个(2 班轮):
$context["myText"] = $this->getAttribute((isset($context["lang"]) ? $context["lang"] : null), "get", array(0 => "test"), "method");
echo twig_escape_filter($this->env, (isset($context["myText"]) ? $context["myText"] : null), "html", null, true);
After examination, I realized the difference (check the 3rd parameters to getAttribute), the arguments parameter is an array! This is good information to know. I changed my original to this:
经过检查,我意识到了区别(检查getAttribute的第三个参数),arguments参数是一个数组!这是很好的信息。我把原来的改成这样:
{{ attribute(lang, 'get', ['test']) }}
and it's now working!
它现在开始工作了!
Hope this helps someone!
希望这可以帮助某人!

