Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

425
Views
Es idéntico (===) en Twig

Aquí está el código PHP:

 if ($var===0) {do something}

"Hace algo" solo cuando $var es realmente 0 (y si $var no está configurado, no funciona, por lo que todo está bien).

Sin embargo, Twig no admite el operador ===, y si escribo:

 {% if var==0 %}do something{% endif %}

"hace algo" todo el tiempo (incluso cuando $var no está configurado). Para solucionarlo, escribí un código de este tipo:

 {% if var matches 0 %}do something{% endif %}

¿Es una forma correcta de hacer una comparación === en Twig, o hice algo mal aquí? Si está mal, ¿cómo se debe arreglar?

about 4 years ago · Santiago Trujillo
2 answers
Answer question

0

Debe usar same as en Twig para comparaciones === :

 {% set var1=0 %} {% set var2='0' %} {% if var1 is same as( 0 ) %} var1 is 0. {% else %} var1 is not zero. {% endif %} {% if var2 is same as( 0 ) %} var2 is 0. {% else %} var2 is not 0. {% endif %} {% if var2 is same as( '0' ) %} var2 is '0'. {% else %} var2 is not '0'. {% endif %}

Aquí hay un twigfiddle que lo muestra en funcionamiento:

https://twigfiddle.com/k09myb

Aquí está la documentación para same as también indica que es equivalente a === . ¡Espero que te ayude!

about 4 years ago · Santiago Trujillo Report

0

Twig no tiene === pero tiene lo same as su lugar. Ver: https://twig.sensiolabs.org/doc/2.x/tests/sameas.html

Así que podrías escribir:

 {% if var is same as(0) %}do something{% endif %}

Eventualmente, puede usar is defined para verificar si la variable está configurada.

about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!