4

i have my view in twig

and i have an array which has or hasnt key value i need how to check it if its exist ?

example {{ weather.wind.deg }} and for current moment its possible there is no wind deg so the weather.wind array will not contain an element witch key deg how to check it if it has it or no ? maybe i should do it before i past this to my view ? somewhere here ?

$app->get('/', function () use ($app) {
    return $app['twig']->render('index.html.twig', array(
            'weather' => $app['weather_service']->get($app['location_service']->get()),
            'location' => $app['location_service']->get())
    );
});
vardius
  • 6,326
  • 8
  • 52
  • 97

1 Answers1

7

In you twig template you can do :

{% if weather.wind.deg is defined %}
        make your things
{% endif %}
BENARD Patrick
  • 30,363
  • 16
  • 99
  • 105
  • ok it works great! if you can tell me one more thing how to get the key and the value of key from array like this `[clouds] => Array ( [all] => 20 )` because key of array change with the weather it unknow for me, it can be "all" as here for all day it means or for example "3h" for 3 hours – vardius Sep 12 '13 at 18:21
  • also how is going to look else statment here ? – vardius Sep 12 '13 at 18:23
  • for the condition : {% if something %} code {% else %} code {% endif %} . for the rest : http://stackoverflow.com/questions/10299202/twig-for-loop-and-array-with-key – BENARD Patrick Sep 12 '13 at 18:26