Symfony2 - Cast a String to Int in twig

蹲街弑〆低调 提交于 2019-12-08 13:27:15

问题


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 %}

But when I try to compare creditUser :

{% if creditUser < c.idCompetenceCours.prix %}disabled="false"{% endif %}

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.

Any idea ? Thank you in advance for my first question on Stackoverflow and sorry for my english.


回答1:


This is not string but Twig_Markup

{% if creditUser.__toString < c.idCompetenceCours.prix %}

but this is not good approach you should get this value from object/variable not from rendered template



来源:https://stackoverflow.com/questions/29400482/symfony2-cast-a-string-to-int-in-twig

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!