<p>So the way Twig works is that it populates a Twig Environment with a <em>context</em> of global variables.</p>

<p>Then any time a template <code>include</code>s another template, the context is passed down to it.</p>

<p>So if you want something to be globally available you have to insert it into the Twig environment <em>before</em> Twig is used, for it to be truly globally available.</p>

<p>You can do that via a Twig extension that <code>implements GlobalsInterface</code> like this:</p>

<p><a href="https://github.com/nystudio107/craft-seomatic/blob/v3/src/twigextensions/SeomaticTwigExtension.php#L36" rel="nofollow noreferrer">https://github.com/nystudio107/craft-seomatic/blob/v3/src/twigextensions/SeomaticTwigExtension.php#L36</a></p>

<p>So you might need to revise how you're doing it actually, and instead of using <code>View::EVENT_BEGIN_PAGE</code> you can just do whatever you need to do inside your Twig extension.</p>

CC BY-SA 4.0
2020-05-26T19:44:11.087Z
Thanks that example helped a lot
Callum
CC BY-SA 4.0
2020-05-26T18:13:17.240Z
2020-05-26T18:13:17.240Z
andrew.welch
<p>I have created an event to transform the entry data before rendering the page so that it's ready to render components in the template. </p>

<pre><code>Event::on(View::class, View::EVENT_BEGIN_PAGE, [$this, 'transformSingleEntry']);
</code></pre>

<p>Here's an example of what the function looks like. I've cut out most of the code, but essentially at the end of the function I have a <code>$transformed_entry</code> variable that I want to add to the twig context. </p>

<pre><code>public function transformSingleEntry() {
  $entry = Craft::$app-&gt;urlManager-&gt;getMatchedElement();
  // ... do stuff
  $transformed_entry = [
    'title' =&gt; 'test',
    'url' =&gt; 'test',
  ];
  // Add $transformed_entry to the page context
}
</code></pre>

<p>I cannot find anything in the Craft documentation to help here. Any ideas?</p>

<templating><frontend>
Add variable to twig context
# Add variable to twig context

(Asked by: username_0 on 2020-05-26)

I have created an event to transform the entry data before rendering the page so that it's ready to render components in the template. 
Event::on(View::class, View::EVENT_BEGIN_PAGE, [$this, 'transformSingleEntry']);

Here's an example of what the function looks like. I've cut out most of the code, but essentially at the end of the function I have a $transformed_entry variable that I want to add to the twig context. 
public function transformSingleEntry() {
  $entry = Craft::$app->urlManager->getMatchedElement();
  // ... do stuff
  $transformed_entry = [
    'title' => 'test',
    'url' => 'test',
  ];
  // Add $transformed_entry to the page context
}

I cannot find anything in the Craft documentation to help here. Any ideas?

**Tags:** templating, frontend


---

## 1 answers

### Answer by username_1 on 2020-05-26. Score: 4 | Accepted answer

So the way Twig works is that it populates a Twig Environment with a *context* of global variables.

Then any time a template `include`s another template, the context is passed down to it.

So if you want something to be globally available you have to insert it into the Twig environment *before* Twig is used, for it to be truly globally available.

You can do that via a Twig extension that `implements GlobalsInterface` like this:

<https://github.com/nystudio107/craft-seomatic/blob/v3/src/twigextensions/SeomaticTwigExtension.php#L36>

So you might need to revise how you're doing it actually, and instead of using `View::EVENT_BEGIN_PAGE` you can just do whatever you need to do inside your Twig extension.

> **Comments:**
> **username_0**: Thanks that example helped a lot  

---

