<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>

CC BY-SA 3.0
2017-05-28T06:26:31.370Z
That's a slightly nicer way to do it, without have to modify the template! Thanks.
James White
CC BY-SA 3.0
2017-05-27T20:34:19.083Z
2017-05-27T20:34:19.083Z
Ben Croker
<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>

CC BY-SA 3.0
2017-05-27T05:03:56.483Z
2017-05-27T05:03:56.483Z
James White
<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>

CC BY-SA 3.0
2017-05-26T20:06:31.440Z
If it's a preview, could you just wrap the content with a `pre` tag?
Stephen Lewis
CC BY-SA 3.0
2017-05-26T20:35:23.773Z
@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?
James White
CC BY-SA 3.0
2017-05-26T21:04:29.180Z
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.
Stephen Lewis
CC BY-SA 3.0
2017-05-26T21:14:23.327Z
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.
James White
CC BY-SA 3.0
2017-05-26T21:17:24.550Z
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.
Stephen Lewis
CC BY-SA 3.0
2017-05-26T21:22:29.740Z
Yeah its not pretty, but the actual email template being sent is the version being previewed, so limited options.
James White
<templating>
Using renderTemplate for a plain text twig template
# 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**: @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?  
> **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 to fetch the template and then wrap it in pre tags before outputting:
// Wrap in pre tags
$output = '' . craft()->templates('emailPreviewPlain') .'';

// 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 Stephen'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' %}
    {{ '' | raw }}
{% endif %}

{# Plain text template content goes here #}

{% if craft.request.getLastSegment == 'emailPreviewPlain' %}
    {{ '' | 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.


---

