17

I have the following code in a template that I use to prepare the data for an embedded template using the Stash partials approach. It is for a single entry page that draws data from an entry that can have one or more images stored in a Matrix field.

{exp:stash:set_list name="business_listing" parse_tags="yes"}
  {stash:title}{title}{/stash:title}
  {stash:address_1}{b_address_1},{/stash:address_1}
  {stash:address_2}{b_address_2},{/stash:address_2}
  {stash:city}{b_city},{/stash:city}
  {b_images}
    {exp:stash:append_list:nested name="business_images" parse_tags="yes"}
    {stash:image}{b_img}{/stash:image}
    {/exp:stash:append_list:nested}
  {/b_images}
{/exp:stash:set_list}

Note that I am using the set_list on the outside and the append_list:nested inside.

I fetch these in the embedded template like so,

{exp:stash:get_list name="business_listing" orderby="title"}
  <ul class="business-images slides">
    {exp:stash:get_list:nested name="business_images" orderby=""}
    <li>
    {exp:ce_img:pair src="{image}" width="277" height="175" crop="yes"}
      <img src="{made}" alt="{title}" width="{width}" height="{height}" />
    {/exp:ce_img:pair}
    </li>
    {/exp:stash:get_list:nested}
  </ul>
  <h1>{title}</h1>
  {address_1}
  {address_2}
  {city}
{/exp:stash:get_list}

Now whilst this code works, I am not sure that it is the best way to do this. The {stash:image_{count}}{b_img}{/stash:image_{count}} feels decidedly dodgy, and as you can see I have left the orderby blank as I am not 100% sure what to put in there for a matrix field (that has only one column 'b_img').

So, if anyone would like to chime in and give me some wisdom on this I would love to hear it.

6

The initial code block could be made more efficient by wrapping the outer list around Channel Entries tag pair and the nested {exp:stash:set_list} around the Matrix variable pair instead of using {exp:stash:append_list} inside it (which creates multiple append_list tags). You can also create an explicit relationship between the outer and inner list by either prepending the entry_id to the inner list name or setting the context of the inner list. This would also have the benefit of supporting multiple Channel entries should you ever need to capture more than one.

{exp:stash:set_list name="business_listing" parse_tags="yes" parse_depth="2"}
  {exp:channel:entries channel="my_channel" limit="1"}
    {stash:entry_id}{entry_id}{/stash:entry_id}
    {stash:title}{title}{/stash:title}
    {stash:address_1}{b_address_1},{/stash:address_1}
    {stash:address_2}{b_address_2},{/stash:address_2}
    {stash:city}{b_city},{/stash:city}
    {exp:stash:set_list:nested name="business_images" context="{entry_id}" parse_tags="yes"}
      {b_images}
        {stash:image}{b_img}{/stash:image}
      {/b_images}
    {/exp:stash:set_list:nested}
  {/exp:channel:entries}
{/exp:stash:set_list}

By default a Stash list will be ordered by the order in which the list rows were captured, so you can leave that blank if you wish.

{exp:stash:get_list name="business_listing"}
  <ul class="business-images slides">
    {exp:stash:get_list:nested name="business_images" context="{entry_id}"}
    <li>
    {exp:ce_img:pair src="{image}" width="277" height="175" crop="yes"}
      <img src="{made}" alt="{title}" width="{width}" height="{height}" />
    {/exp:ce_img:pair}
    </li>
    {/exp:stash:get_list:nested}
  </ul>
  <h1>{title}</h1>
  {address_1}
  {address_2}
  {city}
{/exp:stash:get_list}
| improve this answer | |
  • Mark, I appreciate your response, thanks. Lightbulb moment approaching I'm sure. – Adam Wiggall Nov 27 '12 at 14:36
  • One question on this method for multiple listings. If I use set_list around exp:channel:entries (returning all businesses) I only get the first one returned. What am I missing? – Adam Wiggall Nov 27 '12 at 14:44
  • And as an update to this, using the set_list:nested around the images is only returning one entry per listing? – Adam Wiggall Nov 27 '12 at 14:57
  • You need a recent version of Stash from the DEV branch. The ability to capture multiple rows automatically using a Stash:set_list was only added a few version points back. github.com/croxton/Stash/tree/dev – Mark Croxton Nov 27 '12 at 16:30
  • Appreciate the response, this is stuck on EE 2.1.4 for the time being so I will have to stick with what I have for now as I see the dev branch is 2.4 plus. Thanks – Adam Wiggall Nov 27 '12 at 17:23
3

Looks fine to me, and you can't argue with "it works." ;)

You might try just leaving {count} out completely, though I may be missing something on why you have added that. The last version I used didn't have nesting built in yet, so I'm not totally up to date with the newest quirks.

| improve this answer | |
  • John, you're right on the {count} option, I don't need it, so that helps. – Adam Wiggall Nov 27 '12 at 11:55
2

One problem with that code is it doesn't scale beyond a single business listing as all of the listing images will be appended to the statically named business_images list. You could easily add support for multiple listings by adding an {entry_id} suffix to the name of each image set.

{exp:stash:append_list name="business_listing" parse_tags="yes"}
  {stash:entry_id}{entry_id}{/stash:entry_id}
  {stash:title}{title}{/stash:title}
  {stash:address_1}{b_address_1},{/stash:address_1}
  {stash:address_2}{b_address_2},{/stash:address_2}
  {stash:city}{b_city},{/stash:city}

  {b_images}
    {exp:stash:append_list:nested name="business_images_{entry_id}" parse_tags="yes"}
        {stash:image}{b_img}{/stash:image}
    {/exp:stash:append_list:nested}
  {/b_images}

{/exp:stash:append_list}

Within your business listing loop you'd tweak your get_list code to:

{exp:stash:get_list:nested name="business_images_{entry_id}"}
| improve this answer | |
  • Dom, that is a good point, although this is designed for just a single entry. On that note if I wanted it to be for multiple listings I guess I would need to change the main set_list to append_list as well wouldn't I? – Adam Wiggall Nov 27 '12 at 14:04
  • That you would, my mistake. The set_list function threw me a bit as I've always used {exp:stash:set} for storing single-row datasets (like this). – Dom Stubbs Nov 27 '12 at 14:10

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.