hucste
August 27, 2020, 2:31pm
1
First, very good idea!
I read this topic:
The docs are a bit unclear to me.
says:
In order to return a value, a partial must include a lone return statement.
A lone return statement is AFAIK:
{{ return }}
However the examples show
{{ return $value }}
(which makes more sense)
The docs also say (emphasis mine):
Only one return statement is allowed per partial file.
This would imply that –
you cannot have {{ if ... }}{{ return $this }}{{ else }}{{ return $that }}{{ end }}
you cannot have multiple inline partials that ret…
But, I’ve one question:
It’s possible to call in other partial?
By instance, with this code:
{{* partial "return-description" *}}
{{ $desc := "" }}
{{ with .Description }}{{ $desc := printf `content=%q` . | safeHTMLAttr }}
{{ else }}
{{ if .IsPage }}{{ $desc := printf `content=%q` .Summary | safeHTMLAttr }}
{{ else }}
{{ with .Site.Params.description }}{{ $desc := printf `content=%q` . | safeHTMLAttr }}{{ end }}
{{ end }}
{{ end }}
{{ return $desc }}
Yes, you can nest partial calls.
markdown
Answer: {{< foo 6 >}}
layouts/shortcodes/foo.html
{{ partial "mul-7.html" (.Get 0) }}
layouts/partials/mul-7.html
{{ return (partial "div-21.html" (mul 7 .)) }}
layouts/partials/div-21.html
{{ return (div . 21) }}
rendered html
<p>Answer: 2</p>
hucste
August 27, 2020, 5:21pm
3
Ok, but into your example, you use shortcode.
and about template?
If I include the code on partial (showed on my first port), name “return-decription.html”, on partial “meta.html”, as:
<meta name="description" {{ with partial "return-description.html" . }}{{ $desc }}{{ end }}>
I’ve not any result.
What’s wrong?!
Calling a partial from a template vs. calling a partial from a shortcode – no difference.
Partials return a value , not a variable. Additionally, with
“rebinds the context ( .
) within its scope.”
So don’t do this:
{{ with partial "return-description.html" . }}{{ $desc }}{{ end }}
Do this:
{{ with partial "return-description.html" . }}{{ . }}{{ end }}
1 Like
hucste
August 27, 2020, 5:37pm
5
OK, you reassure me!
empty!
<meta name="description" >
If I write:
<meta name="description" {{ return ( partial "return-description.html" . ) }}>
No code is displaying!
I can help you if share your repository.
Please post a link to the public repository for your site. See:
https://discourse.gohugo.io/t/requesting-help/9132
Include a link to the source code repository of your project, because we really need the context of seeing your templates and partials to be able to help you. It is trivial to do a quick git clone
on your repo, then run hugo server
in your project, to help you out. On the other hand, recreating your code from screenshots, or sort of guessing at it, is not.
If you can’t share your repository for whatever reason, consider creating a dummy repo that you can share, which reproduces the problem you’re experiencing.
hucste
August 27, 2020, 5:48pm
7
I Known, but i’m on the local development.
I go to push this test code.
See https://golang.org/pkg/text/template/#hdr-Variables . You are initializing variables instead of assigning value. Use :=
to initialize, and =
to assign value. Initialize once, assign value as needed.
2 Likes
I think a careful read of these resources will be helpful:
1 Like
system
Closed
August 29, 2020, 6:09pm
10
This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.