I'm trying to load a plugin template via ajax inside another plugin template like so:


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

1 Answer 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();
    $html = \Craft::$app->view->renderTemplate('plugin-handle/path/to/template');

(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);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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