MorganFreeFarm MorganFreeFarm - 19 days ago 68
Twig Question

Twig error when try to format {$

Try to format that code in twig format:

<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>


So i tried with that format:

<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>


Error:


Uncaught exception 'Twig_Error_Syntax' with message 'A hash key must
be followed by a colon (:). Unexpected token "punctuation" of value
"." ("punctuation" expected with value ":")

Answer Source

There is no such thing as isset in twig. You also need to access the special variable _context if you want use dynamic variables in twig

Some possible solutions,

<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