Reputation: 129
The following code is supposed to add an <h1>
for every page in PDF output. Basically, it steps through the YAML file that defines my guide and, if the URL of the current page matches the URL in the YAML block, it places the title
key of the parent block within an <h1>
tag.
{% assign sidebar = site.data.sidebars[page.sidebar].entries %}
{% for entry in sidebar %}
{% for folder in entry.folders %}
{% if folder.title and folder.type != "navi" and folder.type != "frontmatter" %}
{% for folderitem in folder.folderitems %}
{% if folderitem.url == page.url %}
<h1 class="post-title-main" id="{{page.permalink | replace: '/', '' }}">{{ folder.title }}</h1>
{% endif %}
{% endfor %}
{% endif %}
{% endfor %}
{% endfor %}
The problem is that the current code does this for every page, like this:
<h1>The Title I Want to Put in the H1</h1>
<h2>The Title of the Page Which I *Do* Want the <H1> to Appear Above</h2>
. . .
<h1>The Title I Want to Put in the H1</h1>
<h2>The Title of the Page Which I *Don't* Want the <H1> to Appear Above</h2>
I need another conditional check, something like "if folderitem
is the first one" that would let the rest of the conditional logic proceed only if the item is the first in the list, resulting in:
<h1>The Title I Want to Put in the H1</h1>
<h2>The Title of the Page Which I *Do* Want the <H1> to Appear Above</h2>
What is the syntax for selecting the first item? first
appears to be for returning the first item of an array, which doesn't seem to apply to checking if the current item is the first item in the YAML data structure that I'm working with.
Here's an example YAML structure:
entries:
- title:
pdftitle: foobar.pdf
product:
version:
folders:
- title:
output: pdf
type: frontmatter
folderitems:
- title:
url: /titlepage.html
output: pdf
type: frontmatter
- title: My Amazing Guide
output: web
type: navi
folderitems:
- title: Home
url: /index.html
output: web
- title: The Title I Want to Put in the H1
url: /section/page/index.html
output: web, pdf
folderitems:
- title: The Title of the Page Which I *Do* Want the <H1> to Appear Above
url: /section/page/some-page.html
output: web, pdf
- title: The Title of the Page, Which I *Don't* Want the <H1> to Appear Above
url: /section/page/another-page.html
output: web, pdf
Upvotes: 1
Views: 291
Reputation: 129
I figured it out. Instead of
{% if folderitem.url == page.url %}
I was able to use
{% if folderitem.url == page.url and forloop.first == true %}
This additional condition does, in fact, check precisely what I wanted to check—whether the array item is first. Ironically (because I'm writing documentation?), the problem is that the Liquid documentation on https://shopify.github.io/liquid/ is incomplete, whereas the Liquid documentation on https://shopify.dev/api/liquid/ is far more comprehensive.
Here is the documentation on forloop.first
. It is here that I also learned that I could use properties like forloop.index
(1-based), forloop.index0
, and so forth.
Upvotes: 1
Reputation: 5541
Idea from: jekyll/liquid: given key access value from hash in template
Could not test it but could something like this help you in your loop?
{% assign first_url= site.data.sidebars[page.sidebar].entries | where: "url", page.url | first %}`
Upvotes: 1