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())
    );
});

1 Answer 1

7

In you twig template you can do :

{% if weather.wind.deg is defined %}
        make your things
{% endif %}
Sign up to request clarification or add additional context in comments.

3 Comments

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
also how is going to look else statment here ?
for the condition : {% if something %} code {% else %} code {% endif %} . for the rest : stackoverflow.com/questions/10299202/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.