Question: How does one set a flash message in the templates? I can't find any documentation about this, only for plugins or getting the message. 

My use-case is, I am checking if a user is allowed to access a certain page and if they cannot I want to set a flash message, then redirect them to another page. I prefer this than to keep them on the same page and is if else logic to show the message or the rest of the page. 

Thanks

Answer: 
You can set [flash messages](https://www.yiiframework.com/doc/guide/2.0/en/runtime-sessions-cookies#flash-data) using [yii\web\Session::setFlash()](https://www.yiiframework.com/doc/api/2.0/yii-web-session#setFlash()-detail).

The `session` component is available to templates via `craft.app.session`.

```
{% do craft.app.session.setFlash('error', 'You’re not allowed to go there.') %}

```

For convenience, Craft’s [SessionBehavior](https://docs.craftcms.com/api/v3/craft-behaviors-sessionbehavior.html) class adds shortcut [setError()](https://docs.craftcms.com/api/v3/craft-behaviors-sessionbehavior.html#method-seterror) and [setNotice()](https://docs.craftcms.com/api/v3/craft-behaviors-sessionbehavior.html#method-setnotice) methods to the `session` component as well.

```
{% do craft.app.session.setError('You’re not allowed to go there.') %}

```

You can place that right before your `{% redirect %}` tag.
**Edit: Brandon replied as I was writing this. Feel free to remove :)**

You can do that using this in your conditional, just above your redirect tag

```
{% do craft.app.session.setFlash('error', 'no no no') %}
{% redirect "tosomewhere" %}

```

Then in the template you are redirecting to

```
{% set message = craft.app.session.getFlash('error', null, true) %}
{% if message %}
    {{ message }}
{% endif %}

```