Answers.Body: <p>I believe you are overwriting the <code>log</code> variable you are sending back to the template when you set log equal to the response of getPost.</p>
  
  <p>From what I can see of your code, when you return variables to your template via <code>setRouteVariables</code>, you should be providing the variables you define in that array to your template, <em>without</em> having to take a second step and retrieve them.  So, when your controller returns:</p>
  
  <pre><code>craft()-&gt;urlManager-&gt;setRouteVariables(array(
     'log' =&gt; $log
  ));
  </code></pre>
  
  <p>You should then be able to access that variable in your template if it exists:</p>
  
  <pre><code>{% if log is defined %}
      {{ dump(log) }}
  {% endif %}
  </code></pre>
  
  <hr>
  
  <p><code>setRouteVariables</code> adds your variables to Twig's context, which you can see in your template by dumping <code>_context</code>:</p>
  
  <pre><code>{{ dump(_context) }}
  </code></pre>
  
  <p>Or, a handy way to just see the variables available in the context and not the full models is using the <code>keys</code> filter:</p>
  
  <pre><code>{{ dump(_context|keys) }}
  </code></pre>
  
  <hr>
  
  <p>In your case, I believe when your page reloads, your <code>log</code> variable is available, but then you set it to null when you try to retrieve it.</p>
  
  <p>Your current code:</p>
  
  <pre><code>{# Sets log to null, because no post variable exists named `log` #}
  {% set log = craft.request.getPost('log') %}
  
  {% if log is defined %}
      {{ dump(log) }} {# outputs null #}
  {% endif %}
  </code></pre>
  
  <p>Try outputting the log variable before setting your getPost value:</p>
  
  <pre><code>{# Outputs log if it exists in Twig's context #}
  {% if log is defined %}
      {{ dump(log) }} {# will probably output your value #}
  {% endif %}
  
  {# Delete this. It just overwrites your existing log variable #}
  {% set log = craft.request.getPost('log') %}
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-10-23T22:08:39.407Z
Answers.Text: You are hero! This works like a charm. Thanks for not only saving this concrete situation, but now I also understand way better how things work, and what is accessible where, which I'm sure will save me a lot of headaches also down the road.
Answers.UserDisplayName: KSP
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-10-23T21:03:14.430Z
Answers.LastActivityDate: 2016-10-23T21:03:14.430Z
Answers.UserDisplayName: Ben Parizek
Body: <p>I'm trying to write a plugin that lives in the admin interface, that reads external data (a cinema program), parses it, and creates entries based on this (movie info and screening times).</p>
  
  <p>While doing so I build a log of the additions/updates made, that I want to return to the template (the plugin page from where I trigger the update) and display the changes listed in a table. But I have trouble reading anything out with <code>craft.request.getPost()</code> from the template.</p>
  
  <p>The log, basically an array of custom models (log items), seems to be fine while still in the controller. I try to pass this array via <code>craft()-&gt;urlManager-&gt;setRouteVariables()</code>, but at the template, it seems the only variables it finds are the <code>action</code> and <code>redirect</code> parameters from the original form.</p>
  
  <p>The code in the controller looks like this:</p>
  
  <pre><code>$log = craft()-&gt;updateProgram_program-&gt;update();
  craft()-&gt;urlManager-&gt;setRouteVariables(array(
     'log' =&gt; $log
  ));
  </code></pre>
  
  <p>And then I try to read the <code>log</code> variable in my template like this:</p>
  
  <pre><code>{% set log = craft.request.getPost('log') %}
  </code></pre>
  
  <p>Whatever I do, it seems to always be <code>NULL</code>. I've also tried to set a simple text string as the value, in case it's not possible to post an array like this, but it's still <code>NULL</code> when it comes to the template. It seems the <code>log</code> parameter is simply not found in the <code>POST</code> request at all.</p>
  
  <p>Is there anything obvious I'm doing wrong?</p>
  
  <p>I've scratched my head all day over this, so any tips are extremely welcome!</p>
  
Tags: <plugin-development><templating><models><post>
Title: Is it possible to return data/models from controller to template via setRouteVariables?
ThreadText: # Is it possible to return data/models from controller to template via setRouteVariables?
  
  (Asked by: username_0 on 2016-10-23)
  
  I'm trying to write a plugin that lives in the admin interface, that reads external data (a cinema program), parses it, and creates entries based on this (movie info and screening times).
  
  While doing so I build a log of the additions/updates made, that I want to return to the template (the plugin page from where I trigger the update) and display the changes listed in a table. But I have trouble reading anything out with `craft.request.getPost()` from the template.
  
  The log, basically an array of custom models (log items), seems to be fine while still in the controller. I try to pass this array via `craft()->urlManager->setRouteVariables()`, but at the template, it seems the only variables it finds are the `action` and `redirect` parameters from the original form.
  
  The code in the controller looks like this:
  
  ```
  $log = craft()->updateProgram_program->update();
  craft()->urlManager->setRouteVariables(array(
     'log' => $log
  ));
  
  ```
  
  And then I try to read the `log` variable in my template like this:
  
  ```
  {% set log = craft.request.getPost('log') %}
  
  ```
  
  Whatever I do, it seems to always be `NULL`. I've also tried to set a simple text string as the value, in case it's not possible to post an array like this, but it's still `NULL` when it comes to the template. It seems the `log` parameter is simply not found in the `POST` request at all.
  
  Is there anything obvious I'm doing wrong?
  
  I've scratched my head all day over this, so any tips are extremely welcome!
  
  **Tags:** plugin-development, templating, models, post
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2016-10-23. Score: 2 | Accepted answer
  
  I believe you are overwriting the `log` variable you are sending back to the template when you set log equal to the response of getPost.
  
  From what I can see of your code, when you return variables to your template via `setRouteVariables`, you should be providing the variables you define in that array to your template, *without* having to take a second step and retrieve them. So, when your controller returns:
  
  ```
  craft()->urlManager->setRouteVariables(array(
     'log' => $log
  ));
  
  ```
  
  You should then be able to access that variable in your template if it exists:
  
  ```
  {% if log is defined %}
      {{ dump(log) }}
  {% endif %}
  
  ```
  
  ---
  
  `setRouteVariables` adds your variables to Twig's context, which you can see in your template by dumping `_context`:
  
  ```
  {{ dump(_context) }}
  
  ```
  
  Or, a handy way to just see the variables available in the context and not the full models is using the `keys` filter:
  
  ```
  {{ dump(_context|keys) }}
  
  ```
  
  ---
  
  In your case, I believe when your page reloads, your `log` variable is available, but then you set it to null when you try to retrieve it.
  
  Your current code:
  
  ```
  {# Sets log to null, because no post variable exists named `log` #}
  {% set log = craft.request.getPost('log') %}
  
  {% if log is defined %}
      {{ dump(log) }} {# outputs null #}
  {% endif %}
  
  ```
  
  Try outputting the log variable before setting your getPost value:
  
  ```
  {# Outputs log if it exists in Twig's context #}
  {% if log is defined %}
      {{ dump(log) }} {# will probably output your value #}
  {% endif %}
  
  {# Delete this. It just overwrites your existing log variable #}
  {% set log = craft.request.getPost('log') %}
  
  ```
  
  > **Comments:**
  > **username_0**: You are hero! This works like a charm. Thanks for not only saving this concrete situation, but now I also understand way better how things work, and what is accessible where, which I'm sure will save me a lot of headaches also down the road.  
  
  ---
  
  