0

I'm trying to use Stash embeds on a pretty complex site and can 'see' a way to make things more DRY but can't actually figure out how to 'do'.

I have a collection of viewModal templates, that handle the various channels, and a lot of these channels share a naming convention for their right column meta data. In a perfect world, I would've shared these fieldgroups across the channels, but as that's not possible, I had to repeat myself a lot in the fields.

An example (stripped down slightly):

/editorial.html (viewModal template)

{!-- Set the page layout --}
{stash:embed:layouts:standard}

{!-- Right side area [inject the rightcol_meta partial into the {stash:page_meta} region] --}
{exp:stash:set_value name="st_page_meta" value="{exp:stash:embed:partials:rightcol_meta}"}

{!-- Main content area --}
{exp:channel:entries limit="1" channel="editorial|structure_editorial" disable="member_data|pagination|categories" require_entry="yes"}
    {exp:stash:st_page_title}{title}{/exp:stash:st_page_title}
    {exp:stash:st_page_body}{cf_editorial_body}{/exp:stash:st_page_body}

    {!-- (make all the meta fields snippets) --}
    {!-- Related links --}
    {exp:stash:set_list name="st_rel_links" parse_tags="yes"}
        **{cf_editorial_rel_links}**
            {stash:st_rel_link_title}{title}{/stash:st_rel_link_title}
            {stash:st_rel_link_url}{page_url}{/stash:st_rel_link_url}
        **{/cf_editorial_rel_links}**
    {/exp:stash:set_list}

    {!-- Related publications --}
    {exp:stash:set_list name="st_rel_pubs" parse_tags="yes"}
        **{cf_editorial_rel_publication}**
            {stash:st_rel_link_title}{title}{/stash:st_rel_link_title}
            {stash:st_rel_link_url}{page_url}{/stash:st_rel_link_url}
        **{/cf_editorial_rel_publication}**
    {/exp:stash:set_list}

    {!-- featureboxes (make all the meta fields snippets --}
    {exp:stash:set_list name="st_fbox_list" parse_tags="yes"}
        **{cf_editorial_fbox}**
            {stash:st_fbox_type}{cf_fbox_type}{/stash:st_fbox_type}
        {!-- Heading style --}
            {stash:st_fbox_heading}{cf_fbox_heading}{/stash:st_fbox_heading}
            {stash:st_fbox_heading_body}{cf_fbox_body}{/stash:st_fbox_heading_body}
        {!-- Image --}
            {cf_fbox_img}
                {stash:st_fbox_img_src}{url}{/stash:st_fbox_img_src}
                {stash:st_fbox_img_alt}{alt_text}{/stash:st_fbox_img_alt}
            {/cf_fbox_img}
            {stash:st_fbox_img_link}{cf_fbox_img_link}{/stash:st_fbox_img_link}     
        **{/cf_editorial_fbox}**
    {/exp:stash:set_list}

{/exp:channel:entries}

I've surrounded the parts I'm talking about in *s (turns out I can't bold inside a code snippet). I want to be able to change these sections into snippets, and instead of {cf_editorial_rel_links} etc I would like to use {cf_{channel_short_name}_rel_links} so that in each viewModal I could just include 1 snippet. Even if I had to use {cf_{exp:stash:get name="channel_name"}_rel_links} it would be fine.

Is this possible? I think it is, but can't seem to get it to parse the channel short name (with stash or otherwise) in time for it to be processed correctly.

Thanks in advance for any help!

Rob

2 Answers 2

1

I'm afraid that won't work, because those relationship variable pairs are parsed by the channel entries tag before being captured by Stash. As the value of {channel_short_name} cannot be known until the channel entries tag has been parsed you cannot dynamically determine the name of the variable pairs.

What you could do however is make the code inside the variable pairs into a snippet:

    {stash:st_rel_link_title}{title}{/stash:st_rel_link_title}
    {stash:st_rel_link_url}{page_url}{/stash:st_rel_link_url}
1
  • Hi Mark, thanks for getting back to me. I do actually know the channel short name for each viewModel, so can I stash get inside a set list? I'm off to experiment now :)
    – Rob Hodges
    Commented Mar 18, 2013 at 9:37
0

OK, so I managed to solve this.

In the viewModel template, I've set a preload replace variable:

{!-- Used for right column meta data --}
{preload_replace:pr_channel="editorial"}

At the top of the page, and then put the entire meta fields section, including the stash:set_list tags in one snippet: {sp_st_fields_page_meta}.

Inside that I can just use {pr_channel} and it will parse at the correct time, meaning that all these variables (21 in the real site) can be set for each viewModel (15, it is a huge site) with only one snippet :)

Hope this helps someone!

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.