Include dictionary and context when calling partial in hugo - hugo

I know you can pass variables into a partial as such:
{{ partial "name_of_partial" . }}
^^ calls partial and passes current context
{{ partial "name_of_partial" (dict "imageUrl" .Params.image "title" .Title "subtitle" .Params.subtitle) }}
^^ calls partial and passes "imageURL" as a dictionary with variables.
But what if I wanted to pass the current context and the above dictionary?

I found an answer on https://discourse.gohugo.io/t/accessing-from-partial-that-also-has-a-dict/5162
Summary:
there is currently no way to pass the context i.e. . separate to the dictionary. Workaround is to make . a variable within the dictionary. e.g.
{{ partial "name_of_partial" (dict "imageUrl" .Params.image "title" .Title "subtitle" .Params.subtitle "content" .) }}

Related

Hugo - using index with complex key to get data

Assuming the following urls.toml file in data folder:
[Group]
link = "http://example.com"
[Group.A]
link = "http://example.com"
I know that I can access the link value in Group.A in my shortcode like this:
{{ index .Site.Data.urls.Group.A "link" }}
But, I would like to access the link in a way similar to the following:
{{ index .Site.Data.urls "Group.A.link" }}
The reason for this is to enable me to pass the "Group.A.link" as a parameter to my "url" shortcode within the content markdown like this:
{{< url "Group.A.link" >}}
Otherwise, I won't be able to use nesting for logical organisation in the urls.toml data file.
Thanks in advance.
You can use nested calls of index COLLECTION "key" to narrow your way down.
Meaning,
(index (index (index .Site.Data.urls "Group") "A") "link")
would work given your urls.toml structure.
The trick is making it somewhat dynamic, so you don't need to worry too much about depth.
The snippet below might serve as a potential starting point for a shortcode. However, it doesn't have any safe-guards. I'd recommend to add a few checks to get meaningful errors/warnings if things go wrong.
{{ $path := .Get 0 }}
{{/* split the string to have indices to follow the path */}}
{{/* if $path is "A.B.C", $pathSlice wil be ["A" "B" "C"] */}}
{{ $pathSlice := split $path "." }}
{{ $currentValue := .Site.Data.urls }}
{{ range $pathSlice }}
{{/* recommended homework: check that $currentValue is a dict otherwise handle with defaults and/or warnings */}}
{{ $currentValue = index $currentValue . }}
{{ end }}
<p>et voila: {{ $currentValue }}</p>
After having looked at Hugo's code (Index function) I found a very simple solution.
If we want to pass a complex comma-separated key, all we need to do is split it when calling index. Example:
Using the url shortcode in markdown:
{{< url "Group.A.link" >}}
Code of the url shortcode:
{{ index .Site.Data.urls (split (.Get 0) ".")}}

List all content not matching criteria in Hugo

I'd like to list all the pages that do not have a type of featured. If I want only the featured pages, I would use the following:
{{ range ( where .Site.RegularPages "Type" "featured" ) }}
...
{{ end }}
What's the opposite of that? I know I can list all the pages and then put an if inside of the range, but I'm thinking there must be a way to do it in the range expression itself.
OK...figured this out. The operator needed to be in quotation marks, so this works:
{{ range ( where .Site.RegularPages "Type" "!=" "featured" ) }}
...
{{ end }}
I had tried using the operator without the quotation marks and it kept throwing an error.

is not a field of struct type hugolib.SiteInfo in

I tried adding a new property to my theme/partials/footer.html template, and adding that property to my /config.toml file, but I keep getting the error:
ERROR: 2017/07/09 template: theme/partials/footer.html:16:40: executing "theme/partials/footer.html" at <.Site.CopyrightStart...>: CopyrightStartYear is not a field of struct type *hugolib.SiteInfo in theme/partials/footer.html
Example from my partial template file:
<span>© {{.Site.copyrightStartYear}}</span>
The template engine in Hugo will look for all site params under the [Params] block in the config.toml file (must be a quoted string for this example). These can be referenced via the .Site.Params.<paramName> lookup in partial templates.
e.g.
# config.toml
...
[Params]
myParam = "weeee!"
...
And use it in your HTML fragment:
# somePartial.html
<span>{{ .Site.Params.myParam }}</span>
...

Use variable inside Hugo content

I'm trying to use a variable within the content of a Hugo statically generated site. For example, the content looks like the following:
Go to your site's url ({{ .Site.BaseURL }})
Enter your credentials
.....(blah blah blah)
When this gets rendered, the {{ .... }} part doesn't get processed...it stays the same as I put above. I've tried it with a $ in front as well. Variables within templates seem to work just fine. Do I need to create a shortcode to use within content pages?
So it looks like a shortcode is the way to do this. For what it's worth, I changed the document to look like the following:
Go to your site's url ({{< siteurl >}})
In layouts/shortcodes, I created the file siteurl.html. It looks like the following:
{{ .Page.Site.BaseURL }}
I needed to add .Page in there to get access to the Site variables. See this Issue Report for more details.
In Hugo, When you want to use a variable in markdown (.md) file then you need to create a shortcode for that first.
You can follow these steps:-
create shortcode
layouts/shortcodes/siteurl.html
{{ .Page.Site.BaseURL }}
usage
content/post/myblogpost.md
---
# front-matter
---
1. Go to your site's url ({{< siteurl >}})
2. Enter your credentials
3. .....(blah blah blah)
result
post/myblogpost.html
1. Go to your site's url (https://codingnconcepts.com)
2. Enter your credentials
3. .....(blah blah blah)
Source: https://codingnconcepts.com/hugo/custom-shortcode-hugo/
I had the same problem, and this post helped me.
I wanted to display a site param in my site content, and discovered you cannot use regular templating inside content files.
In the end I created a shortcode to load the requested site param. Who knows this information might help someone.
/config.yml
params:
appName: My app
/content/about.html
<p>My app's name is {{< param "appName" >}}</p>
/layouts/shortcodes/param.html
{{/* Usage: {{< param "siteParamName" }} */}}
{{ index .Site.Params (.Get 0) }}
Result
<p>My app's name is My app</p>
This is an attempt to slightly improve #minitauros answer with a simplistic example to lookup a (site) parameter sub-key (aka walk the YAML tree, infer an element, etc.).
I would like Hugo to have a JSONPath or jq syntax and, obviously, this example is far from competing with either solutions.
config.yml
params:
mode: one
support:
mailing: info#example.net
layouts/shortcodes/param.html
{{ $v := .Site.Params }}
{{ range (split (.Get 0) ".") }}{{ $v = index $v (.) }}{{ end }}
{{ $v }}
content/_index.md
We are in mode {{< param "mode" >}}.
In case of turbulence, [reach the support](mailto:{{< param "support.mailing" >}}) for help.

How to query the number of elements stored in a db.ListProperty in google app engine

The entity class is defined like:
class Item(db.Model):
list = db.ListProperty(db.Key)
What's the attribute or function to return the number of elements stored in a ListProperty, so that I can use something like
{{ item.list.... }}
to display that in a html.
It's a list so you can use len() on it.
total = len(item.list)
or if you are using jinja on your html you can use the count filter
{{ item.list|count }}

Resources