Hi, I’m picking up some data from a data file (or possibly front matter?) and am trying to solve this in a generalized fashion…
we have markdownify and plainify to take the context of a variable and convert it, but I don’t see a “hugoify” or “eval” function.
I want to be able to do
data/stuff.yaml:
a: "Some regular text."
b: "{{ ref . `about` }}"
c: "this is a title -- {{ .Title }}"
and then in a theme later:
{{ with .Site.data.stuff.yaml }}
Your slogan is {{ .a }} <br>
It can be found at {{ .b }}<br>
{{ .c }}
{{ end }}
It would currently generate:
Your slogan is: Some regular text.
It can be found at: {{ ref . `about` }}
This is a title {{ .Title }}
When I want to generate:
Your slogan is: Some regular text.
It can be found at: localhost://my-about-page/
This is a title -- The Title of this Page Context
Again, this needs to be a general solution, NOT a type/field specific solution.
Is this possible?
For full context, I’m auto-generating schema.org compatible JSON+LD data at
timer-hugo/seo_schema.html at master · shockwavefound/timer-hugo · GitHub
pleasantone:
Is this possible?
No, it is not.
You could do something like this, but I wouldn’t…
a:
type: string
content: Some regular text.
b:
type: pageReference
content: "/about"
c:
type: pageProperty
content: title
I came up with an eval function a while ago, but I only use for testing and some documentation:
https://discourse.gohugo.io/t/eval-function/32148/5
1 Like
Thanks, I’m sorry I missed the earlier thread, not googled enough.
Best,
Paul
jmooring:
an eval function
I don’t think that will do what you want… it doesn’t handle action delimiters {{ }}
. You could probably do some regex magic to extract the action contents.
yes, I was thinking this would be a good test of the findRESubmatch code
So, if you’re gonna do it, use this instead of what was posted way back when:
layouts/partials/eval.html
{{ $f := printf "%d.txt" now.UnixNano }}
{{ $r := printf "{{ %s }}" . | resources.FromString $f }}
{{ $r = $r | resources.ExecuteAsTemplate $f . }}
{{ return $r.Content }}
Example:
{{ $s := slice "add 5 1" "sub 6 2" "mul 7 3" "div 8 4"}}
{{ range $s }}
{{ $r := partial "eval" . }}
{{ printf "<pre>%s = %s</pre>" . $r | safeHTML }}
{{ end }}
Result:
add 5 1 = 6
sub 6 2 = 4
mul 7 3 = 21
div 8 4 = 2
I had to use now.UnixNano
to get the correct result on a multilingual site, but it works fine with a monolingual site as well.
1 Like
system
Closed
February 1, 2023, 12:27am
7
This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.