<p>Given that it's <code>craft\web\View.php</code> I think you're probably out of luck. Though it's only <code>registerAssetFlashes()</code> that seems to call <code>getSession()</code>, 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.</p>

CC BY-SA 4.0
2018-07-17T10:01:03.857Z
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
CC BY-SA 4.0
2018-07-17T03:48:57.820Z
2018-07-17T05:13:20.643Z
2018-07-17T05:13:20.643Z
andrew.welch
<p>You need to remove every Twig function like <code>craft.app.getSession()</code> within your templates and use the </p>

<pre><code>$view-&gt;renderTemplate($template, $variables); 
</code></pre>

<p>function instead of </p>

<pre><code>$view-&gt;renderPageTemplate($template, $variables);
</code></pre>

CC BY-SA 4.0
2018-07-20T05:25:42.087Z
I can do that, but still get errors with any Twig extension that is added by a plugin.
Tim Kelty
CC BY-SA 4.0
2018-07-17T10:15:17.837Z
2018-07-17T10:15:17.837Z
Robin Schambach
<p>I'm trying to call <code>craft\web\View::renderPageTemplate</code> from within a console controller (also tried from a queue job), but running into all kinds of trouble:</p>

<p>E.g.</p>

<ul>
<li>Exception 'Twig_Error_Runtime' with message 'An exception has been thrown during the rendering of a template ("Session does not exist in a console request.").'

<ul>
<li>I discovered the this was due to <code>registerAssetFlashes</code>: <a href="https://github.com/craftcms/cms/blob/279116ba4091edc67c92fcbbefc257bb4fe30335/src/web/View.php#L1418" rel="nofollow noreferrer">https://github.com/craftcms/cms/blob/279116ba4091edc67c92fcbbefc257bb4fe30335/src/web/View.php#L1418</a></li>
</ul></li>
<li>No plugins that register twig functions/filters work</li>
</ul>

<p>Currently I'm doing:</p>

<pre><code>$view = Craft::$app-&gt;getView();
$view-&gt;setTemplateMode($view::TEMPLATE_MODE_SITE);
$view-&gt;renderPageTemplate($template, $variables);
</code></pre>

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

<p>How can I do this without everything breaking?</p>

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

<console>
How can I render a page template from a queue job/console controller?
# How can I render a page template from a queue job/console controller?

(Asked by: username_0 on 2018-07-17)

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:
E.g.

Exception 'Twig_Error_Runtime' with message 'An exception has been thrown during the rendering of a template ("Session does not exist in a console request.").'


I discovered the this was due to registerAssetFlashes: https://github.com/craftcms/cms/blob/279116ba4091edc67c92fcbbefc257bb4fe30335/src/web/View.php#L1418

No plugins that register twig functions/filters work

Currently I'm doing:
$view = Craft::$app->getView();
$view->setTemplateMode($view::TEMPLATE_MODE_SITE);
$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?

**Tags:** console

---

## 2 answers

### Answer by username_1 on 2018-07-17. Score: 2

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.

> **Comments:**
> **username_0**: 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).  

---

### Answer by username_2 on 2018-07-17. Score: 1

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

> **Comments:**
> **username_0**: I can do that, but still get errors with any Twig extension that is added by a plugin.  

---

