Body: <p>I'm trying to pass an array to a module's twig template (on the front end). I have tried many solutions I've seen here, but I'm not able to read the passed variables inside the twig template.</p>
  <p>In my module's controller I'm calling my template this way:</p>
  <pre><code>$variables = ['errors' =&gt; $model-&gt;errors]; // $model-&gt;errors is an array of error codes
  $html = \Craft::$app-&gt;view-&gt;renderTemplate(
              'password-global/_formTemplate',
              $variables
          );
  return TemplateHelper::raw($html);
  </code></pre>
  <p>Inside my twig template (_formTemplate.twig) I have this:</p>
  <pre><code>{% if errors|default(null)  %}
  Errors:
  &lt;pre&gt;
      {{ dump(errors) }}
  &lt;/pre&gt;
  {% endif %}
  </code></pre>
  <p>I'm expecting to see a dump of errors' array. Instead, I see nothing, and there's not any error or clue to indicate me what I'm doing wrong. Any suggestions would be incredibly useful.</p>
  <p>Thank you.</p>
  
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2021-10-22T07:25:14.173Z
Comments.Text: Are you sure that the model has any errors? Try dumping the `_context` like this in your Twig template to see if the `errors` variable exists: `{% dd _context %}` Or if that's too unwieldy: `{% dd _context|keys %}`
Comments.UserDisplayName: MoritzLost
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2021-10-22T12:23:37.327Z
Comments.Text: Thanks for looking into this. Yes I did a _context dump, and I do see my errors array ` 'errors' => [
          'theGreeting' => [
              0 => 'The password cannot be validated.'
          ]
          'theDate' => [
              0 => 'The Date should contain at most 0 characters.'
          ]
      ]`. So I'm not sure why I cannot access it directly.
Comments.UserDisplayName: mauricio
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2021-10-22T12:29:49.003Z
Comments.Text: Is the `{{ dump(errors) }}` statement in the same context you tested the `_context` in? Is it maybe in an included template, a macro, and overwritten blog, or anything else that might not have access to the parent scope?
Comments.UserDisplayName: MoritzLost
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2021-10-22T12:32:41.240Z
Comments.Text: Also, what you doing with the `TemplateHelper::raw` method? Maybe that's somehow messing with the output. Can you try rendering the output using `$this->renderTemplate()` and returning it directly: `return $this->renderTemplate('password-global/_formTemplate', $variables)`. This returns a Response object ... just to make sure that nothing else is messing with your output.
Comments.UserDisplayName: MoritzLost
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2021-10-22T13:51:43.963Z
Comments.Text: I removed TemplateHelper::raw and am returning `$this->renderTemplate()` directly. The issue remains the same. Both `dump(errors)` and `dump(_context)` are written in the same module's twig template where I'm trying to read the variables. Big sight. I'm new to Craft and the MVC architecture in general. So I could be easily making a mistake.
Comments.UserDisplayName: mauricio
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2021-10-22T13:59:29.657Z
Comments.Text: Hm, that's curious. Based on the code you posted it should work. Is your environment set to `dev` in your `.env` file? The `dump` method is only available in dev mode, where the Twig debug extension is included. Can you try looping over the array instead, outputting the keys and nested values? Just to make sure the problem is not the `dump` method itself. If this doesn't work, it might help to see the module code in it's entirety.
Comments.UserDisplayName: MoritzLost
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2021-10-22T18:27:47.697Z
Comments.Text: One additional thought: the Craft-y pattern is usually to pass then entire `Model` back to the view (i.e. so attributes, etc. can be displayed as they were submitted, even if an error occurred).
  
  In error states, you can actually call `Craft::$app->getUrlManager()->setRouteParams(['myModel' => $model]);` and then `return null` from your action—this will basically let the request fall through to the original route, and populate arguments in the action that match (assuming it _was_ a controller action). 
  
  @MoritzLost suggestion to use `$this->renderTemplate()` is solid. 
