Answers.Body: <p>Plugin files are located outside your web root, thats why you can't access them directly. You should create a controller action and render the template from there.</p>
  
  <pre><code>class DefaultController extends Controller{
    protected $allowAnonymous = ['getTemplate'];
  
    public function actionGetTemplate(){
      $request = Craft::$app-&gt;getRequest();
      $templatevar = $request-&gt;getParam('templatevar');
  
      use craft\web\View;
      $oldMode = \Craft::$app-&gt;view-&gt;getTemplateMode();
      \Craft::$app-&gt;view-&gt;setTemplateMode(View::TEMPLATE_MODE_CP);
      $html = \Craft::$app-&gt;view-&gt;renderTemplate('plugin-handle/path/to/template');
      \Craft::$app-&gt;view-&gt;setTemplateMode($oldMode);
    }
  }
  </code></pre>
  
  <p>(see <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>)</p>
  
  <p>You can define the route to your controller action in your plugin file</p>
  
  <pre><code>public function init(){
    Event::on(UrlManager::class, UrlManager::EVENT_REGISTER_SITE_URL_RULES, 
    function(RegisterUrlRulesEvent $event) {
        $event-&gt;rules['gettemplate/&lt;templatevar:\w+&gt;''] = 'myPlugin/getTemplate';
    });
  }
  </code></pre>
  
  <p>and call it via
   $("#modal").load("/gettemplate/"+templatevar);</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-11-23T11:44:03.130Z
Answers.LastActivityDate: 2018-11-23T11:44:03.130Z
Answers.UserDisplayName: phaetons
Body: <p>I'm trying to load a plugin template via ajax inside another plugin template like so: </p>
  
  <pre><code> $("#modal").load("/mypluginname/myplugintemplatefolder/myplugintemplate.html?queryvar="+var); 
  </code></pre>
  
  <p>but it keeps looking for myplugintemplate.html on the frontend templates folder instead of in my plugin template folder - I don't understand how to tell it that I'm trying to load a file from my plugintemplate folder</p>
  
  <p>Basically I'm trying to refresh a section of my template by reloading an include </p>
  
Tags: <plugin-development>
Title: Loading a plugin template via ajax
ThreadText: # Loading a plugin template via ajax
  
  (Asked by: username_0 on 2018-11-23)
  
  I'm trying to load a plugin template via ajax inside another plugin template like so:
  
  ```
   $("#modal").load("/mypluginname/myplugintemplatefolder/myplugintemplate.html?queryvar="+var); 
  
  ```
  
  but it keeps looking for myplugintemplate.html on the frontend templates folder instead of in my plugin template folder - I don't understand how to tell it that I'm trying to load a file from my plugintemplate folder
  
  Basically I'm trying to refresh a section of my template by reloading an include
  
  **Tags:** plugin-development
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2018-11-23. Score: 1
  
  Plugin files are located outside your web root, thats why you can't access them directly. You should create a controller action and render the template from there.
  
  ```
  class DefaultController extends Controller{
    protected $allowAnonymous = ['getTemplate'];
  
    public function actionGetTemplate(){
      $request = Craft::$app->getRequest();
      $templatevar = $request->getParam('templatevar');
  
      use craft\web\View;
      $oldMode = \Craft::$app->view->getTemplateMode();
      \Craft::$app->view->setTemplateMode(View::TEMPLATE_MODE_CP);
      $html = \Craft::$app->view->renderTemplate('plugin-handle/path/to/template');
      \Craft::$app->view->setTemplateMode($oldMode);
    }
  }
  
  ```
  
  (see <https://docs.craftcms.com/v3/extend/updating-plugins.html#rendering-templates>)
  
  You can define the route to your controller action in your plugin file
  
  ```
  public function init(){
    Event::on(UrlManager::class, UrlManager::EVENT_REGISTER_SITE_URL_RULES, 
    function(RegisterUrlRulesEvent $event) {
        $event->rules['gettemplate/<templatevar:\w+>''] = 'myPlugin/getTemplate';
    });
  }
  
  ```
  
  and call it via
  $("#modal").load("/gettemplate/"+templatevar);
  
  
  ---
  
  