1

I'd like to set a variable like "totalCustomers" and then use this throughout our content in different pages and content blocks so that there is one source of truth. Is that possible?

e.g. I define a global variable "totalCustomers" = "10,000"

Then within a new entry I can use this in the heading, something like "We serve [global.totalCustomers] customers daily"

And then in another entry I can use this in some other content "Our [global.totalCustomers] love us"

2 Answers 2

1

You can use reference tags: https://craftcms.com/docs/4.x/reference-tags.html#examples E.g:

  1. Create a Global Set with handle customers
  2. Create a totalCustomers field and assign to said Global Set
  3. Save value to totalCustomers field.
  4. Then use the following ref: {globalset:customers:totalCustomers} as a placeholder on any text field.
  5. When you render the field you'll need to call the parseRefs filter:
entry.myField | parseRefs
0

You can't directly use like this. but there is way with twig replace. you can use same keyword as above [global.totalCustomers] in your heading or any field. then in twig template query the global and replace this with original count in frontend.

{% set globalCount = globalhandle.totalCustomers %}
{{ entry.heading|replace({'[global.totalCustomers]': globalCount}) }}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.