<p>The issue here is that extends isn't the same thing, neither conceptually or technically, as an include. When you <code>extend</code> a template with a layout template, the template is "decorated" by the layout. So, what's happening is that the code in the layout is parsed after all of the code in the template. </p>

<p>Check out <a href="https://straightupcraft.com/articles/twig-processing-order" rel="nofollow">this article on straightupcraft</a> explains the order of which things happen in Twig. In short:</p>

<p>1) All code in your <strong>template</strong>, except code inside blocks, gets processed.</p>

<p>2) All code in your <strong>layout</strong>, except code inside blocks, gets processed.</p>

<p>3) Code inside <strong>blocks</strong> gets processed.</p>

<p>So, what the solution to your problem is, depends on your setup. But, if your code that accesses currentUser is inside the block in your page template, the redirect should fire first. If you need to access currentUser outside of the blocks in your page template, you have to do the redirect there.</p>

CC BY-SA 3.0
2016-10-05T20:59:59.490Z
2016-10-05T21:17:55.497Z
2016-10-05T21:17:55.497Z
André Elvan
<p>I have this conditional included in a layout file I use within others, using the extends feature in twig.</p>

<p>This is at the very top of _layouts/application.twig</p>

<pre><code>{% if not currentUser %}
    {% redirect "login" %}
{% endif %}
</code></pre>

<p>I have this at the very top of account/index.twig</p>

<pre><code>{% extends "_layouts/application" %}
{% set name = currentUser.getFullName() %}
</code></pre>

<p>If the user is not logged in, I expect it to redirect the user to the login page, as it contains the if not currentUser conditional at the very beginning of the extends file: _layouts/application.twig</p>

<p>I then copied and pasted the if not currentUser code and placed it above the extends call within the account/index.twig file (like seen below):</p>

<pre><code>{% if not currentUser %}
    {% redirect "login" %}
{% endif %}

{% extends "_layouts/application" %}
</code></pre>

<p>and it works as I expect it to.</p>

<p>Why does it not work with the if conditional being included in _layouts/application.twig file that I include by using {% extends "_layouts/application" %}</p>

CC BY-SA 3.0
2016-10-05T17:41:31.127Z
Tested this quickly, and it seems to work for me both when I have the conditional in the layout and in the template. Just guessing here but, you wouldn't maybe be extending the login template with the same layout? That would probably generate an indefinite loop. Not sure how Craft/Twig would handle that.
André Elvan
CC BY-SA 3.0
2016-10-05T20:18:56.883Z
no, its not a case of it being used within the login page. @AndréElvan in the file you called the extend file, can you put below the extend call this: {{ currentUser.id }}, make sure you're logged out before accessing that page.
Brad
CC BY-SA 3.0
2016-10-05T20:45:58.457Z
Ah yeah, that'll create an twig error. Is that the problem?
André Elvan
CC BY-SA 3.0
2016-10-05T20:48:33.917Z
@AndréElvan yes, I am trying to get it work within the file I am calling in the extends, instead of copying and pasting the if currentUser conditional into hundreds of templates.
Brad
CC BY-SA 3.0
2016-10-05T21:03:02.823Z
Might be that my answer isn't correct. What is the exact twig error you're getting?
André Elvan
CC BY-SA 3.0
2016-10-05T21:20:04.637Z
Elaborated a bit on my answer. The actual error message would be useful though, since what you write ("can you put below the extend call this: {{ currentUser.id }}") would result in the error message "A template that extends another one cannot have a body.", which is completely unrelated to your question. :)
André Elvan
<templating><conditional><session><current-user>
conditional does not run first in a file that is included using the extends feature for twig
# conditional does not run first in a file that is included using the extends feature for twig

(Asked by: username_0 on 2016-10-05)

I have this conditional included in a layout file I use within others, using the extends feature in twig.
This is at the very top of _layouts/application.twig
{% if not currentUser %}
    {% redirect "login" %}
{% endif %}

I have this at the very top of account/index.twig
{% extends "_layouts/application" %}
{% set name = currentUser.getFullName() %}

If the user is not logged in, I expect it to redirect the user to the login page, as it contains the if not currentUser conditional at the very beginning of the extends file: _layouts/application.twig
I then copied and pasted the if not currentUser code and placed it above the extends call within the account/index.twig file (like seen below):
{% if not currentUser %}
    {% redirect "login" %}
{% endif %}

{% extends "_layouts/application" %}

and it works as I expect it to.
Why does it not work with the if conditional being included in _layouts/application.twig file that I include by using {% extends "_layouts/application" %}

**Tags:** templating, conditional, session, current-user
> **Comments:**
> **username_1**: Tested this quickly, and it seems to work for me both when I have the conditional in the layout and in the template. Just guessing here but, you wouldn't maybe be extending the login template with the same layout? That would probably generate an indefinite loop. Not sure how Craft/Twig would handle that.  
> **username_0**: no, its not a case of it being used within the login page. @AndréElvan in the file you called the extend file, can you put below the extend call this: {{ currentUser.id }}, make sure you're logged out before accessing that page.  
> **username_1**: Ah yeah, that'll create an twig error. Is that the problem?  
> **username_0**: @AndréElvan yes, I am trying to get it work within the file I am calling in the extends, instead of copying and pasting the if currentUser conditional into hundreds of templates.  
> **username_1**: Might be that my answer isn't correct. What is the exact twig error you're getting?  
> **username_1**: Elaborated a bit on my answer. The actual error message would be useful though, since what you write ("can you put below the extend call this: {{ currentUser.id }}") would result in the error message "A template that extends another one cannot have a body.", which is completely unrelated to your question. :)  

---

## 1 answers

### Answer by username_1 on 2016-10-05. Score: 3

The issue here is that extends isn't the same thing, neither conceptually or technically, as an include. When you `extend` a template with a layout template, the template is "decorated" by the layout. So, what's happening is that the code in the layout is parsed after all of the code in the template.

Check out [this article on straightupcraft](https://straightupcraft.com/articles/twig-processing-order) explains the order of which things happen in Twig. In short:

1) All code in your **template**, except code inside blocks, gets processed.

2) All code in your **layout**, except code inside blocks, gets processed.

3) Code inside **blocks** gets processed.

So, what the solution to your problem is, depends on your setup. But, if your code that accesses currentUser is inside the block in your page template, the redirect should fire first. If you need to access currentUser outside of the blocks in your page template, you have to do the redirect there.


---