Comments.UserDisplayName: August Miller
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2021-10-23T04:40:57.713Z
Comments.Text: @MoritzLost, thank you for the feedback. Your help made me realize I was making a very silly mistake. The template I was rendering corresponded to a partial, not a full page. So, I guess, that might explain why I couldn't see the errors variable I was passing (?).
Comments.UserDisplayName: mauricio
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2021-10-23T05:21:02.983Z
Comments.Text: @AugustMiller, thank you for helping. Really appreciated. I'm trying to implement your solution: setting the route parameters and returning null from the controllers function. With this, the browser reloads the page that made the request, as you mentioned it. But I haven't figured out how to access the` myModel` parameter in the reloaded page. I have tried `craft.request.getParam('myModel'))` and `craft.app.request.getBodyParam('myModel')`. Both return a `null` value.
Comments.UserDisplayName: mauricio
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2021-10-24T00:08:51.620Z
Comments.Text: This may depend on how the original page is routed (i.e. via another controller action, or falling directly through to a template), but it _may_ already be available as `{{ myModel }}`! Moritz's recommendation to use `{{ dump(_context | keys) }}` is great for seeing what gets automatically populated.
Comments.UserDisplayName: August Miller
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2021-10-24T00:16:25.000Z
Comments.Text: I looks like the asker has posted a more recent question about route params, [here](https://craftcms.stackexchange.com/questions/38511/how-to-access-setrouteparams-model-in-form-validation), for anyone following! We may revisit this question if part of the new answer applies.
Comments.UserDisplayName: August Miller
Tags: <templating><modules>
Title: Passing variables, and reading them, to Module template
ThreadText: # Passing variables, and reading them, to Module template
  
  (Asked by: username_0 on 2021-10-22)
  
  I'm trying to pass an array to a module's twig template (on the front end). I have tried many solutions I've seen here, but I'm not able to read the passed variables inside the twig template.
  
  In my module's controller I'm calling my template this way:
  
  ```
  $variables = ['errors' => $model->errors]; // $model->errors is an array of error codes
  $html = \Craft::$app->view->renderTemplate(
              'password-global/_formTemplate',
              $variables
          );
  return TemplateHelper::raw($html);
  
  ```
  
  Inside my twig template (\_formTemplate.twig) I have this:
  
  ```
  {% if errors|default(null)  %}
  Errors:
  <pre>
      {{ dump(errors) }}
  </pre>
  {% endif %}
  
  ```
  
  I'm expecting to see a dump of errors' array. Instead, I see nothing, and there's not any error or clue to indicate me what I'm doing wrong. Any suggestions would be incredibly useful.
  
  Thank you.
  
  **Tags:** templating, modules
  
  > **Comments:**
  > **username_1**: Are you sure that the model has any errors? Try dumping the `_context` like this in your Twig template to see if the `errors` variable exists: `{% dd _context %}` Or if that's too unwieldy: `{% dd _context|keys %}`  
  > **username_0**: Thanks for looking into this. Yes I did a _context dump, and I do see my errors array ` 'errors' => [         'theGreeting' => [             0 => 'The password cannot be validated.'         ]         'theDate' => [             0 => 'The Date should contain at most 0 characters.'         ]     ]`. So I'm not sure why I cannot access it directly.  
  > **username_1**: Is the `{{ dump(errors) }}` statement in the same context you tested the `_context` in? Is it maybe in an included template, a macro, and overwritten blog, or anything else that might not have access to the parent scope?  
  > **username_1**: Also, what you doing with the `TemplateHelper::raw` method? Maybe that's somehow messing with the output. Can you try rendering the output using `$this->renderTemplate()` and returning it directly: `return $this->renderTemplate('password-global/_formTemplate', $variables)`. This returns a Response object ... just to make sure that nothing else is messing with your output.  
  > **username_0**: I removed TemplateHelper::raw and am returning `$this->renderTemplate()` directly. The issue remains the same. Both `dump(errors)` and `dump(_context)` are written in the same module's twig template where I'm trying to read the variables. Big sight. I'm new to Craft and the MVC architecture in general. So I could be easily making a mistake.  
  > **username_1**: Hm, that's curious. Based on the code you posted it should work. Is your environment set to `dev` in your `.env` file? The `dump` method is only available in dev mode, where the Twig debug extension is included. Can you try looping over the array instead, outputting the keys and nested values? Just to make sure the problem is not the `dump` method itself. If this doesn't work, it might help to see the module code in it's entirety.  
  > **username_2**: One additional thought: the Craft-y pattern is usually to pass then entire `Model` back to the view (i.e. so attributes, etc. can be displayed as they were submitted, even if an error occurred).  In error states, you can actually call `Craft::$app->getUrlManager()->setRouteParams(['myModel' => $model]);` and then `return null` from your action—this will basically let the request fall through to the original route, and populate arguments in the action that match (assuming it _was_ a controller action).   @username_1 suggestion to use `$this->renderTemplate()` is solid.   
  > **username_0**: @username_1, thank you for the feedback. Your help made me realize I was making a very silly mistake. The template I was rendering corresponded to a partial, not a full page. So, I guess, that might explain why I couldn't see the errors variable I was passing (?).  
  > **username_0**: @username_2, thank you for helping. Really appreciated. I'm trying to implement your solution: setting the route parameters and returning null from the controllers function. With this, the browser reloads the page that made the request, as you mentioned it. But I haven't figured out how to access the` myModel` parameter in the reloaded page. I have tried `craft.request.getParam('myModel'))` and `craft.app.request.getBodyParam('myModel')`. Both return a `null` value.  
  > **username_2**: This may depend on how the original page is routed (i.e. via another controller action, or falling directly through to a template), but it _may_ already be available as `{{ myModel }}`! Moritz's recommendation to use `{{ dump(_context | keys) }}` is great for seeing what gets automatically populated.  
  > **username_2**: I looks like the asker has posted a more recent question about route params, [here](https://craftcms.stackexchange.com/questions/38511/how-to-access-setrouteparams-model-in-form-validation), for anyone following! We may revisit this question if part of the new answer applies.  
  
  ---
  
  ## 0 answers
  
  