2

I have multiple channels with different field groups, but several duplicate fields. For example:

Channels:

  • products_bikes
  • products_skateboards
  • products_trikes

Fields:

  • bikes_summary
  • skateboards_summary
  • trikes_summary

I use the same detail template to output info for all of the channels, but with the duplicate fields, I have to repeat the "summary" field 3 times:

{exp:channel:entries
    channel='products_bikes|products_skateboards|products_trikes'
}
    <h1>{title}</h1>

    {bikes_summary}
    {skateboards_summary}
    {trikes_summary}

{/exp:channel:entries}

And this isn't the only field that's repeated. So that can get pretty hairy, especially if I need to check if a field is empty or not, or if I want to add another product type in the future.

So ideally I'd be able to set a channel prefix, and use it in a single field tag:

{{channel_prefix}_summary}

I can't use the channel_short_name because it doesn't match up, so I figured Stash might be able to do it. I tried:

{exp:stash:channel_prefix}
    {if channel_short_name == 'products_bikes'}
        bikes
    {/if}
    {if channel_short_name == 'products_skateboards'}
        skateboards
    {/if}
    {if channel_short_name == 'products_trikes'}
        trikes
    {/if}
{/exp:stash:channel_prefix}

{{exp:stash:channel_prefix}_summary}

But that output the text "{bikes_summary}" rather than parsed the field tag. I figure parse order has to do with it, but I'm not sure how to fix it - or if it's possible.

Thanks for any help!

3

The first thing I would ask is do you really need a fieldgroup for each product channel? Multiple channels can share the same fieldgroup and you can use publish layouts to hide the fields that are not relevant to a particular product. Using the same fieldgroup will also force you to generalise the fields you use which can be a good thing for rationalising your data structure.

If not then you could adopt the following approach. It does requires that you use a consistent naming scheme for your fields, e.g. channel_fieldname:

{!-- make a list of your channel prefixes --}
{exp:stash:set_list name="channels"}
    {stash:channel}products_bikes{/stash:channel}
    {stash:channel}products_skateboards{/stash:channel}
    {stash:channel}products_trikes{/stash:channel}
{/exp:stash:set_list}

{!-- make a list of your common field suffixes --}
{exp:stash:set_list name="fields"}
    {stash:field}summary{/stash:field}
    {stash:field}another_field{/stash:field}
{/exp:stash:set_list}

{!-- map channels and fields to generate tag pairs --}
{exp:stash:set name="mapped_fields" parse_tags="yes" parse_depth="2" type="snippet"}
   {exp:stash:get_list name="fields"}
      {stash:{field}}
         {exp:stash:get_list:nested name="channels"}
            {{channel}_{field}}
         {/exp:stash:get_list:nested}
      {/stash:{field}}
   {/exp:stash:get_list}
{/exp:stash:set}

{!-- capture the data --}
{exp:stash:set parse_tags="yes" type="snippet"}
   {exp:channel:entries
      channel='{exp:stash:get_list name="channels" backspace="1"}{channel}|{/exp:stash:get_list}'
      parse="inward"
   }

   {mapped_fields}

  {/exp:channel:entries}
{/exp:stash:set}

{!-- example of using the data in your view template (Stash embed or EE embed) --}
{if summary}{summary}{/if}

{!-- example of using the data in the *same* template you capture the data --}
{if summary}{exp:stash:get name="summary" type="snippet"}{/if}

To make this even more robust, you could list the channels automatically using a plugin like Get Channels.

{exp:stash:set_list name="channels" parse_tags="yes"}
    {exp:getchannels}
       {stash:channel}{channel_name}{/stash:channel}
    {/exp:getchannels}
{/exp:stash:set_list}
| improve this answer | |
  • Yikes, that's almost more complicated than just duplicating the fields! – kmgdev Jul 31 '13 at 21:53
  • 1
    Super minor, but could you perhaps elaborate on the capturing and usage example? I have this {exp:stash:get name="date_to_use" type="snippet"} inside of a channel entries loop in the same template as the data capture, but no luck. – jpea Oct 21 '14 at 16:47
0

Stash doesn't work for this because their tow instances are parsed on the same parsing stage. So, when you call stash:channel_prefix its value isn't available yet.

Your better option is to remove the "products_" prefix from the short names of the channels.

| improve this answer | |
0

I think basically if you wrap the second part in a {exp:stash:parse parse_vars="yes" parse_depth="2"} - it will be like the categories bit in my router basically.

https://expressionengine.stackexchange.com/a/12231/1532

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.