Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2021-08-10T13:25:25.920Z
Answers.Text: 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
Answers.UserDisplayName: Joan
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-09-25T23:57:14.050Z
Answers.LastActivityDate: 2018-09-26T21:49:45.427Z
Answers.LastEditDate: 2018-09-26T21:49:45.427Z
Answers.UserDisplayName: JamesNZ
Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-09-26T18:20:36.633Z
Answers.LastActivityDate: 2018-09-26T18:20:36.633Z
Answers.UserDisplayName: lenka
Body: <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>
  
Tags: <plugin-development><craft3>
Title: Craft 3 Plugins - Setting Template Path To Site Templates
ThreadText: # 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:
  
  ```php
  # 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.
  
  ```php
  # 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**.
  
  ```php
  # 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: username_2 commented pointing out there is actually some [official documentation](https://docs.craftcms.com/v3/extend/updating-plugins.html#rendering-templates) 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.
  
  
  ---
  
  