<p>As you noted in your comment, the <a href="https://plugins.craftcms.com/twig-perversion" rel="nofollow noreferrer">Twig Perversion</a> plugin will do exactly what you need. It allows you to <code>return</code> a value from a macro, which isn't typically something that Twig permits.</p>
<p>Personally, I'd consider it to be fairly safe to rely on this plugin for a few reasons. Marion is a long-standing and trusted developer in the community, the plugin is relatively simple, and could easily be replaced with a small custom module if it ever went defunct. It also doesn't touch your data, so removing it in the future won't cause any problems.</p>

CC BY-SA 4.0
2020-07-30T19:01:41.243Z
Twig Perversion relies on twig not changing drastically, which is a reasonably safe assumption.
Marion Newlevant
CC BY-SA 4.0
2020-07-30T17:41:35.963Z
2020-07-30T17:41:35.963Z
Lindsey D
<p>I have a need for something like the following:</p>
<pre><code>{# Template A #}
    {# Do X that sets variable x #}
    {# Do Y that sets variable y #}
    {# Pass x and y into A include partials #}

{# Template B #}
    {# Do X that sets variable x #}
    {# Do Y that sets variable y #}
    {# Pass x and y into B include partials #}
</code></pre>
<p>The logic for X and Y is the same in templates A and B.</p>
<p>Is there a way to write each once in some kind of dedicated partial and place them in templates A and B, something akin to <code>return</code> in a JavaScript function?</p>
<p>I'm not sure there is. I <em>think</em> that macros only produce front-end output. And so I have to duplicate the logic in each template. Is that right?</p>

CC BY-SA 4.0
2020-07-29T20:59:54.843Z
How good are your PHP skills?
Lindsey D
CC BY-SA 4.0
2020-07-30T10:59:23.930Z
Hi Lindsey. Not great. Is this plugin territory?
Jonathan Schofield
CC BY-SA 4.0
2020-07-30T13:55:19.807Z
Not necessarily, though it would be a reasonable solution.  You can probably pull this off in raw Twig as well.
Lindsey D
CC BY-SA 4.0
2020-07-30T15:18:31.893Z
Ok. Am I right that you can’t use a Macro to change the state/contents of a variable – e.g. a hash – because a Macro is predicated on generating _markup_? If yes, is there another technique?
Jonathan Schofield
CC BY-SA 4.0
2020-07-30T15:33:10.327Z
Have just found Robin Schambach’s 2017 answer to [Use a Twig macro to set a variable](https://craftcms.stackexchange.com/a/23194/3507). I note that [Twig perversion](https://plugins.craftcms.com/twig-perversion) provides a `return` scope, which I think is what I need, but I’m nervous about relying on a third party plugin for this. Would be great if it were native to Craft.
Jonathan Schofield
<templating><macro><dry>
DRY variable setting across templates (macro for variable?)
# DRY variable setting across templates (macro for variable?)

(Asked by: username_0 on 2020-07-29)

I have a need for something like the following:
{# Template A #}
    {# Do X that sets variable x #}
    {# Do Y that sets variable y #}
    {# Pass x and y into A include partials #}

{# Template B #}
    {# Do X that sets variable x #}
    {# Do Y that sets variable y #}
    {# Pass x and y into B include partials #}

The logic for X and Y is the same in templates A and B.
Is there a way to write each once in some kind of dedicated partial and place them in templates A and B, something akin to return in a JavaScript function?
I'm not sure there is. I think that macros only produce front-end output. And so I have to duplicate the logic in each template. Is that right?

**Tags:** templating, macro, dry
> **Comments:**
> **username_1**: How good are your PHP skills?  
> **username_0**: Hi Lindsey. Not great. Is this plugin territory?  
> **username_1**: Not necessarily, though it would be a reasonable solution.  You can probably pull this off in raw Twig as well.  
> **username_0**: Ok. Am I right that you can’t use a Macro to change the state/contents of a variable – e.g. a hash – because a Macro is predicated on generating _markup_? If yes, is there another technique?  
> **username_0**: Have just found Robin Schambach’s 2017 answer to [Use a Twig macro to set a variable](https://craftcms.stackexchange.com/a/23194/3507). I note that [Twig perversion](https://plugins.craftcms.com/twig-perversion) provides a `return` scope, which I think is what I need, but I’m nervous about relying on a third party plugin for this. Would be great if it were native to Craft.  

---

## 1 answers

### Answer by username_1 on 2020-07-30. Score: 2

As you noted in your comment, the [Twig Perversion](https://plugins.craftcms.com/twig-perversion) plugin will do exactly what you need. It allows you to `return` a value from a macro, which isn't typically something that Twig permits.

Personally, I'd consider it to be fairly safe to rely on this plugin for a few reasons. Marion is a long-standing and trusted developer in the community, the plugin is relatively simple, and could easily be replaced with a small custom module if it ever went defunct. It also doesn't touch your data, so removing it in the future won't cause any problems.

> **Comments:**
> **username_2**: Twig Perversion relies on twig not changing drastically, which is a reasonably safe assumption.  

---

