<p>I think you're using Craft 2 syntax. This should work in Craft 3:</p>
<pre><code>$vars = ['foo' =&gt; 'bar']; // variables loaded into the template
$mode = 'cp'; // or &quot;site&quot;
$html = Craft::$app-&gt;view-&gt;renderTemplate('path/to/template', $vars, $mode);
return $html;
</code></pre>

CC BY-SA 4.0
2021-07-21T13:19:20.247Z
2021-07-21T13:19:20.247Z
gioppe
<p>I'm working on a plugin called <code>modelselector</code> and I want to use a template stored in the plugin <code>templates</code> folder but it should be shown on the front-end of the website.</p>

<p>Currently the following direct URL works for me:
<code>actions/modelSelector/showSelection/40c86a9b-53ec-43f4-8e8b-7c77ee0a13f5</code></p>

<p>I also set up a site route like so:
<code>'modelselector/(?P&lt;selectionUid&gt;[^\/]+)' =&gt; array('action' =&gt; 'modelSelector/showSelection')</code></p>

<p>However when I go to: <code>modelselector/40c86a9b-53ec-43f4-8e8b-7c77ee0a13f5</code> I get the error: <code>Unable to find the template “modelselector/show”.</code>.</p>

<p>I did some research and found that I need to set the template mode using: <code>craft()-&gt;templates-&gt;setTemplateMode(TemplateMode::CP);</code>. But this doesn't seem to work for me. It still can't find the template when the TemplateMode is on <code>CP</code> or <code>Site</code>.</p>

<p>Any ideas?</p>

CC BY-SA 3.0
2017-03-30T12:50:58.247Z
Any help would be greatly appreciated!
Floriskoch
CC BY-SA 4.0
2018-12-09T03:54:20.687Z
Did you ever figure this out? I’m struggling with this too.
Chattervast
<plugin-development><templating>
Using plugin template on front end
# Using plugin template on front end

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

I'm working on a plugin called `modelselector` and I want to use a template stored in the plugin `templates` folder but it should be shown on the front-end of the website.

Currently the following direct URL works for me:
`actions/modelSelector/showSelection/40c86a9b-53ec-43f4-8e8b-7c77ee0a13f5`

I also set up a site route like so:
`'modelselector/(?P[^\/]+)' => array('action' => 'modelSelector/showSelection')`

However when I go to: `modelselector/40c86a9b-53ec-43f4-8e8b-7c77ee0a13f5` I get the error: `Unable to find the template “modelselector/show”.`.

I did some research and found that I need to set the template mode using: `craft()->templates->setTemplateMode(TemplateMode::CP);`. But this doesn't seem to work for me. It still can't find the template when the TemplateMode is on `CP` or `Site`.

Any ideas?

**Tags:** plugin-development, templating
> **Comments:**
> **username_0**: Any help would be greatly appreciated!  
> **username_1**: Did you ever figure this out? I’m struggling with this too.  

---

## 1 answers

### Answer by username_2 on 2021-07-21. Score: 1

I think you're using Craft 2 syntax. This should work in Craft 3:
$vars = ['foo' => 'bar']; // variables loaded into the template
$mode = 'cp'; // or "site"
$html = Craft::$app->view->renderTemplate('path/to/template', $vars, $mode);
return $html;


---

