How to display a flash message in a template 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()-&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();


How can I display the notice or error in my template?
You can use the setFlash function in the userSession Service like this:

In your Controller:

craft()-&gt;userSession-&gt;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-&gt;session-&gt;setFlash('yourVariable', "Something happened yo!");


In template:

{% set message = craft.app.session.getFlash('yourVariable') %}
{% if message|length &gt; 0 %}
    {{message}}
{% endif %}