<p>I ended up using Jon's function renderPluginTemplate I found at <a href="https://craftcms.stackexchange.com/a/7132/6062">https://craftcms.stackexchange.com/a/7132/6062</a> which basically implements what's written here: <a href="https://craftcms.com/docs/plugins/templates#plugin-template-paths-explained" rel="nofollow noreferrer">https://craftcms.com/docs/plugins/templates#plugin-template-paths-explained</a></p>

<p>I wonder why there is no easier function included into craft by default.</p>

CC BY-SA 3.0
2017-01-30T01:13:08.947Z
2017-01-30T01:13:08.947Z
2017-04-13T12:51:00.303Z
Manticore
<p>I want to render a (non-CP) template from an action method in my plugin controller over a route I defined in the plugin base class:</p>

<pre><code>function registerSiteRoutes() {
  return array(
    'myservice' =&gt; array('action' =&gt; 'pluginhandle/controller/method')
  );
}
</code></pre>

<p>The action itself basically just sets some things up and renders the template:</p>

<pre><code>public function actionMethod()
{
    // irrelevant code here...

    $variables = array('info' =&gt; 'success');
    $this-&gt;renderTemplate('pluginhandle/template', $variables);
}
</code></pre>

<p>But when trying to access the rendered template via it's routed url '/myservice', an error message is shown telling <em>the template can't be found</em>.</p>

<p>Strangely, it seems to work if I use the <strong>full action url</strong> <code>actions/pluginhandle/controller/method</code> instead. But, I want to have <em>nice</em> URLs if I can have them, right?</p>

<p>I tried it using <code>craft()-&gt;templates-&gt;render('pluginhandle/template')</code> and <code>$this-&gt;renderPluginTemplate('pluginhandle/template')</code> even temporarily setting a new templates path but none of them seems to change that.</p>

<p>Can someone please help me out?</p>

<plugin-development><templating><url><routing><controller>
No template found when accessing action method per route
# No template found when accessing action method per route

(Asked by: username_0 on 2017-01-27)

I want to render a (non-CP) template from an action method in my plugin controller over a route I defined in the plugin base class:
function registerSiteRoutes() {
  return array(
    'myservice' => array('action' => 'pluginhandle/controller/method')
  );
}

The action itself basically just sets some things up and renders the template:
public function actionMethod()
{
    // irrelevant code here...

    $variables = array('info' => 'success');
    $this->renderTemplate('pluginhandle/template', $variables);
}

But when trying to access the rendered template via it's routed url '/myservice', an error message is shown telling the template can't be found.
Strangely, it seems to work if I use the full action url actions/pluginhandle/controller/method instead. But, I want to have nice URLs if I can have them, right?
I tried it using craft()->templates->render('pluginhandle/template') and $this->renderPluginTemplate('pluginhandle/template') even temporarily setting a new templates path but none of them seems to change that.
Can someone please help me out?

**Tags:** plugin-development, templating, url, routing, controller


---

## 1 answers

### Answer by username_0 on 2017-01-30. Score: 1

I ended up using Jon's function renderPluginTemplate I found at <https://craftcms.stackexchange.com/a/7132/6062> which basically implements what's written here: <https://craftcms.com/docs/plugins/templates#plugin-template-paths-explained>

I wonder why there is no easier function included into craft by default.


---

