0

I'm trying to insert html with twig via jQuery but the twig is escaped as text. Is there a way to escape the twig when it is appended with jquery ?

var html = `
         {% if is_granted('ROLE_ADMIN') and app.user %}

        <div class="col-sm-1 col-md-1">
            <div class="form-group">
                <div class="dropup">
                        <a class="btn btn-primary btn-sm" href="{{ path('easyadmin')}}"><i class=" glyphicon glyphicon-dashboard"></i> Administrar sistema</a></li>
                    </ul>
                </div>
            </div>
        </div>

        <div class="col-md-1 col-md-offset-7">
            <div class="form-group">
                    <i id ="hide-footer" class="glyphicon glyphicon-remove white gi-2x"></i>
            </div>
        </div>
        {% endif %}`


$('#reshow-footer').append(html);

2 Answers 2

2

Do something like this

{% if is_granted('ROLE_ADMIN') and app.user %}
    <script>
        var html = 'your text or url="{{ path('rout_name') }}"';
        $('#reshow-footer').append(html);
    </script
{% endif %}
Sign up to request clarification or add additional context in comments.

2 Comments

nice idea, what about the {{ path('easyadmin')}} and other twig vars?
When you are in twig file, you can write every twig code wherever you want(of course between curly brackets or other signs)
1

Actually Twig is interpreted by server (like PHP). So you can't write twig in jQuery (because the page is already loaded).

So the solution is to encapsulate your jQuery code into your twig condition.

Comments

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.