Correct way to access file path in multilingual leaf bundle

I want to add an id="contact" to the <article> element of my single template to apply only to one leaf bundle /pages/contact". This leaf bundle has both the index.md file and the index.[lang].md file. What’s the correct way to call this path? I tried to define {{ $contact := .GetPage "/pages/contact" }} then doing <article {{ if $contact }}id="contact" {{ end }} > but then the ID appears in every page.

If it has to be in the <article> you might need a custom template in layouts.

You could try adding the ID to the heading in the markdown content or create a shortcode for use in the markdown content.

Heading IDs

Not really.

Currently doing this, but trying to avoid it altogether.

{{ with .File }}
  {{ if eq .ContentBaseName "contact" }}
    ...
  {{ end }}
{{ end }}

This appears to work. Just wondering if it is correct.

    {{- $const := slice }}
    {{- $contact := slice }}
    {{- with .File }}
      {{- if eq .ContentBaseName "support" }}
      {{- $support = . }}
      {{- else if eq .ContentBaseName "contact" }}
      {{- $contact = . }}
      {{- end }}
    {{- end }}

<article {{ if $contact }}id="contact" {{ end }} >
...
</article>

No. You are setting assigning the page’s .File structure to $support and $contact.

Refactor to something like

{{ $id := "" }}
{{ with .File }}
  {{ if and (eq .Dir "pages/") (eq .ContentBaseName "contact") }}
    {{ $id = "contact" }}
  {{ end }}
{{ end }}

<article id="{{ $id }}"></article>

Note: added the .Dir comparison just in case you have another contact page somewhere else…

I want to avoid showing empty ID in other pages, hence why I need to wrap the whole code inside the conditional.

Just wrap it…

<article {{ with $id -}} id="{{ . }}" {{- end }}></article>

I didn’t know that was possible. Gotta check the docs on custom variables again.

Correction to white space removal. Use this instead:

<article {{- with $id }} id="{{ . }}" {{- end }}></article>

Also, check with documentation. The behavior applies to any value.

I have been using it, but not in this way before.

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.