TransWikia.com

What is the updated version of assign_variable?

ExpressionEngine® Answers Asked on October 2, 2021

In former days {assign_variable: my_var="foo"} was a very handy tool. I haven’t used it for some time and discovered that it was deprecated in EE 4.x but was not able to determine what if anything it has been replaced with. Is there a "modern" equivalent? If so, would you mind sharing?

Thanks.

2 Answers

I accidentally tripped over the answer while looking for something else. The feature actually still exists but it is called:

{preload_replace:my_category="britain"}

You can find out more about it here: https://docs.expressionengine.com/latest/templates/globals/preload-replacement.html#preload-text-replacements

Correct answer by forrest on October 2, 2021

The best way of creating transient values within a template is through EE's built in layout variable system.

Layout variables can be defined as single values or as part of an addressable array, and can contain anything you can generate as tag output within a template.

The only wrinkle is that the variables only get instantiated when EE template processing transfers to a subsequent template (using EE's Template Layout system). So the most minimal way of defining and then using a variable would be something like this:

First Template in chain

{layout='template_2'}
{layout:set name="my_var" value="foo"}
{layout:set name="my_var_another_option"}foo{/layout:set}

Second Template in chain ('template_2')

<h1>Here is the content of my_var {layout:my_var}</h1>
<p>And here is the content of my_var_another_option {layout:my_var_another_option}.</p>

I'll leave you to read about the layout variable array option in the docs... :)

HTH

Answered by JCOGS Design on October 2, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP