Answers.Body: <p>You could use the <a href="https://craftcms.com/classreference/services/TemplatesService#render-detail" rel="nofollow noreferrer">TemplatesService render() method</a> to fetch the template and then wrap it in <code>pre</code> tags before outputting:</p>
  
  <pre><code>// Wrap in pre tags
  $output = '&lt;pre&gt;' . craft()-&gt;templates('emailPreviewPlain') .'&lt;/pre&gt;';
  
  // Output it into a buffer       
  ob_start();
  echo $output;
  
  // End the request
  craft()-&gt;end();
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-05-28T06:26:31.370Z
Answers.Text: That's a slightly nicer way to do it, without have to modify the template! Thanks.
Answers.UserDisplayName: James White
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-05-27T20:34:19.083Z
Answers.LastActivityDate: 2017-05-27T20:34:19.083Z
Answers.UserDisplayName: Ben Croker
Answers.Body: <p>This is isn't an elegant way of doing it, but essentially as per Stephen's suggestion, if you can wrap the plain text output in a <code>pre</code> block, the preview will visually render the spacing and line breaks in HTML mode in the same way it would be received via email.</p>
  
  <p>You could do this in the actionPreview function itself, or alternatively in the template use a condition that would only be true when the email template is being accessed in Craft for this purpose. I chose to use <code>craft.request</code></p>
  
  <pre><code>{% if craft.request.getLastSegment == 'emailPreviewPlain' %}
      {{ '&lt;pre&gt;' | raw }}
  {% endif %}
  
  {# Plain text template content goes here #}
  
  {% if craft.request.getLastSegment == 'emailPreviewPlain' %}
      {{ '&lt;/pre&gt;' | raw }}
  {% endif %}
  </code></pre>
  
  <p>This then wraps the entire twig template in <code>pre</code> tags when being previewed in Craft, but otherwise should not render when sent as an email.</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-05-27T05:03:56.483Z
Answers.LastActivityDate: 2017-05-27T05:03:56.483Z
Answers.UserDisplayName: James White
Body: <p>Is there a suitable way to use renderTemplate or another method to output a twig template that is plain text?</p>
  
  <p>I'm working with a plugin that has an email template for both HTML and plain text, previewing the HTML version is simple, doing the same with the plain text works but the visual preview within Craft shows the content without any of the spacing or line breaks because its rendering as HTML, viewing the source the correct output with the line breaks and spacing etc is shown. I was wondering if there was a way to output the plain text template in a similar way.</p>
  
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2017-05-26T20:06:31.440Z
Comments.Text: If it's a preview, could you just wrap the content with a `pre` tag?
Comments.UserDisplayName: Stephen Lewis
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2017-05-26T20:35:23.773Z
Comments.Text: @StephenLewis That would be a good idea but the preview is directly rendering the twig email template as is which is sent through the MTA, so I can't add a `pre` wrapper in the template itself. Is it possible to wrap the renderTemplate function with `<pre>` `</pre>` or implement it another way?
Comments.UserDisplayName: James White
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2017-05-26T21:04:29.180Z
Comments.Text: You could pass a flag into the template, which specifies whether or not to output the `pre` tags. That feels a little hacky, but I can't think of a cleaner approach off the top of my head.
Comments.UserDisplayName: Stephen Lewis
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2017-05-26T21:14:23.327Z
Comments.Text: Wrapping the renderTemplate request with `pre` works great. I know what you mean. I think its the only way. I could add a function to make a check to see if the request has been made from Craft and then use that in Twig to output `pre` tags conditionally? Basically a condition that otherwise won't be true when the email actually gets sent.
Comments.UserDisplayName: James White
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2017-05-26T21:17:24.550Z
Comments.Text: If you absolutely have to use the same template for the preview and the actual email, then yes. Still feels like a horrible workaround, though.
Comments.UserDisplayName: Stephen Lewis
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2017-05-26T21:22:29.740Z
Comments.Text: Yeah its not pretty, but the actual email template being sent is the version being previewed, so limited options.
Comments.UserDisplayName: James White
Tags: <templating>
Title: Using renderTemplate for a plain text twig template
ThreadText: # Using renderTemplate for a plain text twig template
  
  (Asked by: username_0 on 2017-05-26)
  
  Is there a suitable way to use renderTemplate or another method to output a twig template that is plain text?
  
  I'm working with a plugin that has an email template for both HTML and plain text, previewing the HTML version is simple, doing the same with the plain text works but the visual preview within Craft shows the content without any of the spacing or line breaks because its rendering as HTML, viewing the source the correct output with the line breaks and spacing etc is shown. I was wondering if there was a way to output the plain text template in a similar way.
  
  **Tags:** templating
  
  > **Comments:**
  > **username_1**: If it's a preview, could you just wrap the content with a `pre` tag?  
  > **username_0**: @username_1 That would be a good idea but the preview is directly rendering the twig email template as is which is sent through the MTA, so I can't add a `pre` wrapper in the template itself. Is it possible to wrap the renderTemplate function with `<pre>` `</pre>` or implement it another way?  
  > **username_1**: You could pass a flag into the template, which specifies whether or not to output the `pre` tags. That feels a little hacky, but I can't think of a cleaner approach off the top of my head.  
  > **username_0**: Wrapping the renderTemplate request with `pre` works great. I know what you mean. I think its the only way. I could add a function to make a check to see if the request has been made from Craft and then use that in Twig to output `pre` tags conditionally? Basically a condition that otherwise won't be true when the email actually gets sent.  
  > **username_1**: If you absolutely have to use the same template for the preview and the actual email, then yes. Still feels like a horrible workaround, though.  
  > **username_0**: Yeah its not pretty, but the actual email template being sent is the version being previewed, so limited options.  
  
  ---
  
  ## 2 answers
  
  ### Answer by username_2 on 2017-05-27. Score: 2
  
  You could use the [TemplatesService render() method](https://craftcms.com/classreference/services/TemplatesService#render-detail) to fetch the template and then wrap it in `pre` tags before outputting:
  
  ```
  // Wrap in pre tags
  $output = '<pre>' . craft()->templates('emailPreviewPlain') .'</pre>';
  
  // Output it into a buffer       
  ob_start();
  echo $output;
  
  // End the request
  craft()->end();
  
  ```
  
  > **Comments:**
  > **username_0**: That's a slightly nicer way to do it, without have to modify the template! Thanks.  
  
  ---
  
  ### Answer by username_0 on 2017-05-27. Score: 2
  
  This is isn't an elegant way of doing it, but essentially as per username_1's suggestion, if you can wrap the plain text output in a `pre` block, the preview will visually render the spacing and line breaks in HTML mode in the same way it would be received via email.
  
  You could do this in the actionPreview function itself, or alternatively in the template use a condition that would only be true when the email template is being accessed in Craft for this purpose. I chose to use `craft.request`
  
  ```
  {% if craft.request.getLastSegment == 'emailPreviewPlain' %}
      {{ '<pre>' | raw }}
  {% endif %}
  
  {# Plain text template content goes here #}
  
  {% if craft.request.getLastSegment == 'emailPreviewPlain' %}
      {{ '</pre>' | raw }}
  {% endif %}
  
  ```
  
  This then wraps the entire twig template in `pre` tags when being previewed in Craft, but otherwise should not render when sent as an email.
  
  
  ---
  
  