尝试格式化{$时出现Twig错误

时间:2017-11-15 15:41:18

标签: php twig

尝试以twig格式格式化该代码:

<textarea class="form-control" name="email_review_subject_<?php echo $language['language_id']; ?>" ><?php echo isset(${'email_review_subject_' . $language['language_id']}) ? ${'email_review_subject_' . $language['language_id']} : ''; ?></textarea>

所以我尝试了这种格式:

<textarea class="form-control" name="email_review_subject_{{ language['language_id'] }}" >{{ isset({'email_review_subject_' . language.language_id}) ? {'email_review_subject_' . language.language_id} : '' }}</textarea>

错误:

  

未捕获的异常&#39; Twig_Error_Syntax&#39;使用消息&#39;哈希密钥必须   后跟一个冒号(:)。意外的令牌&#34;标点符号&#34;有价值的   &#34;&#34; (&#34;标点符号&#34;期望价值&#34;:&#34;)

2 个答案:

答案 0 :(得分:3)

我很确定你不能使用.来连接Twig中的字符串。试试~,然后查看:How to concatenate strings in twig

答案 1 :(得分:3)

isset中没有twig这样的内容。如果要在树枝中使用动态变量

,还需要访问特殊变量_context

一些可能的解决方案,

<textarea class="form-control" name="email_review_subject_{{ language['language_id'] }}" >{{ attribute(_context, 'email_review_subject_'~language.language_id)|default('') }}</textarea>

<textarea class="form-control" name="email_review_subject_{{ language['language_id'] }}" >{{ attribute(_context, 'email_review_subject_'~language.language_id) is defined ? attribute(_context, 'email_review_subject_'~language.language_id) : '' }}</textarea>

fiddle