<p>Craft will only check for templates in your plugin’s templates/ folder during CP and action requests. When you start routing to your controller action via a site-facing route, it’s no longer either one of those; it’s just a normal Site request. So you will need to point Craft to your plugin’s templates folder explicitly.</p>

<pre><code>// Point Craft to this plugin's templates/ folder
$templatesPath = craft()-&gt;path-&gt;getPluginsPath().'myplugin/templates/';
craft()-&gt;path-&gt;setTemplatesPath($templatesPath);

// Render and output the template
$this-&gt;renderTemplate('_mytemplate', $variables);
</code></pre>

<p>Note that it will no longer be necessary to include your plugin’s handle in the template path.</p>

CC BY-SA 3.0
2015-09-03T10:48:12.903Z
Because I no longer need the plugin's handle in the template path, I also have to remove it from `extends` tags in the template. Which is fine, except when the layout I'm extending `extends` a layout from my main (non-plugin) templates. It now can't find that layout, because it's looking in my plugin templates.
Phil Gyford
CC BY-SA 3.0
2015-09-03T20:24:21.230Z
@PhilGyford Yeah if you go this route, you can only include templates that live inside the directory passed into setTemplatesPath(). No way around that currently.
Brandon Kelly
CC BY-SA 3.0
2014-06-30T18:38:49.693Z
Minor typo in your solution, it should apparently be `craft()->path->setTemplatesPath`... Other than that, it worked perfectly! Thanks Brandon!
Lindsey D
CC BY-SA 3.0
2014-06-30T11:03:08.267Z
2014-06-30T18:58:23.410Z
2014-06-30T18:58:23.410Z
Brandon Kelly
<p><strong>This may be a bug...</strong></p>
<p>I have a controller action which has been working great. It's a simple action with a single purpose... load a template with an internal value.</p>
<pre><code>public function actionMyAction()
{
    $this-&gt;renderTemplate('myplugin/_mytemplate', array(
        'internalVar' =&gt; craft()-&gt;myPlugin-&gt;internalVar
    ));
}
</code></pre>
<p>When I call that action via a simple URL, everything loads flawlessly:</p>
<blockquote>
<p>mywebsite.com/actions/myPlugin/myAction</p>
</blockquote>
<p><strong>Here's where it gets interesting...</strong></p>
<p>So I just discovered the <a href="http://buildwithcraft.com/docs/plugins/hooks-reference#registerSiteRoutes" rel="nofollow noreferrer">registerSiteRoutes</a> hook. It looks awesome, and would make accessing my special page that much easier for a front-end user!</p>
<p>Here's how I implemented it in my <code>MyPluginPlugin.php</code> file:</p>
<pre><code>public function registerSiteRoutes()
{
    return array(
        'myroute' =&gt; array('action' =&gt; 'myPlugin/myAction')
    );
}
</code></pre>
<p><strong>The good news is,</strong> the route is properly followed, and my controller action gets loaded.</p>
<p><strong>The bad news is,</strong> suddenly the template that's supposed to be loaded can't be found. :(</p>
<blockquote>
<p><strong>Template Error</strong></p>
<p>Unable to find the template &quot;myplugin/_mytemplate&quot;.</p>
</blockquote>
<p>So does anyone know, <strong>what's going on here?</strong> I know that the <code>renderTemplate</code> method does some minor rewriting of the template path, but why would it be different when accessing via a front-end route?</p>

<plugin-development><controller><routes>
Is the "registerSiteRoutes" hook screwing up my template path?
# Is the "registerSiteRoutes" hook screwing up my template path?

(Asked by: username_0 on 2014-06-30)

This may be a bug...
I have a controller action which has been working great. It's a simple action with a single purpose... load a template with an internal value.
public function actionMyAction()
{
    $this->renderTemplate('myplugin/_mytemplate', array(
        'internalVar' => craft()->myPlugin->internalVar
    ));
}

When I call that action via a simple URL, everything loads flawlessly:

mywebsite.com/actions/myPlugin/myAction

Here's where it gets interesting...
So I just discovered the registerSiteRoutes hook. It looks awesome, and would make accessing my special page that much easier for a front-end user!
Here's how I implemented it in my MyPluginPlugin.php file:
public function registerSiteRoutes()
{
    return array(
        'myroute' => array('action' => 'myPlugin/myAction')
    );
}

The good news is, the route is properly followed, and my controller action gets loaded.
The bad news is, suddenly the template that's supposed to be loaded can't be found. :(

Template Error
Unable to find the template "myplugin/_mytemplate".

So does anyone know, what's going on here? I know that the renderTemplate method does some minor rewriting of the template path, but why would it be different when accessing via a front-end route?

**Tags:** plugin-development, controller, routes


---

## 1 answers

### Answer by username_1 on 2014-06-30. Score: 7 | Accepted answer

Craft will only check for templates in your plugin’s templates/ folder during CP and action requests. When you start routing to your controller action via a site-facing route, it’s no longer either one of those; it’s just a normal Site request. So you will need to point Craft to your plugin’s templates folder explicitly.
// Point Craft to this plugin's templates/ folder
$templatesPath = craft()->path->getPluginsPath().'myplugin/templates/';
craft()->path->setTemplatesPath($templatesPath);

// Render and output the template
$this->renderTemplate('_mytemplate', $variables);

Note that it will no longer be necessary to include your plugin’s handle in the template path.

> **Comments:**
> **username_0**: Minor typo in your solution, it should apparently be `craft()->path->setTemplatesPath`... Other than that, it worked perfectly! Thanks Brandon!  
> **username_2**: Because I no longer need the plugin's handle in the template path, I also have to remove it from `extends` tags in the template. Which is fine, except when the layout I'm extending `extends` a layout from my main (non-plugin) templates. It now can't find that layout, because it's looking in my plugin templates.  
> **username_1**: @PhilGyford Yeah if you go this route, you can only include templates that live inside the directory passed into setTemplatesPath(). No way around that currently.  

---

