lukmil lukmil - 20 days ago 9
Twig Question

symfony 3 twig template function if

this function

function test(){
var distance =calculateHaversineFormula({{lat}}, {{lng}},{{ geocode.latitude}}, {{ geocode.longitude }} );
{% if distance < 1000 %}
console.log(distance);
{% endif %}

}


function calculate.. returns d (distance), so I want to make constant for this.. then i want to filtrate to get all distances who has less than 1000, but I got error Variable "distance" does not exist in default\index.html.twig at line 41.

Without if

var distance =calculateHaversineFormula({{lat}}, {{lng}},{{ geocode.latitude}}, {{ geocode.longitude }} );
console.log(distance);


This works fine.. So what is wrong with if?

Answer

I would suggest you to rewrite your condition in JS, like so:

function test(){
  var distance =calculateHaversineFormula({{lat}}, {{lng}},{{geocode.latitude}}, {{ geocode.longitude }} );
  if distance < 1000 {
    console.log(distance);
  }
}
Comments