<p>Craft will only serve templates from craft/templates/ by default if it’s a front-end site request.</p>

<p>To explicitly tell Craft to serve templates from there, you can use <a href="http://buildwithcraft.com/classreference/services/PathService#setTemplatesPath-detail" rel="noreferrer">PathService::setTemplatesPath()</a>:</p>

<pre class="lang-php prettyprint-override"><code>$path = craft()-&gt;path-&gt;getSiteTemplatesPath();
craft()-&gt;path-&gt;setTemplatesPath($path);

$this-&gt;renderTemplate('path/to/template', $variables);
</code></pre>

CC BY-SA 3.0
2014-10-28T09:41:18.650Z
2015-06-15T17:43:38.180Z
2015-06-15T17:43:38.180Z
Brandon Kelly
<p>I want to be able to render a template in the main 'templates' directory from within a plugin controller.</p>

<p>Basically the controller gets the url request, does some work and then renders a template saved in the 'craft/templates' directory instead of the 'plugin/templates' directory.</p>

<p>I have tried the plugin path trick mentioned on this page <a href="https://craftcms.com/docs/plugins/templates" rel="nofollow noreferrer">https://craftcms.com/docs/plugins/templates</a> without any success.</p>

<p>Any ideas?</p>

CC BY-SA 3.0
2014-10-24T22:15:05.447Z
The trick on that page should do it for you. Can you `var_dump` the paths to make sure they are pointing to where you think they are pointing?
Brad Bell
CC BY-SA 3.0
2014-10-24T23:22:26.880Z
The path echoes out where I would expect '/Users/derrick/Documents/wwwroot/iha/craft/templates/' however when I do $html = craft()->templates->render('test'); it always get a 404 page not found. I don't need an entry or route setup to the 'test' template correct? If I browser to local.site.com/test I see the test template.
Derrick Grigg
CC BY-SA 3.0
2014-10-25T16:57:18.990Z
Digging a little more into this. If I pass a path for a template that doesn't exist I get an error message "Unable to find the template “_test”." However if pass a path for a template that does exist, I don't get that error message but I do get redirected to the 404, page not found. Does that make sense? Right now the template has no logic in just a little markup, there's nothing in it to redirect. Is there something that needs to get returned from the controller method to prevent a 404?
Derrick Grigg
CC BY-SA 3.0
2014-10-25T16:59:22.027Z
Found the answer, you need to use the $this->renderTemplate() in the controller vs the craft()->templates->render();
Derrick Grigg
CC BY-SA 3.0
2014-10-28T09:43:08.393Z
@DerrickGrigg [BaseController::renderTemplate()](http://buildwithcraft.com/classreference/controllers/BaseController#renderTemplate-detail) calls [TemplatesService::render()](http://buildwithcraft.com/classreference/services/TemplatesService#render-detail) internally. Guessing that you just happened to be triggering Craft from a site request when testing that theory. See my answer below.
Brandon Kelly
<plugin-development><templating>
renderTemplate in a plugin for front end request using front end template
# renderTemplate in a plugin for front end request using front end template

(Asked by: username_0 on 2014-10-24)

I want to be able to render a template in the main 'templates' directory from within a plugin controller.

Basically the controller gets the url request, does some work and then renders a template saved in the 'craft/templates' directory instead of the 'plugin/templates' directory.

I have tried the plugin path trick mentioned on this page <https://craftcms.com/docs/plugins/templates> without any success.

Any ideas?

**Tags:** plugin-development, templating
> **Comments:**
> **username_1**: The trick on that page should do it for you. Can you `var_dump` the paths to make sure they are pointing to where you think they are pointing?  
> **username_0**: The path echoes out where I would expect '/Users/derrick/Documents/wwwroot/iha/craft/templates/' however when I do $html = craft()->templates->render('test'); it always get a 404 page not found. I don't need an entry or route setup to the 'test' template correct? If I browser to local.site.com/test I see the test template.  
> **username_0**: Digging a little more into this. If I pass a path for a template that doesn't exist I get an error message "Unable to find the template “_test”." However if pass a path for a template that does exist, I don't get that error message but I do get redirected to the 404, page not found. Does that make sense? Right now the template has no logic in just a little markup, there's nothing in it to redirect. Is there something that needs to get returned from the controller method to prevent a 404?  
> **username_0**: Found the answer, you need to use the $this->renderTemplate() in the controller vs the craft()->templates->render();  
> **username_2**: @DerrickGrigg [BaseController::renderTemplate()](http://buildwithcraft.com/classreference/controllers/BaseController#renderTemplate-detail) calls [TemplatesService::render()](http://buildwithcraft.com/classreference/services/TemplatesService#render-detail) internally. Guessing that you just happened to be triggering Craft from a site request when testing that theory. See my answer below.  

---

## 1 answers

### Answer by username_2 on 2014-10-28. Score: 8

Craft will only serve templates from craft/templates/ by default if it’s a front-end site request.
To explicitly tell Craft to serve templates from there, you can use PathService::setTemplatesPath():
$path = craft()->path->getSiteTemplatesPath();
craft()->path->setTemplatesPath($path);

$this->renderTemplate('path/to/template', $variables);


---

