1

I have a section "Emails" which has an entry 'Welcome Email'. The template assigned for it under Section is '_email/welcome'. This works fine when viewed in frontend. But I want to render the same template in my plugin services. The code I have used is:

$template = 'welcome';
$data = 'foo';

$oldPath = craft()->path->getTemplatesPath();
$path = craft()->path->getTemplatesPath().'_email/';
craft()->path->setTemplatesPath($path);
$html = craft()->templates->render($template,$data);
$breaks = array("<br />","<br>","<br/>");  
$text = str_ireplace($breaks, "\r\n", $html);
craft()->path->setTemplatesPath($oldPath);

But this gives an error "_email/_layout" not found in craft.log . What am I doing wrong ? Could anyone please help.

Thanks in advance.

1

If it is a frontend request

$html = craft()->templates->render('_emails/_layout.twig', $data);

and in a backend request

$oldPath = craft()->path->getTemplatesPath();
$newPath = craft()->path->getSiteTemplatesPath();
craft()->path->setTemplatesPath($newPath);
$html = craft()->templates->render('_emails/_layout.twig', array());
craft()->path->setTemplatesPath($oldPath);
|improve this answer|||||
  • For backend request it Still gives same error exception.Craft\TemplateLoaderException] exception 'Craft\TemplateLoaderException' with message 'Unable to find the template “_emails/_layout.twig”.' in /..path../craft/app/etc/templating/twigextensions/TemplateLoader.php:123 – hashtagerrors Dec 11 '17 at 5:21
  • And your template is a .twig file and in /_emails/_layout.twig? Because it works for me and it always has. Could you dump the $newPath and check if it's correct? – Robin Schambach Dec 11 '17 at 5:25
  • Everything was correct but still things were not working. But strangely after clearing out cache your code worked. Thanks. – hashtagerrors Dec 11 '17 at 6:27

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.