Symfony2-在树枝中将字符串转换为Int [英] Symfony2 - Cast a String to Int in twig
问题描述
我想知道是否可以在细枝中将String转换为Int。我尝试查看用户是否有足够的信用来购买币。为此,我使用模板中的渲染来计算功劳金额(因为我需要模板中的值,但我没有找到更好的方法来做...):
I would like to know if it was possible to cast a String to an Int in twig. I try to see if a user has enough credit or not to buy a cours. For that, I calculate the amount of credit with a render in the template (because I need the value in the template, and I didn't found a better way to do it...) like this :
{% set creditUser %}
{{render(controller('L3L2EntraideBundle:Credits:sommeCredits'))}}
{% endset %}
但是当我尝试比较creditUser时:
But when I try to compare creditUser :
{% if creditUser < c.idCompetenceCours.prix %}disabled="false"{% endif %}
Symfony返回我一个美丽的错误:在第21行的L3L2UserBundle:Profile:modal_prendre_rdv.html.twig中的模板渲染过程中引发了异常(注意:Twig_Markup类的对象无法转换为int)。
Symfony return me a beautiful error : An exception has been thrown during the rendering of a template ("Notice: Object of class Twig_Markup could not be converted to int") in L3L2UserBundle:Profile:modal_prendre_rdv.html.twig at line 21.
有什么想法吗?预先感谢您对Stackoverflow的第一个问题,对不起我的英语。
Any idea ? Thank you in advance for my first question on Stackoverflow and sorry for my english.
推荐答案
这不是字符串,而是Twig_Markup
This is not string but Twig_Markup
{% if creditUser.__toString < c.idCompetenceCours.prix %}
但这不是一个好方法,您应该从object /变量不是来自呈现的模板
but this is not good approach you should get this value from object/variable not from rendered template
这篇关于Symfony2-在树枝中将字符串转换为Int的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!