Need help understanding content organization for product pages

There is a shortcode of sorts included in the Docs to get a .Resources image that @RickCogley asked for and @bep developed. But I haven’t tested it.

But anyway. Time to get sneaky.

Because you see I am moving away from shortcodes and I want to render everything .Resources related right from the template and exactly where I need it.

I tried what you tried. First of all forget about .GetPage a bundled content file is not a page but part of a page.

Second it seems that .GetByPrefix is intended for binaries (imgs) only. It fails in a lovely Go Templates way when invoked within {{ with .Resources.ByType "page" }}

So where does that leaves us? We will get those bloody files rendered by their order using a standard range function.

So @Awestruck (and everyone else)

To render the first bundled content file do this:

{{ range $i, $e := (.Resources.ByType "page") }}
{{ if lt $i 1 }}
<p>{{ .Content }}</p>
{{ end }}
{{ end }} 

And then to render the second bundled file use:

{{ range $i, $e := (.Resources.ByType "page") }}
{{ if eq $i 1 }}
<p>{{ .Content }}</p>
{{ end }}
{{ end }} 

And so on… The rendering order is alphanumeric based on the bundled content’s filename.

Now let me go grab a glass of wine. It’s Saturday night after all. :wine_glass:

What do you mean by the above? GetByPrefix should work fine in most situations – and it returns nil when none found so it works great in “with”.

If you use .GetByPrefix for a bundled content file e.g. content1.md nothing renders and there is no error in the console.

Tested like so with the code from your test repo:

{{ $colors := .Resources.GetByPrefix "content1" }}
{{ with $colors }}
<p>{{ .Content }}</p>
{{ end }}

EDIT

And if it’s included like this:

{{ with .Resources.ByType "page" }}
{{ $colors := .Resources.GetByPrefix "content1" }}
{{ with $colors }}
<p>{{ .Content }}</p>
{{ end }}
{{ end }}

The console throws the following:
<.Resources.GetByPref...>: can't evaluate field Resources in type []resource.Resource

Your code is slightly wrong, but so was mine:

I will fix that in the Monday release, so you can do something ala:

{{ with .Resources.ByType "page" }}
{{ $colors := .GetByPrefix "content1" }}
{{ with $colors }}
<p>{{ .Content }}</p>
{{ end }}
{{ end }}

Thanks for testing my code!

1 Like

Aha! Thank you very much.

@Awestruck this is a bug. You need to wait for the fix in the next release.

Excellent!

Thank you for helping and testing @alexandros and @bep. I look forward to the next release, are these schedule or occur regularly? In the mean time I’ll see if I can do something the sneaky way.

Bring on the wine!

1 Like