<p>If it is a frontend request</p>

<pre><code>$html = craft()-&gt;templates-&gt;render('_emails/_layout.twig', $data);
</code></pre>

<p>and in a backend request</p>

<pre><code>$oldPath = craft()-&gt;path-&gt;getTemplatesPath();
$newPath = craft()-&gt;path-&gt;getSiteTemplatesPath();
craft()-&gt;path-&gt;setTemplatesPath($newPath);
$html = craft()-&gt;templates-&gt;render('_emails/_layout.twig', array());
craft()-&gt;path-&gt;setTemplatesPath($oldPath);
</code></pre>

CC BY-SA 3.0
2017-12-11T05:21:02.563Z
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
CC BY-SA 3.0
2017-12-11T05:25:28.687Z
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
CC BY-SA 3.0
2017-12-11T06:27:47.297Z
Everything was correct but still things were not working. But strangely after clearing out cache your code worked. Thanks.
hashtagerrors
CC BY-SA 3.0
2017-12-09T15:04:52.210Z
2017-12-09T15:04:52.210Z
Robin Schambach
<p>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:</p>

<pre><code>$template = 'welcome';
$data = 'foo';

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

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

<p>Thanks in advance.</p>

<plugin-development>
How to render an entry template in plugin?
# How to render an entry template in plugin?

(Asked by: username_0 on 2017-12-09)

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("","","");  
$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.

**Tags:** plugin-development


---

## 1 answers

### Answer by username_1 on 2017-12-09. Score: 1 | Accepted answer

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

> **Comments:**
> **username_0**: 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`  
> **username_1**: 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?  
> **username_0**: Everything was correct but still things were not working. But strangely after clearing out cache your code worked. Thanks.  

---

