GetPage Information using build url

Hi.

⇒ My git

⇒ My hugo version:

$ hugo version
hugo v0.122.0+extended linux/amd64 BuildDate=2024-02-04T14:35:47Z VendorInfo=debian:0.122.0-1

On my index.htm (home page), I retrieve information from yaml files on my data directory, segun language.lang value, as instance debian.yaml file.

My concerned code:

<div class="content-width-80 pure-g">{{ $site := index .Site.Data.promote .Site.Language.Lang }}
    {{ range $site }}{{ $os := .name | markdownify }}
    <div class="is-white pure-u-1 pure-u-md-1-2 pure-u-lg-1-3" id="section-{{ $os }}">
        <h2>{{ $src := printf "%s%s%s" "/svg/" $os ".svg" }}
            <svg class="svg-48">{{ partial "utils/svg" (dict "src" $src) }}</svg>
            <span aria-hidden="true" class="hidden" hidden>{{ $os | strings.FirstUpper }}</span>
        </h2>
        {{ range index .tag }}
        <h3>{{ .h3}}</h3>
            {{ range index .dl }}
        <dl>
            <dt>{{ .dt }}</dt>
                {{ range index .dd }}{{ $path := printf "%s%s%s%s%s" "/sys/" $os "/" .url "/"  | relLangURL | urlize }}{{ $opts := dict "path" $path  }}
                    {{ $page := .Site.GetPage $path }}Page: {{ $page }}
                <dd><a href="{{ $path }}">{{ .txt }}</a></dd>
                {{ end }}
        </dl>
            {{ end }}
        {{ end }}
    </div>
    {{ end }}

See: doc.huc.fr.eu.org/index.html at f6b08fa4e9a05e747fe3657c21a883400d417692 - doc.huc.fr.eu.org - [Stéphane HUC :: gIT Log]

On line 64, I build a $path.
The aim is to obtain information about the page in question, mainly the title,
(this would allow me to avoid having a ‘txt’ key on my yaml file, same value than .Title)

I also tried to build the variable by putting “.md” in place of the final “/”.

I’m trying .GetPage or .Site.GetPage but it’s not working.
Egual, I tried to use ref, refrel.

What I don’t understand?

Debug your code using the warnf function to print the value of $path. You’ll see this:

WARN  /fr/sys/debian/unbound-dnssec-trigger/
WARN  /fr/sys/debian/add-apt-key/
WARN  /fr/sys/debian/unstable-sid/
WARN  /fr/sys/debian/mfp-epson/
WARN  /fr/sys/debian/mfp-scanner-epson/
WARN  /fr/sys/debian/nvidia-optimus-bumblebee/

Those are relative URLs. The .Site.GetPage and .Page.GetPage methods need a content path, not a URL. Either of these work:

/sys/debian/unbound-dnssec-trigger
/sys/debian/unbound-dnssec-trigger.md

Also, consider using path.Join instead of printf.

1 Like

Ok, for path.Join: usefull (I see too urls.JoinPath)

But:

{{ range index .dd }}
                    {{ $path := path.Join "/sys" $os .url }}{{ warnf $path }}
                    {{ with .Site.GetPage $path }}PageTitle: {{ .Title }}{{ end }}
                <dd><a href="{{ $path }}">{{ .Title }}</a></dd>
                {{ end }}

warnf display:

WARN  /sys/debian/unbound-dnssec-trigger
WARN  /sys/debian/add-apt-key
WARN  /sys/debian/unstable-sid
WARN  /sys/debian/mfp-epson
WARN  /sys/debian/mfp-scanner-epson
WARN  /sys/debian/nvidia-optimus-bumblebee

But nothing is displayed for: {{ with .Site.GetPage $path }}PageTitle: {{ .Title }}{{ end }}
(egual if I used .Page.GetPage)

.Site is not in context!

1 Like

Ahhh! Yes!
Thanks.

{{ with $.Site.GetPage $path }}PageTitle: {{ .Title }}{{ end }} runs! :smiley:

This is handy:
https://gohugo.io/functions/global/site/

1 Like

To simplify your templates, use the global site function regardless of whether the Site object is in context.

Excellent!

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