1

I'm trying to only include some HTML based on an IF statement:

{% if "{{ app.request.attributes.get('var_name') }}" != "" %}
<meta id="var_name" content="{{ app.request.attributes.get('var_name') }}" />
{% endif %}

The above will work exactly as expected if the var_name variable holds data, but if it is empty or not defined it always returns the HTML like this:

<meta id="var_name" content="" />

...so basically it is registering the IF statement as true, even though there's nothing to show.

It makes no difference if the variable is defined or not.

Also, this:

{% if {{ app.request.attributes.get('var_name') }} != "" %}

...returns this error:

A hash key must be a quoted string, a number, a name, or an expression enclosed in parentheses (unexpected token "punctuation" of value "{" in...

How do I do it?

1 Answer 1

7

Do this:

{% if app.request.attributes.get('var_name') != "" %}

You don't need the {{ }} inside the {% %}

More info: Twig - IF

Sign up to request clarification or add additional context in comments.

1 Comment

Perfect. Simple when you know how! Thanks. I'll accept answer in 5 mins when it allows.

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.