0

This one kept me busy for a while and I although could find similar questions none of the solutions worked.

Using template partials, gather the page content then send though (via embed) to a master wrapper template. This site has 'pages' where the URL should identify a unique entry, but EE tries it's best to show something, adding require_entry in the Channel tag should trigger no_results if an entry can't be found.

{no_results} works as expected when trying the channel:entries tag (below) outside of stash. Looking at stash docs, unprefix is specifically for this scenario. The below code does work.

{exp:stash:set
    name        = "st_page_content"
    parse_tags  = "yes"
    unprefix    = "my_prefix"
}
    {exp:channel:entries
            channel         = "channel_short_name"
            limit           = "1"
            require_entry   = "yes"
        }

    <p>{title}</p>

    {if my_prefix:no_results}<p>no results</p>{/if}

    {/exp:channel:entries}
{/exp:stash:set}

But it doesn't work when stashing multiple variables between 1 stash tag pair. Eg. no_results is not stored in st_page_content:

{exp:stash:set
    name        = "st_page_content"
    parse_tags  = "yes"
    unprefix    = "my_prefix"
}

    {stash:st_entry_id}{entry_id}{/stash:st_entry_id}

    {stash:st_page_content}

        {exp:channel:entries
                channel         = "channel_short_name"
                limit           = "1"
                require_entry   = "yes"
            }

        <p>{title}</p>

        {if my_prefix:no_results}<p>no results</p>{/if}

        {/exp:channel:entries}

    {/stash:st_page_content}

{/exp:stash:set}

EE 2.9.2
Stash 2.6.1 (Dev)

1 Answer 1

0

I feel the example code in the question should work but it doesn't. Here's a solution. I understand why it does work, but it seems wrong.

{exp:stash:set
    name        = "st_page_content"
    parse_tags  = "yes"
    unprefix    = "my_prefix"
}

    {stash:st_entry_id}{entry_id}{/stash:st_entry_id}

    {stash:st_page_content}

        {exp:channel:entries
                channel         = "channel_short_name"
                limit           = "1"
                require_entry   = "yes"
            }

        <p>{title}</p>

        {if my_prefix:no_results}<p>no results</p>{/if}

        {/exp:channel:entries}

    {/stash:st_page_content}

    {if stash_prefix:no_results}
        {stash:st_page_content}{redirect="404"}{/stash:st_page_content}
    {/if}

{/exp:stash:set}

If EE can't find an entry, anything not inside no_results is discarded. So all that's left inside the stash:set is:

{if stash_prefix:no_results}
    {stash:st_page_content}{redirect="404"}{/stash:st_page_content}
{/if}

And now st_page_content will store just the no_results content. It seems wrong to be setting the same variable twice but due to EEs parse order there's only ever one instance of st_page_content.


Update 15/03/11

After getting feedback from Mark, there's another solution, that's really not that different! https://twitter.com/croxton/status/573949167824953344

that's one way to do it. But you should be able to use {exp:stash:not_found} inside the first conditional to return your 404.

https://github.com/croxton/Stash/wiki/%7Bexp:stash:not_found%7D#advanced-example

I've tried this and it does not work:

{exp:stash:set
    name        = "st_page_content"
    parse_tags  = "yes"
    unprefix    = "my_prefix"
}

    {stash:st_entry_id}{entry_id}{/stash:st_entry_id}

    {stash:st_page_content}

        {exp:channel:entries
                channel         = "channel_short_name"
                limit           = "1"
                require_entry   = "yes"
            }

        <p>{title}</p>

        {if my_prefix:no_results}<p>no results</p>{/if}

        {/exp:channel:entries}

        {if stash_prefix:no_results}
            {exp:stash:not_found}
        {/if}

    {/stash:st_page_content}

{/exp:stash:set}

Putting the {exp:stash:not_found} in the first tag pair ({stash:st_entry_id}) doesn't work either.

But similar to my original solution wrapping {exp:stash:not_found} with the variable tag pair does work. This is exactly the same solution as using {redirect="404"} but maybe EEs redirect tag will cause problems in other circumstances.

Alternative solution:

{exp:stash:set
    name        = "st_page_content"
    parse_tags  = "yes"
    unprefix    = "my_prefix"
}

    {stash:st_entry_id}{entry_id}{/stash:st_entry_id}

    {stash:st_page_content}

        {exp:channel:entries
                channel         = "channel_short_name"
                limit           = "1"
                require_entry   = "yes"
            }

        <p>{title}</p>

        {if my_prefix:no_results}<p>no results</p>{/if}

        {/exp:channel:entries}

    {/stash:st_page_content}

    {if stash_prefix:no_results}
        {stash:st_page_content}{exp:stash:not_found}{/stash:st_page_content}
    {/if}

{/exp:stash:set}

Your Answer

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

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