0

Is it possible to write an if statement based on a javascript variable with TWIG?

My first approach was this:

{% if info_message is defined %}
    {% include 'includes/alerts/block.html.twig' with {'colorclass': 'danger', 'message': info_message} %}
{% endif %}

I want to create an alert based on a javascript variable, so is it possible to fill the info_message with javascript and than render it with the message as variable?

I am using alerts like alerts.block.html.twig:

<div class="alert alert-{{ colorclass }}">
<button type="button" class="close" data-dismiss="alert">
    <span aria-hidden="true">×</span>
    <span class="sr-only">Close</span>
</button>
{% if raw is defined and raw %}
    {{ message|raw }}
{% else %}
    {{ message }}
{% endif %}

Thomas Richter
  • 833
  • 8
  • 20
Sireini
  • 4,142
  • 12
  • 52
  • 91
  • You can't use `javascript` to alter `twig` output. Consider creating a `javascript` variable with your `twig` variable to output the message. Otherwise resort to `ajax` – DarkBee Sep 26 '18 at 11:46
  • @DarkBee so basically what you mean is store the twig info_message in a js var? – Sireini Sep 26 '18 at 12:07
  • Yes indeed. Have a look [here](https://stackoverflow.com/questions/37604063/twig-variable-in-extern-js-file/37606638) – DarkBee Sep 26 '18 at 12:10

0 Answers0