8

Twig:

{% set var = 'apple' %}

{% block A %}
    {{ var }} {# This prints apple #}
{% endblock %}

{% block B %}
    {% set var = 'banana' %}
    {{ var }} {# This prints banana #}
{% endblock %}

{% block C %}
    {{ var }} {# This prints apple, but I want it to print banana #}
{% endblock %}

Is there any way to get the changed value from block B in block C?

5
  • e.g. In PHP i can do: $var= 0;if(true){$var =2;}echo $var; # value == 2 not 0 Commented Mar 17, 2017 at 10:29
  • Twig blocks have their own scope. So if you want block C to print banana, you have to override var outside block B. Commented Mar 17, 2017 at 11:15
  • Also, have a look at the with keyword in twig: twig.sensiolabs.org/doc/2.x/tags/with.html Commented Mar 17, 2017 at 11:24
  • @mickdev I want the changed value from Block B in Block C i.e. banana. The actual issue I have is I have a {body block} in which I set the value and I want access to that new value in my {javascript block}. Commented Mar 18, 2017 at 10:58
  • @Jan I tried it earlier, But I suspect I might have missed something. Let me check and get back to you. Commented Mar 18, 2017 at 10:59

2 Answers 2

4

You can hack it and set the value in your $_SESSION variable and access it in the required block:

{% set var = 'apple' %}

{% block A %}
    {{ var }} {# This prints apple #}
{% endblock %}

{% block B %}
    {% set var = 'banana' %}
    {{ var }} {# This prints banana #}
    {{ app.session.set('var', var) }}
{% endblock %}

{% block C  %}
    {{ var }} {# This prints apple #}
    {% set var = app.session.get('var') %}
    {{ var }} {# This prints banana #}
{% endblock %}

Here is the twigfiddle to show you.

Or In the comment section you mentioned that you want to access a variable in {% block javascripts %} which has been set in {% block body %}. You can hack it and move your js code in your body block. But I prefer to keep my js code in one place.

{% set var = 'apple' %}

{% block A %}
    {{ var }} {# This prints apple #}
{% endblock %}

{% set var = 'banana' %}
{% block B %}
    {{ var }} {# This prints banana #}
`<script>{{ var }}</script>` {# Bring your js code in the required block#}
{% endblock %}
Sign up to request clarification or add additional context in comments.

2 Comments

This could solve the problem, but, please! stop putting everything into sessions and treating them as if the session were a global var. I consider this a bad practice and should be avoided.
A bit hacky and dirty. But for self-tools not intended to be published, it may do the trick. In my case I had a loop and at the end of the loop a footnote that only was displayed if within the loop I found X condition. As the loop grew, I moved the content of the loop out in a block. No longer I could update the variable where to track if it was found. Thanks for the trick.
1

You have to do the set outside of the block like so:

{% set var = 'apple' %}

{% block A %}
    {{ var }} {# This prints apple #}
{% endblock %}

{% set var = 'banana' %}
{% block B %}
    {{ var }} {# This prints banana #}
{% endblock %}

{% block C %}
    {{ var }}
{% endblock %}

Here is the twigfiddle to show you.

1 Comment

I want the changed value from Block B in Block C i.e. banana. The actual issue I have is I have a {body block} in which I set the value and I want access to that new value in my {javascript block}.

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.