twig 判断 string数组非空判断在不在数组中

所有工具列表php - Twig forgets array-keys - Stack Overflow
Join the Stack Overflow Community
Stack Overflow is a community of 6.9 million programmers, just like you, helping each other.
J it only takes a minute:
I have a weird problem with twig in Symfony2. I am using the following array:
[days] =& Array
[1] =& Array
[money] =& 9
[2] =& Array
[money] =& 21
[3] =& Array
[money] =& 38
[4] =& Array
[money] =& 6
[18] =& Array
[money] =& 6
[19] =& Array
[money] =& 3
[31] =& Array
[money] =& 11
to test this I used the following code
{% for key in days %}
{{ key }}&br&
{% endfor %}
but the output shows the following
but it should look like this
Looks like twig creates a new array with new indexes. Is there a way to get the right index from array?
With var_dump($days) in php I can see the right index, so the "problem" is related to twig.
124k14177214
also you can try this :
{% for key,value in users %}
{% endfor %}
or maybe look into the "loop" object defined in the for loop
Maybe this
By default, a loop iterates over the values of the sequence. You can iterate on keys by using the keys filter:
&h1&Members&/h1&
{% for key in users|keys %}
&li&{{ key }}&/li&
{% endfor %}
It's not related on twig, it's a known gap in doctrine-mongodb using embeddedDocuments. Doctrine can't handle key-Values from embeddedDocuments, it will reorder the keys beginning from 0 and will so ignore the right key-values. Thank you anyway for your help :).
Your Answer
Sign up or
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Post as a guest
By posting your answer, you agree to the
Not the answer you're looking for?
Browse other questions tagged
rev .25564
Stack Overflow works best with JavaScript enabledObject or array in twig file Symfony 2 [在Symfony枝文件2对象或数组] - 问题-字节技术
Object or array in twig file Symfony 2
在Symfony枝文件2对象或数组
问题 (Question)
i edited the post
where i set $appartement:
$appartement = new Apartement ();
$em = $this-&getDoctrine()-&getManager();
$appartement = $em-&getRepository('LeymaxDashboardBundle:Apartement')-&find(1);
well i think that i have a syntax error that what i have in the controller that send s params to twig file :
return $this-&render('LeymaxDashboardBundle:Default:client.html.twig', array('apart' =& $appartement,
'user' =& $connected_user-&getId(),
in the twig file i do the following :
{{ path('leymax_changemodules_showModal',{'iduser': user,'profil':'client','apart':apart}) }}
the generated route is like so :
leymax_changemodules_showModal:
/changemodulesModal/{iduser}/{profil}/{apart}
defaults: { _controller: LeymaxDashboardBundle:Default:changeModulesModal }
the error message :
An exception has been thrown during the rendering of a template ("Warning: preg_match() expects parameter 2 to be string, object given
what's wrong i'm doing ?
我编辑了帖子我将此美元:$appartement = new Apartement ();
$em = $this-&getDoctrine()-&getManager();
$appartement = $em-&getRepository('LeymaxDashboardBundle:Apartement')-&find(1);
我认为我有一个语法错误,我所的控制器发送年代params枝文件:return $this-&render('LeymaxDashboardBundle:Default:client.html.twig', array('apart' =& $appartement,
'user' =& $connected_user-&getId(),
嫩枝的文件我做以下:{{ path('leymax_changemodules_showModal',{'iduser': user,'profil':'client','apart':apart}) }}
生成的路线是这样的:leymax_changemodules_showModal:
/changemodulesModal/{iduser}/{profil}/{apart}
defaults: { _controller: LeymaxDashboardBundle:Default:changeModulesModal }
错误消息:An exception has been thrown during the rendering of a template ("Warning: preg_match() expects parameter 2 to be string, object given
怎么了我在做什么?
最佳答案 (Best Answer)
You can access a (public) property or method of an object in twig using the . syntax.
As long as this method's return value or property is of type string/integer - the following will work:
'route_name',
'parameter':
object.property,
'parameter2': object.someMethod
You can further add a _toString() method to the object's class that will return a i.e. the id ...
class Object {
public function __toString()
return $this-&
... and then pass the object directly:
'route_name',
您可以访问(公共)对象的属性或方法在树枝使用.语法。只要这个方法的返回值或属性的类型是字符串或整数——以下工作:{{
'route_name',
'parameter':
object.property,
'parameter2': object.someMethod
本文翻译自StackoverFlow,英语好的童鞋可直接参考原文:

我要回帖

更多关于 twig 数组 的文章

 

随机推荐