0

Once again going out of my mind trying to figure something out... I have a custom controller which I want to return a rendered template. The template should, ideally, reside in my front end templates directory.

But I can't for the life of me figure out the path to use. The template I want is at ./templates/how-to-book/holidays-for-destination-options.twig but my controller is unable to find it seemingly regardless of how I try to write the path.

This is a sketchy version of the controller (I'm not getting any data yet, just want to render the correct template)

public function actionLoadHolidays()
    {
        $request = Craft::$app->request;
        $destination = $request->post('destination');
        
        $holidays = [];
        
        $oldMode = Craft::$app->view->getTemplateMode();
        Craft::$app->getView()->setTemplateMode(View::TEMPLATE_MODE_SITE);
    
        $this->renderTemplate('how-to-book/holidays-for-destination-options.twig', array(
            'destination' => $destination,
            'holidays' => $holidays
        ));
        Craft::$app->view->setTemplateMode($oldMode);

    }

Using the path as shown, I get the response Not Found: Template not found: actions/my-module/holidays/load-holidays

I'm hitting this controller via an Ajax request if that has any bearing.

Can someone please help me figure this out - I'm feeling dumb!

0

Yes I was being dumb....

I wasn't returning the rendered template. Controller should be

public function actionLoadHolidays()
    {
        $request = Craft::$app->request;
        $destination = $request->post('destination');
        
        $holidays = [];
        
        $oldMode = Craft::$app->view->getTemplateMode();
        Craft::$app->getView()->setTemplateMode(View::TEMPLATE_MODE_SITE);
    
        return $this->renderTemplate('how-to-book/holidays-for-destination-options.twig', array(
            'destination' => $destination,
            'holidays' => $holidays
        ));

    }
| 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.