Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Twig extend based on isxmlhttprequest

Tags:

twig

silex

I'm using Twig with Silex and are having some trouble with extend within an if statement. Basically the page is being extended even tho, the request is not done by AJAX.

{% if app.request.isXmlHttpRequest == false %}
 {% extends 'layout.html.twig' %}
{% endif %}

However this works. The button is only being showed when the request is AJAX based.

{% if app.request.isXmlHttpRequest %}
 <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
{% endif %}

Also possibly worth mentioning my code is enclosed in an {% block content %} tag.

Does anyone have experience with this issue? (Being able to extend only if isXmlHttpRequest is false)

Thanks in advance

like image 900
Kristian Avatar asked Dec 10 '25 14:12

Kristian


1 Answers

Try this one:

{% extends app.request.isxmlhttprequest ? "" : "layout.html.twig" %}

Behind the courtains extends is a token parsed in a way that no conditions can be placed around it, the outcome will be what you got: your template is always extended.

I'm not sure how it'll work with the empty string, you may need to create a "layout-ajax.html.twig" file with no content but the block you want to override.

like image 111
Maerlyn Avatar answered Dec 14 '25 11:12

Maerlyn



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!