How do you set a flash message in the template in Craft 3? 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
You can set flash messages using yii\web\Session::setFlash().

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 class adds shortcut setError() and 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.