Using the "@last" data variable on {{foreach}} statements

If you’re looking for help, please provide information about your environment. If you delete this template and don’t provide any information, your topic will be closed.

If you aren’t running the latest version of Ghost, the first thing we will ask you to do is update to the latest version of Ghost.

And

  • How was Ghost installed and configured? DO Droplet 1-click
  • What Node version, database, OS & browser are you using? Fully upgraded stack to ubuntu 20, etc. etc. Using MacOS with Safari 15
  • What errors or information do you see in the console? not applicable
  • What steps could someone else take to reproduce the issue you’re having? not applicable

Hi there, have a simple secondary menu in the footer. As part of the official ‘edition’ theme. Just trying to customize it a bit. So you’ll see in the menu there is a pip added after each menu item as a separator. But there is also one on the last item. For aesthetics sake I’d like to not have the pip show up after the last menu item.

Is using the foreach loop the right approach? This is what I tried and it failed hard.

{{#unless isSecondary}}
    {{#foreach navigation}}
        <a class="menu-item menu-item-{{slug}}{{#if current}} menu-item-current{{/if}}" href="{{url absolute="true"}}">{{label}}</a>
    {{/foreach}}
{{else}}
{{#foreach navigation}}
        {{#if @last}}
        <a class="menu-item menu-item-{{slug}}{{#if current}} menu-item-current{{/if}}" href="{{url absolute="true"}}">{{label}}</a>
        {{#else}}
        <a class="menu-item menu-item-{{slug}}{{#if current}} menu-item-current{{/if}}" href="{{url absolute="true"}}">{{label}}</a>
        <span class="sep">•</span>
        {{/if}}
    {{/foreach}}

This is the detault that is was before so you can see what I’m working with.

{{#unless isSecondary}}
    {{#foreach navigation}}
        <a class="menu-item menu-item-{{slug}}{{#if current}} menu-item-current{{/if}}" href="{{url absolute="true"}}">{{label}}</a>
    {{/foreach}}
{{else}}
    {{#foreach navigation}}
        <a class="menu-item menu-item-{{slug}}{{#if current}} menu-item-current{{/if}}" href="{{url absolute="true"}}">{{label}}</a>
        <span class="sep">•</span>
    {{/foreach}}
{{/unless}}

Can you try removing the # in your else block?

{{!-- wrong --}}
{{#else}}

{!-- correct --}}
{{else}}
1 Like

Man oh man :sweat_smile: thanks! I really could not see that. Time for new glasses :woozy_face: