3

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.

  • If it's a preview, could you just wrap the content with a pre tag? – Stephen Lewis May 26 '17 at 20:06
  • @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 May 26 '17 at 20:35
  • 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 May 26 '17 at 21:04
  • 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 May 26 '17 at 21:14
  • 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 May 26 '17 at 21:17
1

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' %}
    {{ '<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.

1

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 = '<pre>' . craft()->templates('emailPreviewPlain') .'</pre>';

// Output it into a buffer       
ob_start();
echo $output;

// End the request
craft()->end();
  • That's a slightly nicer way to do it, without have to modify the template! Thanks. – James White May 28 '17 at 6:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.