Answers.Body: <p>Not really a direct answer to your question, but you could also perhaps remove the additional controller routing business and move your conditional logic into a plugin variable (or service method called from your variable).</p>
  
  <pre><code>{% if craft.myPlugin.everythingsOk(entry) %}
      // everything's ok, render template
  {% endif %}
  </code></pre>
  
  <p>Your variable:</p>
  
  <pre><code>public function everythingsOk($entry)
  {
      // Do some conditional checks
  
      if (craft()-&gt;myPlugin-&gt;doChecks($userId, $entry))
      {
          return true;
      }
      return false;
  }
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-04-20T12:58:00.110Z
Answers.Text: Ah, that's a different way of thinking about it. I was primarily using the routing method as it enforces a check for a logged in user by default as only logged in users can access action controllers so I got an `{% if currentUser %}` check for free. It does make the error checking more complicated though, so your idea is probably better overall, thanks!
Answers.UserDisplayName: Steve Rowling
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-04-15T02:40:05.550Z
Answers.LastActivityDate: 2015-04-15T03:14:47.530Z
Answers.LastEditDate: 2015-04-15T03:14:47.530Z
Answers.UserDisplayName: Douglas McDonald
Body: <p>In a plugin I am using <code>registerSiteRoutes</code> to route URL requests to a controller to handle some user validation before rendering the appropriate template. For example:</p>
  
  <pre><code>public function registerSiteRoutes()
  {
      return array(
          'some-path-to/(?P&lt;template&gt;template-name)/(?P&lt;entryId&gt;\d+)' =&gt; array('action' =&gt; 'myPlugin/handleRequest'),
      );
  }
  </code></pre>
  
  <p>The <code>handleRequest</code> controller looks something like this:</p>
  
  <pre><code>public function actionHandleRequest(array $variables = array())
  {
      if (!craft()-&gt;request-&gt;isSiteRequest())
      {
          throw new HttpException(404);
      }
  
      // Do some conditional checks
  
      // If everything is OK, load requested template
      if (craft()-&gt;myPlugin-&gt;doChecks($userId, $entry))
      {
          $variables['entry'] = $entry;
          $this-&gt;renderTemplate('statement/_'.$variables['template'], $variables);
      }
  }
  </code></pre>
  
  <p>The rendered template includes a form that submits to another action controller in the plugin to handle creation of a number of entries based on the submitted values.</p>
  
  <p>How can I pass the error messages (if any) back to the submitted form (that is accessed via the site route set in the plugin)?</p>
  
  <p>I've tried removing the site route and accessing the template directly and this works. However, it also means that I need to add in a load of conditional logic to control access to the template in the template itself. I'd rather extract the logic into a plugin controller as I did before as it makes the templates a lot cleaner and separates out the logic.</p>
  
Tags: <plugin-development><controller>
Title: How do I pass existing template variables on to an action controller?
ThreadText: # How do I pass existing template variables on to an action controller?
  
  (Asked by: username_0 on 2015-04-14)
  
  In a plugin I am using `registerSiteRoutes` to route URL requests to a controller to handle some user validation before rendering the appropriate template. For example:
  
  ```
  public function registerSiteRoutes()
  {
      return array(
          'some-path-to/(?P<template>template-name)/(?P<entryId>\d+)' => array('action' => 'myPlugin/handleRequest'),
      );
  }
  
  ```
  
  The `handleRequest` controller looks something like this:
  
  ```
  public function actionHandleRequest(array $variables = array())
  {
      if (!craft()->request->isSiteRequest())
      {
          throw new HttpException(404);
      }
  
      // Do some conditional checks
  
      // If everything is OK, load requested template
      if (craft()->myPlugin->doChecks($userId, $entry))
      {
          $variables['entry'] = $entry;
          $this->renderTemplate('statement/_'.$variables['template'], $variables);
      }
  }
  
  ```
  
  The rendered template includes a form that submits to another action controller in the plugin to handle creation of a number of entries based on the submitted values.
  
  How can I pass the error messages (if any) back to the submitted form (that is accessed via the site route set in the plugin)?
  
  I've tried removing the site route and accessing the template directly and this works. However, it also means that I need to add in a load of conditional logic to control access to the template in the template itself. I'd rather extract the logic into a plugin controller as I did before as it makes the templates a lot cleaner and separates out the logic.
  
  **Tags:** plugin-development, controller
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2015-04-15. Score: 2 | Accepted answer
  
  Not really a direct answer to your question, but you could also perhaps remove the additional controller routing business and move your conditional logic into a plugin variable (or service method called from your variable).
  
  ```
  {% if craft.myPlugin.everythingsOk(entry) %}
      // everything's ok, render template
  {% endif %}
  
  ```
  
  Your variable:
  
  ```
  public function everythingsOk($entry)
  {
      // Do some conditional checks
  
      if (craft()->myPlugin->doChecks($userId, $entry))
      {
          return true;
      }
      return false;
  }
  
  ```
  
  > **Comments:**
  > **username_0**: Ah, that's a different way of thinking about it. I was primarily using the routing method as it enforces a check for a logged in user by default as only logged in users can access action controllers so I got an `{% if currentUser %}` check for free. It does make the error checking more complicated though, so your idea is probably better overall, thanks!  
  
  ---
  
  