Answers.Body: <p>You can use the <code>setFlash</code> function in the <code>userSession</code> Service like this:</p>
  
  <p>In your Controller:</p>
  
  <pre><code>craft()-&gt;userSession-&gt;setFlash('yourVariable', "Something happened yo!");
  </code></pre>
  
  <p>In your template (that the user gets redirected to):</p>
  
  <pre><code>{% set message = craft.session.getFlash('yourVariable') %}
  </code></pre>
  
  <p>After that just run an if-statement to see if the message is set or not.</p>
  
  <p>EDIT:
  In Craft 3 controller:</p>
  
  <pre><code>Craft::$app-&gt;session-&gt;setFlash('yourVariable', "Something happened yo!");
  </code></pre>
  
  <p>In template:</p>
  
  <pre><code>{% set message = craft.app.session.getFlash('yourVariable') %}
  {% if message|length &gt; 0 %}
      {{message}}
  {% endif %}
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-06-11T10:02:57.577Z
Answers.Text: You can still use `setNotice` and `setError` and display flashes with `getFlash('notice')` and `getFlash('error')` respectively
Answers.UserDisplayName: Yuri Salimovskiy
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2014-11-06T09:27:49.927Z
Answers.LastActivityDate: 2018-10-04T09:53:20.563Z
Answers.LastEditDate: 2018-10-04T09:53:20.563Z
Answers.UserDisplayName: naboovalley
Body: <p>I have a plugin that accepts a form post. I need to return a notice or error, plus the original data. Starting with the notice/error first, I have tried this:</p>
  
  <pre><code>if (craft()-&gt;broadbean_jobs-&gt;createJob())
  {
      craft()-&gt;userSession-&gt;setNotice(Craft::t('Job created.'));
  }
  else
  {
      craft()-&gt;userSession-&gt;setError(Craft::t('Unable to create job.'));
  }
  
  $this-&gt;redirectToPostedUrl();
  </code></pre>
  
  <p>How can I display the notice or error in my template?</p>
  
Tags: <plugin-development><templating><error-message>
Title: How to display a flash message in a template
ThreadText: # How to display a flash message in a template
  
  (Asked by: username_0 on 2014-11-05)
  
  I have a plugin that accepts a form post. I need to return a notice or error, plus the original data. Starting with the notice/error first, I have tried this:
  
  ```
  if (craft()->broadbean_jobs->createJob())
  {
      craft()->userSession->setNotice(Craft::t('Job created.'));
  }
  else
  {
      craft()->userSession->setError(Craft::t('Unable to create job.'));
  }
  
  $this->redirectToPostedUrl();
  
  ```
  
  How can I display the notice or error in my template?
  
  **Tags:** plugin-development, templating, error-message
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2014-11-06. Score: 16 | Accepted answer
  
  You can use the `setFlash` function in the `userSession` Service like this:
  
  In your Controller:
  
  ```
  craft()->userSession->setFlash('yourVariable', "Something happened yo!");
  
  ```
  
  In your template (that the user gets redirected to):
  
  ```
  {% set message = craft.session.getFlash('yourVariable') %}
  
  ```
  
  After that just run an if-statement to see if the message is set or not.
  
  EDIT:
  In Craft 3 controller:
  
  ```
  Craft::$app->session->setFlash('yourVariable', "Something happened yo!");
  
  ```
  
  In template:
  
  ```
  {% set message = craft.app.session.getFlash('yourVariable') %}
  {% if message|length > 0 %}
      {{message}}
  {% endif %}
  
  ```
  
  > **Comments:**
  > **username_2**: You can still use `setNotice` and `setError` and display flashes with `getFlash('notice')` and `getFlash('error')` respectively  
  
  ---
  
  