-
-
Notifications
You must be signed in to change notification settings - Fork 109
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Change the approach for {last} to be more like {else} on {foreach} loops, so that it would not be affected by {skipIf} #375
Comments
Anyway, for anyone who happens on this issue, one way to achieve this is to add a flag (or counter) and not use {var bool $have_data = false}
{foreach $page_list as $slug => $page}
{skipIf ((! $user->is_admin()) && ($page->is_draft()))}
{first}<h1>List of Pages</h1><ul>{/first}
{var bool $have_data = true}
<li><a href={$slug}>{$page->title}</a></li>
{else}
<div class="alert">Oops! No Pages Found!</div>
{/foreach}
{if $have_data}</ul>{/if} Be sure to initialize the flag OUTSIDE the foreach or else you will have an error if everything got Another way is to redesign this to be more like the example in official documentation. <h1>List of Pages</h1><ul>
{foreach $page_list as $slug => $page}
{skipIf ((! $user->is_admin()) && ($page->is_draft()))}
<li><a href={$slug}>{$page->title}</a></li>
{else}
<li>Oops! No Pages Found!</li>
{/foreach}
</ul> But as you can see, the error message is now an entry in the Maybe for the time being, this can be noted in the documentation about using |
Opening tags in places where they are not closed (like To prevent the error message from being output inside the {try}
<h1>List of Pages</h1>
<ul>
{foreach $page_list as $slug => $page}
{skipIf ((! $user->is_admin()) && ($page->is_draft()))}
<li><a href={$slug}>{$page->title}</a></li>
{else}
{rollback}
{/foreach}
</ul>
{else}
<div class="alert">Oops! No Pages Found!</div>
{/try} |
It's not possible to modify the behavior of |
Version: 3.0.18
So I'm trying to have a simple index listing of pages.
Now if it so happens that the last page in
$page_list
is a draft, and the user is not an admin, then the{last}
never gets executed. Note that this also happens if we use{continueIf}
instead.A few observations:
{first}
came before the{skipIf}
then of course{first}
would get executed even if everything was skipped - which is expected and correct behaviour.{last}
before the{skipIf}
then it would work, but if the last page is a "visible" page, then the<ul>
would appear before the actual link - which is also expected and correct behaviour, even if that's not what we want.I know this is not really a Latte specific problem - it's the way loops are handled/done in the programming language. One could talk about setting a flag or counter to indicate if there is or isn't any data (and so the
<ul>
needs to be closed). Or maybe rejig the whole thing with more{if}
statements instead. Or redesign the HTML so we do not depend on{first}
and{last}
to handle the<ul>
tag.But I'm just wondering if there is an easier, Latte-specific way (with nice tools like
{skipIf}
,{first}
,{last}
and{else}
) to make this sort of thing happen?I feel that for
{last}
, if we use a similar approach to handling{else}
, we can execute{last}
on the last iteration (even if it was{skipIf}
-ed), as long as there was some data that wasn't{skipIf}
-ed earlier.Another approach, or another way to think about it, is to define a
{header}
and{footer}
(for lack of a better word) that is executed before the first data in the loop (if there is data after any{skipIf}
), and after the last data in the loop (if there was data). I think such a thing would be very useful for many common scenarios where we need to loop through stuff and display them.Ref #263 points to a similar issue, but affecting
{sep}
instead. I admit I think that is an even more complex problem to solve because one needs to look AHEAD in the loop to decide if there is or isn't another item after this (hence needing a{sep}
). Maybe that requires an advanced version ofimplode()
.The text was updated successfully, but these errors were encountered: