.GetPage from different language

I have a shortcode that is meant to produce a call to action button for a given page. It works for a page that matches the current language, in this case PT.

For example: {{< showpage-large ref="page/iscsp-ced/index.pt.md" col="col-md-6" >}}

If the current language is PT and I try to fetch a page in the default English language, it simply doesn’t return anything.

{{< showpage ref="post/2018-02-23-full-digital-strategy/index.md" col="col-md-6" >}}

Is there a way I can bypass this?

{{ $ref := (.Get "ref") }}
{{ $page := .Site.GetPage $ref }}
{{ $col := (.Get "col") }}
{{ with $page }}
<div class="{{- with $col -}}{{- . -}}{{- else -}}col-md-4{{- end -}}">
	<div class="card card-background card-showpage card-raised" data-background-color="" style="background-image: url('{{- with .Resources.GetMatch "header" }}{{ $image := .Fill "350x283" }}{{ $image.Permalink }}{{ end }}')">
		<div class="info">
			<div class="icon icon-white">
				<i class="now-ui-icons business_bulb-63"></i>
			</div>
			<div class="description">
				<h4 class="info-title"><a href="{{.Permalink}}">{{ .Title }}</a></h4>
				<p>{{ .Params.subtitle }} </p>
				<a href="{{ .Permalink }}" class="ml-3">{{ i18n "read_more" }}</a>
			</div>
		</div>
	</div>
</div>
{{ end }}

I have also tried the methods here, but they don’t seem to work with the most recent Hugo version.

On 69.2, with this in the shortcode:

{{ $ref := (.Get "ref") }}

{{ range .Page.Sites }}
    {{ .GetPage $ref }} <br>
{{ end }}
<hr>

and this structure:

content/posts/
├── post1.la.md
└── post1.md

Used as:

{{< showpage ref="post1" >}}
{{< showpage ref="post1.md" >}}
{{< showpage ref="post1.la.md" >}}

I get these results:

Page(/posts/post1.md)
Page(/posts/post1.la.md)
---
Page(/posts/post1.md)
Page(/posts/post1.la.md)
---
nopPage
Page(/posts/post1.la.md)
2 Likes

Thank you ! That works, I added a check that the ref exists, here’s the final code:

{{ $page := "" }}
{{ range .Page.Sites }}
	{{ with (.GetPage $ref) }}
		{{ $page = (.GetPage $ref) }}
	{{ end }}
{{ end }}

At first I tried to check if $ref and (.GetPage $ref).File matched, that would have been a better fit because it is more explicit. For some reason that method didn’t work although the output of those variables was identical in the HTML.

Maybe one is an object and the other a string?

Yeah, {{ printf "%#T" (.GetPage $ref).File }} returns *hugolib.fileInfo.

You could do (print ((.GetPage $ref).File) to “stringify” the value maybe?

That’s right, it works. At first I was trying to use the string function.

{{ if eq (print (.GetPage $ref).File) $ref }}

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