6

I'm currently wiring together a site that uses the DRY principle and makes heavy use of @croxton Stash module.

All is working as expected but if I purposefully remove part of the URL I cannot force a redirect as I'm using the URL template to capture the info and embedding (last) a template for display.

I'm using the {if {exp:stash:not_empty name="my-stash"}} Show the info {/if} to great affect but when I set {exp:channel require_entry="yes"} AND there is no entry, how do I force a redirect ?

{embed="_includes/_layout_equipment"} // The layout template

//Collect all values for all custom fields
{exp:stash:set parse_tags="yes" trim="yes" parse_conditionals="yes"}
{exp:channel:entries channel="equipment" url_title="{segment_3}" require_entry="yes"}
    {if no_results}Has no effect here{/if}

    {stash:title}{title}{/stash:title} // Get title
    {stash:matrix}{matrix}<li>{value}</li>{/matrix}{/stash:title} Get Matrix Pair
    //repeat for all custom fields in the entry - a lot of them
    {/exp:channel:entries}
{/exp:stash:set}

//Need to SET a host of 'stash's' using the master stash vars captured above

{exp:stash:set name="main" parse_tags etc}
    {if {exp:stash:not_empty name="title"}}
    <h1>{stash:title}</h1>
    {/if}
    <ul>
        {stash:matrix}
    </ul>
{/exp:stash:set}

{exp:stash:set name="sidecontent" parse_tags etc}
  //Pull in other Vars declared in the steps above
{/exp:stash:set}

{exp:stash:set name="tab01" parse_tags etc}
  //Pull in other Vars declared in the steps above
{/exp:stash:set}

// Join all the bits
{exp:stash:set name="contentregion" parse_tags etc}
  <div class="abc">
  {stash:my_var}
  <div class="inner">
  {stash:tabs}
  </div>
  </div>
  <div class="abcde">
  {stash:anothervar}
  {stash:morevars}
{/exp:stash:set}

Then in the Embed template:

<html>
<body>
<div class="content">
{exp:stash:get name="contentregion"}
// By this stage at least one var within this var contains some mark-up - no actual data but a random <div> or <ul>
// ************* WHERE DOES A REDIRECT FIT IF THESE IS NO CONTENT ??? *****//
</div>
<div class="sidecontent">
{exp:stash:get name="sidecontent"}
</div>
</body>
</html>

Apologies for confusion but my embed is quite simple due to the varying nature of the content (tabs, optional tabs, accordions, optional accordions and so on)

I've got all this going on and setting {no_results="redirect"} just fails to trigger

  • Can you post a code snippet(s)? It's kind of hard to fully understand what's going on from your description? – Tom Davies Nov 22 '12 at 8:21
  • 1
    FYI you can highlight code by indenting 4 spaces, or adding backticks ( ` ) around it. – Adrian Macneil Nov 22 '12 at 10:33
12

You should be able to implement redirects once you add the Stash no_results_prefix parameter. For example:

//Collect all values for all custom fields
{exp:stash:set parse_tags="yes" trim="yes" parse_conditionals="yes" no_results_prefix="equipment"}
{exp:channel:entries channel="equipment" url_title="{segment_3}" require_entry="yes"}
    {if equipment:no_results}{redirect="404"}{/if}

    {stash:title}{title}{/stash:title} // Get title
    {stash:matrix}{matrix}<li>{value}</li>{/matrix}{/stash:title} Get Matrix Pair
    //repeat for all custom fields in the entry - a lot of them
    {/exp:channel:entries}
{/exp:stash:set}
| improve this answer | |
  • 4
    This is the correct answer. The reason it is required is because the {if no_results}..{/if} conditional is 'owned' by the outermost tag only; EE has no in-built mechanism to namespace them in nested tags. – Mark Croxton Nov 22 '12 at 13:14
0

Okay so I must confess to not having tested this, but can you not stash the "result" of the no results tag:

{exp:stash:set parse_tags="yes" trim="yes" parse_conditionals="yes"}
    {exp:channel:entries channel="equipment" url_title="{segment_3}" require_entry="yes"}
        {if no_results}
          {stash:no_results}true{/stash:no_results}
        {/if}
        {stash:title}{title}{/stash:title} // Get title
        ...
    {/exp:channel:entries}
{/exp:stash:set}

Then parse that to create a redirect (note needs parse_tag="no"):

{exp:stash:set name="redirect" parse_tags="no"  trim="yes" parse_conditionals="yes"}
    {if {exp:stash:not_empty name="no_results"}}
      {redirect="redirect/path"}
    {/if}
{/exp:stash:set}

Then push that into your embed template:

{exp:stash:set name="contentregion" parse_tags etc}
  {stash:redirect}
  <div class="abc">
  {stash:my_var}
  <div class="inner">
  {stash:tabs}
  </div>
  </div>
  <div class="abcde">
  {stash:anothervar}
  {stash:morevars}
{/exp:stash:set}

A bit long-winded I know, but might meet your use case?

| improve this answer | |
  • Unfortunatly not, I'm running stash around/outside the exp:channel tags and can't switch parse_tags from yes to no :( Trust me I tried ;) – stuartmcd69 Nov 22 '12 at 11:59
  • how about just passing the value of {stash:no_results} into the embed directly (ie as a param to the embed tag) and then doing a simple conditional in the embed template? – Tom Davies Nov 22 '12 at 12:09
0

Put the redirect in the original channel:entries tag so you can use no_results:

{exp:stash:set parse_tags="yes" trim="yes" parse_conditionals="yes"}
    {exp:channel:entries channel="equipment" url_title="{segment_3}" require_entry="yes"}
        {if no_results}{redirect="404"}{/if}
        {stash:title}{title}{/stash:title} // Get title
        ...
    {/exp:channel:entries}
{/exp:stash:set}

EDIT...

Sorry I should have tested first. Try adding parse="inward" to the original stash:set tag - that works for me.

{exp:stash:set parse_tags="yes" trim="yes" parse_conditionals="yes" parse="inward"}
    {exp:channel:entries channel="equipment" url_title="{segment_3}" require_entry="yes"}
        {if no_results}{redirect="404"}{/if}
        {stash:title}{title}{/stash:title} // Get title
        ...
    {/exp:channel:entries}
{/exp:stash:set}

The alternative would be to put the stash:set on the inside of the channel:entries tag, with the no_results conditional outside the stash:set block... That works too, however it would prevent you from easily doing the next part where you're calling up those stashed variables and skewering them back together in a different order since the vars will not be set at that point... parse="inward" gets around that I think.

| improve this answer | |
  • No go, the first stash/channel item merely collects the data and placing in a no_results / redirect does nothing as I believe the actual embed template needs to handle that. – stuartmcd69 Nov 22 '12 at 10:51
  • No, the {if no_results} doesn't go in the embedded template. Check out this from Mark which has a switchee_no_results tag inside a stash:set_list tag. It doesn't matter that it's switchee_no_results, it's just because he's also using Switchee in that example. – Tyssen Nov 22 '12 at 11:02
  • Edited my answer with an alternative approach – James Smith Nov 22 '12 at 12:20
  • Another no-go unfortunately, I was most excited as that parse has worked for me in previous scenarios but not this time, I think I'll wrap it up for tonight and look at it with fresh eyes. Thx so much for your time & suggestions thus far. – stuartmcd69 Nov 22 '12 at 12:34
  • Hmm, this works ok for me, though I think it might also be falling back to my main site index (home page) template, which has an additional check for {if segment_1}{redirect="404"}{/if} – James Smith Nov 22 '12 at 13:00

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