conditional does not run first in a file that is included using the extends feature for twig 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" %}
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 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.