Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-07-30T19:01:41.243Z
Answers.Text: Twig Perversion relies on twig not changing drastically, which is a reasonably safe assumption.
Answers.UserDisplayName: Marion Newlevant
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-07-30T17:41:35.963Z
Answers.LastActivityDate: 2020-07-30T17:41:35.963Z
Answers.UserDisplayName: Lindsey D
Body: <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>
  
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2020-07-29T20:59:54.843Z
Comments.Text: How good are your PHP skills?
Comments.UserDisplayName: Lindsey D
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2020-07-30T10:59:23.930Z
Comments.Text: Hi Lindsey. Not great. Is this plugin territory?
Comments.UserDisplayName: Jonathan Schofield
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2020-07-30T13:55:19.807Z
Comments.Text: Not necessarily, though it would be a reasonable solution.  You can probably pull this off in raw Twig as well.
Comments.UserDisplayName: Lindsey D
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2020-07-30T15:18:31.893Z
Comments.Text: 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?
Comments.UserDisplayName: Jonathan Schofield
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2020-07-30T15:33:10.327Z
Comments.Text: 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.
Comments.UserDisplayName: Jonathan Schofield
Tags: <templating><macro><dry>
Title: DRY variable setting across templates (macro for variable?)
ThreadText: # 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 username_1. 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. username_2 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.  
  
  ---
  
  