0

I'm currently using a template that displays an entry that could come from one of two channels like so;

{exp:channel:entries
    channel="channel1|channel2"
    status="open"
    limit="1"
    require_entry="yes"
}
    {exp:stash:set parse_tags="yes" parse_depth="3" save="yes" scope="site"}
        {stash:place_shortdesc}{cf_channel1_shortdescription}{cf_channel2_shortdescription}{/stash:place_shortdesc}
        {stash:place_desc}{cf_channel1_description}{cf_channel2_description}{/stash:place_desc}
    {/exp:stash:set}
{/exp:channel:entries}

I'm using Stash to create the variables, for example {stash:place_shortdesc} where the short description could come from 1 of 2 fields, depending on which channel the current entry is from.

I was wondering if this is the best way to approach this, particularly for performance? or whether it needs something like conditional loading of fields based on channel?

Thanks.

2 Answers 2

2

There's no need for conditionals as the fields are mutually exclusive. With Stash on the inside of channel:entries tags there's also no need for parse_tags or parse_depth since the enclosed content contains no tags. (The custom fields are variables, not tags). Otherwise it should still be fine the way you have it. Personally I prefer putting Stash on the outside of tags, and generally use set_list even for single entries, since this allows you to store a single named cache entry instead of lots of separate ones for each variable. I also find it simplifies any future parse order issues a bit:

{exp:stash:set_list name="cached-data-{segment_2}" parse_tags="yes" save="yes" scope="site"}
    {exp:channel:entries
        channel="channel1|channel2"
        status="open"
        limit="1"
        require_entry="yes"
    }        
        {stash:place_shortdesc}{cf_channel1_shortdescription}{cf_channel2_shortdescription}{/stash:place_shortdesc}
        {stash:place_desc}{cf_channel1_description}{cf_channel2_description}{/stash:place_desc}        
    {/exp:channel:entries}
{/exp:stash:set_list}
0

I would just use a conditional for which tags to use in the stash:set:

{exp:channel:entries
    channel="channel1|channel2"
    status="open"
    limit="1"
    require_entry="yes"
}
    {exp:stash:set 
        parse_tags="yes" 
        parse_conditionals="yes" 
    }
        {if channel == 'channel1'}
            {stash:place_shortdesc}{cf_channel1_shortdescription}{/stash:place_shortdesc}
            {stash:place_desc}{cf_channel1_description}{/stash:place_desc}
        {/if}
        {if channel == 'channel2'}
            {stash:place_shortdesc}{cf_channel2_shortdescription}{/stash:place_shortdesc}
            {stash:place_desc}{cf_channel2_description}{/stash:place_desc}
        {/if}
    {/exp:stash:set}
{/exp:channel:entries}

I don't think performance would be an issue as you're only dealing with a single entry. One thing that would help though is not using additional parse_depth unless it's needed for stash set. Also, pretty sure you need to add parse_conditionals="yes" to the set parameters.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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