<p>Ok I've made sense of this - if you set the view template mode/context to site, then use the view <strong>renderTemplate()</strong> method (not render) it works.</p>

<p>So this is how you achieve the same thing in <strong>Craft 3</strong>. </p>

<pre class="lang-php prettyprint-override"><code># Set the template context to the site mode.
Craft::$app-&gt;getView()-&gt;setTemplateMode(View::TEMPLATE_MODE_SITE);

# Now using the renderTemplate() method, it will work.
$html = Craft::$app-&gt;getView()-&gt;renderTemplate("/path/to/particular/site/template");
</code></pre>

<p>Edit: Lenka commented pointing out there is actually some <a href="https://docs.craftcms.com/v3/extend/updating-plugins.html#rendering-templates" rel="noreferrer">official documentation</a> on this.</p>

CC BY-SA 4.0
2021-08-10T13:25:25.920Z
What worked for me was Craft::$app->getView()->setTemplateMode('site')
as per the documentation on https://docs.craftcms.com/api/v3/craft-web-view.html#method-settemplatemode
Joan
CC BY-SA 4.0
2018-09-25T23:57:14.050Z
2018-09-26T21:49:45.427Z
2018-09-26T21:49:45.427Z
JamesNZ
<p>This is a comment, but I'm a new participant, so I can't add it.
I recommend the official documentation page <a href="https://docs.craftcms.com/v3/extend/updating-plugins.html#rendering-templates" rel="nofollow noreferrer">https://docs.craftcms.com/v3/extend/updating-plugins.html#rendering-templates</a> where you can find all the variants how to updating Rendering Templates from Craft2 to Craft3.</p>

CC BY-SA 4.0
2018-09-26T18:20:36.633Z
2018-09-26T18:20:36.633Z
lenka
<p>In Craft 2, if you have a <strong>plugin</strong> that needs to load a view from the <strong>site templates</strong> directory, you could just go:</p>

<pre class="lang-php prettyprint-override"><code># Set the template context to site templates.
$path = craft()-&gt;path-&gt;getSiteTemplatesPath();
craft()-&gt;path-&gt;setTemplatesPath($path);

# Now we can load sweet templates from the sites templates dir, not the plugin one.
craft()-&gt;templates-&gt;render("/path/to/particular/site/template");
</code></pre>

<p>I'm struggling with doing this in a <strong>Craft 3 plugin</strong>. I thought I was on the right track with the below, but the context isn't updating.</p>

<pre class="lang-php prettyprint-override"><code># Set the template context to site templates.
$path = Craft::$app-&gt;getPath()-&gt;getSiteTemplatesPath();
Craft::$app-&gt;getView()-&gt;setTemplatesPath($path);

# Or this seems like it might do the same? 
# Craft::$app-&gt;getView()-&gt;setTemplateMode(View::TEMPLATE_MODE_SITE);

# Render the view!
Craft::$app-&gt;getView()-&gt;render("/path/to/particular/site/template")
</code></pre>

<p>This results in explosions. It shows the error:</p>

<blockquote>
  <p>Unable to resolve view file for view
  '/path/to/particular/site/template': no active view context.</p>
</blockquote>

<p>Setting the template context is the bit that's not working. It's still looking at the plugin template directory.</p>

<plugin-development><craft3>
Craft 3 Plugins - Setting Template Path To Site Templates
# Craft 3 Plugins - Setting Template Path To Site Templates

(Asked by: username_0 on 2018-09-25)

In Craft 2, if you have a plugin that needs to load a view from the site templates directory, you could just go:
# Set the template context to site templates.
$path = craft()->path->getSiteTemplatesPath();
craft()->path->setTemplatesPath($path);

# Now we can load sweet templates from the sites templates dir, not the plugin one.
craft()->templates->render("/path/to/particular/site/template");

I'm struggling with doing this in a Craft 3 plugin. I thought I was on the right track with the below, but the context isn't updating.
# Set the template context to site templates.
$path = Craft::$app->getPath()->getSiteTemplatesPath();
Craft::$app->getView()->setTemplatesPath($path);

# Or this seems like it might do the same? 
# Craft::$app->getView()->setTemplateMode(View::TEMPLATE_MODE_SITE);

# Render the view!
Craft::$app->getView()->render("/path/to/particular/site/template")

This results in explosions. It shows the error:

Unable to resolve view file for view
  '/path/to/particular/site/template': no active view context.

Setting the template context is the bit that's not working. It's still looking at the plugin template directory.

**Tags:** plugin-development, craft3


---

## 2 answers

### Answer by username_0 on 2018-09-25. Score: 7

Ok I've made sense of this - if you set the view template mode/context to site, then use the view renderTemplate() method (not render) it works.
So this is how you achieve the same thing in Craft 3. 
# Set the template context to the site mode.
Craft::$app->getView()->setTemplateMode(View::TEMPLATE_MODE_SITE);

# Now using the renderTemplate() method, it will work.
$html = Craft::$app->getView()->renderTemplate("/path/to/particular/site/template");

Edit: Lenka commented pointing out there is actually some official documentation on this.

> **Comments:**
> **username_1**: What worked for me was Craft::$app->getView()->setTemplateMode('site') as per the documentation on https://docs.craftcms.com/api/v3/craft-web-view.html#method-settemplatemode  

---

### Answer by username_2 on 2018-09-26. Score: 0

This is a comment, but I'm a new participant, so I can't add it.
I recommend the official documentation page <https://docs.craftcms.com/v3/extend/updating-plugins.html#rendering-templates> where you can find all the variants how to updating Rendering Templates from Craft2 to Craft3.


---

