1

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

| improve this question | | | | |
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);

| improve this answer | | | | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.