4

Use case

The front page of a site has a region that should display the most recent item of featured content. Since this content can come from various channels, it needs to display custom channel fields depending on the channel that it originates from.

Approach

I'm trying to output the value of a custom field using stash but am only able to output the literal value of the variable. I'm using example code found on the stash repo at: https://github.com/croxton/Stash/wiki/Mapping-custom-fields-to-a-common-nomenclature

So my code looks like:

{!-- make a list of your channel prefixes --}
{exp:stash:set_list name="channels"}
    {stash:channel}article{/stash:channel}
    {stash:channel}book{/stash:channel}
    {stash:channel}project{/stash:channel}
    {stash:channel}service{/stash:channel}
{/exp:stash:set_list}

{!-- make a list of your common field suffixes --}
{exp:stash:set_list name="fields"}
    {stash:field}subtitle{/stash:field}
    {stash:field}year{/stash:field}
    {stash:field}description{/stash:field}
    {stash:field}body{/stash:field}
    {stash:field}image{/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}

...

{exp:channel:entries status="Front page featured" orderby="date|edit_date" sort="desc" limit="1" }

    {title} <br>

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

{/exp:channel:entries}

Outputs:

Dr. Strangelove or: 
{project-title}

Where {project-title} should render How I Learned to Stop Worrying and Love the Bomb

Edit

I've moved my channel entries to a stash embed and now calling the {subtitle} variable doesn't evaluate. However, calling {mapped_fields} returns:

{stash:subtitle}How I Learned to Stop Worrying and Love the Bomb{/stash:subtitle}
{stash:description}Dr. Strangelove, is a 1964 black comedy film that satirizes the cold war fears of a nuclear conflict between the USSR and the US. The film was directed, produced, and co-written by Stanley ...{/stash:description}

But, I'm unable to access these values as individual variables within my view template.

Edit 2

My current embedded view template looks like the following:

{exp:channel:entries status="Front page featured" orderby="date|edit_date" sort="desc" limit="1"}

    {subtitle}<br>

    {mapped_fields}

{/exp:channel:entries}

I've tried variations on the variable syntax including {exp:stash:get name="subtitle"} and {exp:stash:get name="subtitle" type="snippet"} as well as wrapping the channel entries tag w/ {exp:stash:parse process="end"}

Strangely, if I have a channel prefix where the channel doesn't have a corresponding field, e.g., service-subtitle then that will render {service-subtitle} in the view when calling {subtitle}.

Edit 3

Here's a test example that simplifies template architecture and produces similar results as described above.

test.group - index.html

{!-- make a list of your channel prefixes --}
{exp:stash:set_list name="channels"}
    {stash:channel}article{/stash:channel}
    {stash:channel}book{/stash:channel}
    {stash:channel}project{/stash:channel}
{/exp:stash:set_list}

{!-- make a list of your common field suffixes --}
{exp:stash:set_list name="fields"}
    {stash:field}subtitle{/stash:field}
    {stash:field}description{/stash:field}
    {stash:field}body{/stash:field}
    {stash:field}image{/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}

{!-- embed the view --}
{stash:embed:test}

stash_templates - test.html

{exp:channel:entries status="Front page featured" orderby="date|edit_date" sort="desc" limit="1"}

    {subtitle}<br>

    {mapped_fields}

{/exp:channel:entries}

Rendered result (in browser)

{subtitle}

{stash:subtitle} Test subtitle {/stash:subtitle} {stash:description}

Test description {/stash:description} {stash:body} {book-body}

Test body {/stash:body} {stash:image} {/stash:image}

  • What happens if instead of {exp:stash:get name="subtitle"} You use: {exp:stash:get name="subtitle" type="snippet"} - use the type variable ? – stuartmcd69 Jan 30 '15 at 6:12
  • I've tried specifying type with the same result as calling {subtitle}. I've updated my question in response. – Jack Frost Jan 30 '15 at 18:22
5
+50

There's no sense in running channel entries twice.

This code generates a bunch of stash capture pairs, mapping field names in different channels to a common name:

{!-- make a list of your channel prefixes --}
{exp:stash:set_list name="channels"}
    {stash:channel}article{/stash:channel}
    {stash:channel}book{/stash:channel}
    {stash:channel}project{/stash:channel}
{/exp:stash:set_list}

{!-- make a list of your common field suffixes --}
{exp:stash:set_list name="fields"}
    {stash:field}subtitle{/stash:field}
    {stash:field}description{/stash:field}
    {stash:field}body{/stash:field}
    {stash:field}image{/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}

The {mapped fields} variable would then be set to this value (formatted for legibility). Verify this is the case by looking in your template debug output for a line beginning Stash: SET mapped_fields to value ...

{stash:subtitle} 
   {article-subtitle} {book-subtitle} {project-subtitle}
{/stash:subtitle} 

{stash:description} 
    {article-description} {book-description} {project-description} 
{/stash:description} 

{stash:body} 
    {article-body} {book-body} {project-body} 
{/stash:body} 

{stash:image} 
    {article-image} {book-image} {project-image} 
{/stash:image}

Now you just need to use this variable when capturing your channel entry data. Note the dynamic="no".

{exp:stash:set type="snippet" parse_tags="yes"}
    {exp:channel:entries 
        status="Front page featured" 
        orderby="date|edit_date" 
        sort="desc" 
        limit="1" 
        dynamic="no"
        disable="member_data|categories|pagination"
    }
        {mapped_fields}
    {/exp:channel:entries}
{/exp:stash:set}

You then include the stash embed like so (this would be better at the top the of your template, but no matter). A native layout would also work just the same.

{stash:embed:test}

In your embed you can now access your 'Front page featured' content like so:

Subtitle: {subtitle}
Description: {description}
Body: {body}
Image: {image}
| improve this answer | |
  • It seems that default fields, e.g., {title} aren't available using when using this method. – Jack Frost Feb 4 '15 at 0:50
  • This is still problematic in that empty positions are rendered alongside the intended value. Let's say I'm calling to the "articles" channel which has a subtitle value, the page then renders "How I Learned to Stop Worrying and Love the Bomb {book-subtitle}" – Jack Frost Feb 18 '15 at 19:53

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.