var*_*ius 4 html php if-statement twig
我在树枝上看到了自己的看法
我有一个具有或没有键值的数组我需要如何检查它是否存在?
例如{{ weather.wind.deg }},对于当前时刻它可能没有wind deg所以weather.wind数组将不包含一个元素,如果它有或没有它如何检查它?也许我应该在我过去之前做到这一点?在某处?
$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())
);
});
Run Code Online (Sandbox Code Playgroud)
在你的树枝模板中你可以做到:
{% if weather.wind.deg is defined %}
make your things
{% endif %}
Run Code Online (Sandbox Code Playgroud)