Symfony2 - Cast a String to Int in twig

2019-06-08 17:09发布

问题:

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