croxton / Stash

Stash allows you to stash text and snippets of code for reuse throughout your templates.
GNU General Public License v3.0
197 stars 20 forks source link

optional paramater to save a stash:embed as a var #35

Closed timkelty closed 11 years ago

timkelty commented 11 years ago

I find myself often using stash:embeds for code reuse.

I find myself doing this alot:

{exp:channel:entries
  disable="categories|category_fields|member_data|pagination|trackbacks"
  dynamic="yes"
  require_entry="yes"
}
    {exp:stash:content}
      {stash:embed:tab_products
        process="start"
        stash:field="cf_store_product_suggested"}
    {/exp:stash:content}
{/exp:channel:entries}
...
 {exp:stash:get name="content"}

What do you think about adding a param to to stash:embed save the output to a stash var? Or maybe use the output param? So it might look like:

      {stash:embed:tab_products
        process="start"
        output="variable|snippet"
        name="content
        stash:field="cf_store_product_suggested"}
...
 {exp:stash:get name="content"}

Does that make sense?

croxton commented 11 years ago

The good news is that Stash already does this (kind of). Stash embeds are saved into memory as native Stash variables, and you can access them at any point after the template has been embedded:

{exp:stash:get name="tab_products"}

Note that is is equivalent to the following:

{exp:stash:embed name="tab_products" process="inline"}

In both cases the value is read from memory, a new query or file-read is not required.

Or within a template parsed by Stash:

{stash:tab_products}

In your case you want to want to include the embed and capture it's parsed value, rather than the value before parsing. For this you can use the parse_stage="set" parameter. Note that you would need to pass any field values captured from the channel entries tag to the embed via stash:my_var parameters:

{exp:channel:entries
  disable="categories|category_fields|member_data|pagination|trackbacks"
  dynamic="yes"
  require_entry="yes"
}
      {stash:embed:tab_products
        process="inline"
        parse_stage="set"
        stash:field="{cf_store_product_suggested}"
      }
{/exp:channel:entries}

{!-- you need to access later in the parse order of the template if using in same template, hence process="end" --}
 {exp:stash:get name="tab_products" process="end"}

If you are trying to pass values that may contain quotes, you could do this:

{exp:channel:entries
  disable="categories|category_fields|member_data|pagination|trackbacks"
  dynamic="yes"
  require_entry="yes"
}
      {exp:stash:set}
            {stash:store_product_suggested}{cf_store_product_suggested}{/stash:store_product_suggested}
      {/exp:stash:set}
      {stash:embed:tab_products
        process="inline"
        parse_stage="set"
      }
{/exp:channel:entries}

{!-- you need to access later in the parse order of the template if using in same template, hence process="end" --}
 {exp:stash:get name="tab_products" process="end"}
timkelty commented 11 years ago

Brilliant! Thanks Mark.