LoginSignup
0
0

More than 5 years have passed since last update.

twig extendでif文は使えないので三項演算子を使う

Last updated at Posted at 2017-09-21

ログイン可否でレイアウトを分けたいが
if文の分岐では500エラーが出てしまう。
三項演算子を使うと上手くいった。

間違ったコード

sample.twig
{% if abc() %}
  {% extends 'twig/test1.twig' %}
{% else %}
  {% extends 'twig/test2.twig' %}
{% endif %}
(後略)

500エラー文
Twig_Error_Syntax: "An exception has been thrown during the compilation of a template ("Node "1" does not exist for Node "Twig_Node".") in "今いるURL"."[Previous] LogicException: "Node "1" does not exist for Node "Twig_Node"."

上手くいったコード

qiita.rb
{% extends abc() ? " twig/test1.twig " : " twig/test2.twig " %}

正常に表示される。

以下のURLを参考にif文ではなく三項演算子を使うとうまくいった

参考URL
https://github.com/symfony-japan/twig-docs-ja/blob/master/recipes.rst

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0