croxton / Stash

Stash allows you to stash text and snippets of code for reuse throughout your templates.
GNU General Public License v3.0
197 stars 20 forks source link

Stash Only Working Inside Channel Entries Tag #83

Closed oblogic7 closed 10 years ago

oblogic7 commented 10 years ago

I am having trouble getting stash to store the entry_ids of a 2 level relationship. I have 3 related channels. Case Study has one Project and a Project belongs to many Portfolio items. I need to get Portfolio entry_ids from a case study entries tag.

Stash is only working when the get_list tag (line 13) is inside of the channel entries tag. If I move the get_list tag outside of the channel entries loop it quits working. I need to use the stash outside of the channel entries tag.

Here is the code... (not working)

https://gist.github.com/oblogic7/2c528aef8dc7b2fae754

Any ideas why this would not work? Is it a problem traversing the relationships, or am I doing something wrong?

oblogic7 commented 10 years ago

Ok... I got the code above to work by using the save parameter.

Is it possible to use the stashed entry_ids as a parameter in another channel entries tag like below?

{exp:channel:entries channel="portfolio" dynamic="off" disable="pagination|member_data" entry_id="{exp:stash:get_list name="parent_ids"}{pid}|{/exp:stash:get_list}"}

I know it looks like I am going around the world to retrieve the related portfolio items, but I have a special use case that makes this necessary. I'm building it up as I go and it should work perfectly when all the pieces are there.

GDmac commented 10 years ago

IMO the stash list should go outside the loop (either all the way outside channel entries or outside project tag):

{exp:stash:append_list name="parent_ids"}
  {exp:channel:entries ...}
    {project}
      {project:parents field="project"}
        {stash:pid}{project:parents:entry_id}{/stash:pid}
      {/project:parents}
    {/project}
  {/exp:channel:entries}
{/exp:stash:append_list}
oblogic7 commented 10 years ago

I'll give this a shot and see if it makes a difference.

On Friday, December 6, 2013, GDmac wrote:

IMO the stash list should go outside the loop (either all the way outside channel entries or outside project tag):

{exp:stash:append_list name="parent_ids"} {exp:channel:entries ...} {project} {project:parents field="project"} {stash:pid}{project:parents:entry_id}{/stash:pid} {/project:parents} {/project} {/exp:channel:entries} {/exp:stash:append_list}

— Reply to this email directly or view it on GitHubhttps://github.com/croxton/Stash/issues/83#issuecomment-29974310 .

croxton commented 10 years ago

Your problem arises from a misunderstanding of parse order. Tags are parsed in layers, like peeling an onion.

Please take a look at slides 17 -29 here, which also contains one solution: https://speakerdeck.com/croxton/stash-core-principles?slide=17

GDmac commented 10 years ago

@croxton regarding 2. e.g. using stash variable as a parameter to channel:entries. Personally, i like to avoid using plugins/modules as parameter. What would be an approach with stash to pass thru the entry ids in the following example?

{exp:plugin}
  {exp:channel:entries entry_id="{plugin_value}"}
    {title}
  {/exp:channel:entries}
{/exp:plugin}

instead of

{exp:channel:entries entry_id="{exp:plugin}{plugin_value}{/exp:plugin}"}
  {title}
{/exp:channel:entries}
GDmac commented 10 years ago

found it :-) Slide 8 https://speakerdeck.com/croxton/stash-core-principles?slide=8 (although IMO using stash:parse is (as we say in NL) shooting a cannon at a fly)

croxton commented 10 years ago

Not really - with parse="inward" EE runs a new instance of the parser for every parameter one by one, whether it has tags in or not. Stash:parse is actually more lightweight since it runs one pass only (by default) and the Stash parser process is more optimised than a full parse by EE.