"Request scope" in Craft/Yii? #12951
-
Still wondering what is the best way to use a "request scope" in Craft/Yii. Something that is described in a legacy system as "a variable scope that lives for the duration of a http/console request, and is accessible from everwhere", i.e. from every twig template, php code etc. Useful for example to set a copyright notice in a header image template Currently we are using this: {% do craft.app.urlManager.setRouteParams({copyright: image.copyright}, true) %}
---
{% set copyright = craft.app.urlManager.getRouteParams.copyright ?? '' %} which works but looks semantically strange. What do we miss? /Mel |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
Twig doesn’t have a built-in way of doing this. It’s come up a few times before, though, and there’s the Transient plugin, but it hasn’t yet been updated for Craft 4. So I just submitted a PR that would bring similar functionality into Craft natively: #12990 |
Beta Was this translation helpful? Give feedback.
-
Ended up taking a different approach. Craft 4.5 is going to introduce a new {% do _globals.set('copyright', image.copyright) %}
---
{{ _globals.get('copyright') }} |
Beta Was this translation helpful? Give feedback.
Ended up taking a different approach. Craft 4.5 is going to introduce a new
_globals
global Twig variable, which is set to a collection, and be used to store globally-scoped values across all your templates. (#13050)