I'm trying to call craft\web\View::renderPageTemplate from within a console controller (also tried from a queue job), but running into all kinds of trouble:


Currently I'm doing:

$view = Craft::$app->getView();
$view->renderPageTemplate($template, $variables);

The end goal is to render a template, cache it, and send the html to a web service.

How can I do this without everything breaking?

…Or is there another way to do this, or should this be a feature request?


Given that it's craft\web\View.php I think you're probably out of luck. Though it's only registerAssetFlashes() that seems to call getSession(), but I'm expecting more than just that may assume a frontend request. You could always ping a controller endpoint via Guzzle to return the Twig-rendered HTML to you though.

| improve this answer | |
  • I thought about just making a web request via Guzzle but that isn't really what I want in this case. This is not a publicly accessible template, I need to pass it $variables, and I want to cache the result (the latter I could still do via Guzzle). – Tim Kelty Jul 17 '18 at 10:01

You need to remove every Twig function like craft.app.getSession() within your templates and use the

$view->renderTemplate($template, $variables); 

function instead of

$view->renderPageTemplate($template, $variables);
| improve this answer | |
  • I can do that, but still get errors with any Twig extension that is added by a plugin. – Tim Kelty Jul 20 '18 at 5:25

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.