Answers.Body: <p>You can set <a href="https://www.yiiframework.com/doc/guide/2.0/en/runtime-sessions-cookies#flash-data" rel="noreferrer">flash messages</a> using <a href="https://www.yiiframework.com/doc/api/2.0/yii-web-session#setFlash()-detail" rel="noreferrer">yii\web\Session::setFlash()</a>.</p>
  
  <p>The <code>session</code> component is available to templates via <code>craft.app.session</code>.</p>
  
  
  
  <pre><code>{% do craft.app.session.setFlash('error', 'You’re not allowed to go there.') %}
  </code></pre>
  
  <p>For convenience, Craft’s <a href="https://docs.craftcms.com/api/v3/craft-behaviors-sessionbehavior.html" rel="noreferrer">SessionBehavior</a> class adds shortcut <a href="https://docs.craftcms.com/api/v3/craft-behaviors-sessionbehavior.html#method-seterror" rel="noreferrer">setError()</a> and <a href="https://docs.craftcms.com/api/v3/craft-behaviors-sessionbehavior.html#method-setnotice" rel="noreferrer">setNotice()</a> methods to the <code>session</code> component as well.</p>
  
  <pre><code>{% do craft.app.session.setError('You’re not allowed to go there.') %}
  </code></pre>
  
  <p>You can place that right before your <code>{% redirect %}</code> tag.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-01-15T00:16:38.917Z
Answers.LastActivityDate: 2019-01-15T00:16:38.917Z
Answers.UserDisplayName: Brandon Kelly
Answers.Body: <p><strong>Edit: Brandon replied as I was writing this. Feel free to remove :)</strong></p>
  
  <p>You can do that using this in your conditional, just above your redirect tag</p>
  
  <pre><code>{% do craft.app.session.setFlash('error', 'no no no') %}
  {% redirect "tosomewhere" %}
  </code></pre>
  
  <p>Then in the template you are redirecting to</p>
  
  <pre><code>{% set message = craft.app.session.getFlash('error', null, true) %}
  {% if message %}
      {{ message }}
  {% endif %}
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-01-15T00:23:34.023Z
Answers.LastActivityDate: 2019-01-15T00:29:03.663Z
Answers.LastEditDate: 2019-01-15T00:29:03.663Z
Answers.UserDisplayName: Oli
Body: <p>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. </p>
  
  <p>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. </p>
  
  <p>Thanks</p>
  
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2019-01-14T09:09:09.667Z
Comments.Text: AFAIK it is not possible to set a flash message from the template, however this might be a nice improvement for the `{% redirect %}` tag.
Comments.UserDisplayName: Oliver Stark
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2019-01-15T23:52:01.650Z
Comments.Text: Now (Craft 3.1) you can set a notice/error message with the `{% redirect %}` tag: https://docs.craftcms.com/v3/dev/tags/redirect.html#flash-messages
Comments.UserDisplayName: Oliver Stark
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2019-01-16T10:51:25.940Z
Comments.Text: redirect with flash notice is great. I had not found that documentation because its within the redirect documentation, whereas I had been searching for flash and did not find this page! Thanks.
Comments.UserDisplayName: Laurence Cope
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2019-01-17T08:59:53.523Z
Comments.Text: Laurence, it was added to the docs yesterday, along with the Craft 3.1 release ;-) https://github.com/craftcms/cms/pull/3625
Comments.UserDisplayName: Oliver Stark
Tags: <craft3>
Title: How do you set a flash message in the template in Craft 3?
ThreadText: # How do you set a flash message in the template in Craft 3?
  
  (Asked by: username_0 on 2019-01-12)
  
  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
  
  **Tags:** craft3
  
  > **Comments:**
  > **username_1**: AFAIK it is not possible to set a flash message from the template, however this might be a nice improvement for the `{% redirect %}` tag.  
  > **username_1**: Now (Craft 3.1) you can set a notice/error message with the `{% redirect %}` tag: https://docs.craftcms.com/v3/dev/tags/redirect.html#flash-messages  
  > **username_0**: redirect with flash notice is great. I had not found that documentation because its within the redirect documentation, whereas I had been searching for flash and did not find this page! Thanks.  
  > **username_1**: username_0, it was added to the docs yesterday, along with the Craft 3.1 release ;-) https://github.com/craftcms/cms/pull/3625  
  
  ---
  
  ## 2 answers
  
  ### Answer by username_2 on 2019-01-15. Score: 13
  
  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.
  
  
  ---
  
  ### Answer by username_3 on 2019-01-15. Score: 10
  
  **Edit: username_2 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 %}
  
  ```
  
  
  ---
  
  