Answers.Body: <p>You are mixing Craft 3 code with Craft 2. There is no native way to access the session in Craft 2 via Twig so you need to inject the functions with your plugin</p>
  
  <p>See a working example <a href="https://craftcms.stackexchange.com/a/16746/5557">here</a></p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-10-09T19:55:08.447Z
Answers.Text: Thanks- I added a bit of clarification to my question, above.  Would a session be the only way to get error messages to the non-logged-in users?
Answers.UserDisplayName: buck1112
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-10-09T19:56:26.800Z
Answers.Text: Why don't you just work with a proper model as it's intended? `$model->addError('name', 'username not found')` and in your template `user.getErrors()`
Answers.UserDisplayName: Robin Schambach
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-10-08T22:09:06.947Z
Answers.LastActivityDate: 2018-10-08T22:09:06.947Z
Answers.UserDisplayName: Robin Schambach
Answers.Body: <p>Craft 2 doesn't have a <code>craft.app</code> variable you can use like Craft 3 does, but there is a <a href="https://docs.craftcms.com/v2/templating/craft.session.html#getflash-key-defaultvalue-delete" rel="nofollow noreferrer"><code>craft.session.getFlash()</code></a> exposed to the templates in Craft 2 that you can use.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-10-10T23:57:18.653Z
Answers.LastActivityDate: 2018-10-10T23:57:18.653Z
Answers.UserDisplayName: Brad Bell
Body: <p>I'm working at getting error messages sent from my plugin to my template (on the front end), when there is no session (no logged in user). 
  The plugin is for LDAP authentication.</p>
  
  <p>Edit: For clarification, this plugin (and the error messages) will be for as-of-yet un-logged-in users.  Would a session structure be the only way to get the messages to those end users?</p>
  
  <p>I've tried this (below), but I get the error message (below):</p>
  
  <p>code in the plugin controller:   <code>craft()-&gt;userSession-&gt;setFlash('msg_template',"Credentials were not accepted.")</code></p>
  
  <p>code in the template:</p>
  
  <pre><code>{% set message = craft.app.session.getFlash('msg_template') %} 
  
  {% if message|length &gt; 0 %}
  {{message}} 
  {% endif %}
  </code></pre>
  
  <p>error message:</p>
  
  <pre><code>Neither the property "session" nor one of the methods "session()", "getsession()"/"issession()" or "__call()" exist and have public access in class "Craft\AppVariable
  </code></pre>
  
Tags: <plugin-development><templating><error-message><craft2>
Title: Sending Error Message to Template With No Logged In User - Craft 2
ThreadText: # Sending Error Message to Template With No Logged In User - Craft 2
  
  (Asked by: username_0 on 2018-10-08)
  
  I'm working at getting error messages sent from my plugin to my template (on the front end), when there is no session (no logged in user).
  The plugin is for LDAP authentication.
  
  Edit: For clarification, this plugin (and the error messages) will be for as-of-yet un-logged-in users. Would a session structure be the only way to get the messages to those end users?
  
  I've tried this (below), but I get the error message (below):
  
  code in the plugin controller: `craft()->userSession->setFlash('msg_template',"Credentials were not accepted.")`
  
  code in the template:
  
  ```
  {% set message = craft.app.session.getFlash('msg_template') %} 
  
  {% if message|length > 0 %}
  {{message}} 
  {% endif %}
  
  ```
  
  error message:
  
  ```
  Neither the property "session" nor one of the methods "session()", "getsession()"/"issession()" or "__call()" exist and have public access in class "Craft\AppVariable
  
  ```
  
  **Tags:** plugin-development, templating, error-message, craft2
  
  
  ---
  
  ## 2 answers
  
  ### Answer by username_1 on 2018-10-08. Score: 1
  
  You are mixing Craft 3 code with Craft 2. There is no native way to access the session in Craft 2 via Twig so you need to inject the functions with your plugin
  
  See a working example [here](https://craftcms.stackexchange.com/a/16746/5557)
  
  > **Comments:**
  > **username_0**: Thanks- I added a bit of clarification to my question, above.  Would a session be the only way to get error messages to the non-logged-in users?  
  > **username_1**: Why don't you just work with a proper model as it's intended? `$model->addError('name', 'username not found')` and in your template `user.getErrors()`  
  
  ---
  
  ### Answer by username_2 on 2018-10-10. Score: 0
  
  Craft 2 doesn't have a `craft.app` variable you can use like Craft 3 does, but there is a [`craft.session.getFlash()`](https://docs.craftcms.com/v2/templating/craft.session.html#getflash-key-defaultvalue-delete) exposed to the templates in Craft 2 that you can use.
  
  
  ---
  
  