Hugo/Docsy: execute of template failed at <.url>: can’t evaluate field url in type bool - html

I am developing a static web application using Hugo with Docsy theme. I would like to add an condition within Docsy Partials code where I would like to append the mailTo: word to my .url if the .mail is set to true, when I try to do this then I get the following error:
/themes/docsy/layouts/partials/footer.html:36:34": execute of template failed at <.url>: can’t evaluate field url in type bool
Following is the code I am adding to my partials:
{{ $myUrl := "" }}
{{ with .mail }}
{{ $myUrl = print "mailTo:" .url }}
{{ else }}
{{ $myUrl = .url }}
{{ end }}
{{ $myUrl }}
If I add some test then everything would work perfectly:
{{ with .mail }}
TRUE
{{ else }}
FALSE
{{ end }}
I am quite new to the Hugo and Docsy theme so finding difficult to understand and fix it. Any help would be really appreciated.

You are not using with correct here: https://gohugo.io/functions/with/. I think you should use if here.

Related

SaltStack check if grain exists in Jinja file

I'm using SaltStack to manage my infra. Machines are hosted in different DCs, so they also have slightly different network setup.
Currently, I'm running into the following issue:
Comment: Unable to manage file: Jinja variable 'dict object' has no attribute 'macaddress'; line 9
---
[...]
ethernets:
{{ grains['interface_context'] }}:
dhcp4: {{ grains['dhcp4'] }}
dhcp6: {{ grains['dhcp6'] }}
addresses: [{{ grains['ipv4'] }}, "{{ grains['ipv6'] }}"]
{% if grains['macaddress'] %} <======================
match:
macaddress: {{ grains['macaddress'] }}
{% endif %}
routes:
- to: default
[...]
---
As the message indicates, the grain "macaddress" is missing, which I can confirm, it's not set for this minion. But What I do not understand is how I can simply check if this variable/grain exists at all within a jinja template?
I wouldn't expect this error to come up, as I actually wanted to catch it with the if statement.
Can somebody help?
Use get to return None instead of raising:
{% if grains.get('macaddress') is not none %}
Or if you want to treat "empty" values the same:
{% if not grains.get('macaddress') %}

jekyll find filename portion of page url

I need to extract the filename portion of the page url, that is from a post saved in 2011-12-31-new-years-eve-is-awesome.md I would like to just the part new-years-eve-is-awesome .
Unfortunately post_url contains also the directory tree 2011/12/31/
This page https://jekyllrb.com/docs/permalinks/ seems to suggest that defining
---
shorttitle: :title
---
in the front matter should work but that produces an empty string from {{ post.shorttitle }}
Here is my solution so far. Perhaps there is a variable left in the code but it's not documented, therefore I just filter the post URL:
{% assign spliturl = post.url | split: '/' %}
{% assign postname = spliturl[4] | replace: '.html', '' %}
{{ postname }}

Using {{ myvariable }} in Nunjucks just prints "{{ myvariable }}" in final HTML file

I use Metalsmith with Nunjucks in a static site generator. In the project, I am importing a Nunjucks macro into a file and trying to invoke it using {{ someMacro }}. However, in the resulting HTML file, "{{ someMacro }}" gets printed as a string instead of the markup inside the macro.
I've also tried {% set someVariable = "abc" %}, and invoke the variable with {{ someVariable}}, both in the same file, but I get the same issue with "{{ someVariable }}" being printed right into HTML.
I've tried invoking both inside in a {% block xyz%} {% endblock %}. Any ideas?
I found my own answer. In the project I'm working on, we use Metalsmith in-place, and there our engineOptions looks like this:
const templateConfig = {
engineOptions: {
filters: {},
tags: {
variableStart: '<<<',
variableEnd: '>>>',
}
}
};
Instead of using {{ }} to use variables or macros, I just had to use <<< >>> to call variables or macros in the main file, and likewise when passing arguments into macros in macro files.

default jekyll metadata setting?

when I create the port on Jekyll admin
title: layout:
page comments: true
social-share: true
show-avatar:true
use_math: true
I want to create this as default how should I do it?
Copy paste into your new post. Most CMS editors do that for you (like CloudCannon).
The real solution would be to transform them into site variables, by adding them to _config.yml and calling them using {{ site.social-share }} instead of {{ page.social-share }}.
An advanced solution would be to check if there are page variables, like this:
{% if page.social-share != nil %}
{{ page.social-share }}
{% else %}
{{ site.social-share }}
{% endif %}

In Jekyll, how do I render custom metadata for a collection?

I have the following in my _config.yml file:
collections:
nr_qa:
output: true
permalink: /:collection/:name
title: 'Node-RED Questions and Answers'
descriptions: 'Node-RED is a flow-based (visual) programming tool. These pages have some information that may be currently missing from the documentaiton.'
github_pages:
title: 'GitHub Pages and Jekyll/Liquid'
description: 'Hints and tips on using Jekyll for publishing to GitHub Pages.'
output: true
permalink: /:collection/:name
and I want to create an automatic index for my collections. So I use code like this:
## {{ site.collections.github_pages.title }}
{{ site.collections.github_pages.description }}
<ul>
{% for item in site.github_pages %}
<li>
{{ item.title | replace:'_',' ' }}
<p>{% if item.description %}
{{ item.description }}
{% else %}
{{ item.excerpt | strip_html }}
{% endif %}</p>
</li>
{% endfor %}
</ul>
And yes, I know I've rather mixed up my markdown and html there. Not relevant to this question.
The problem is that {{ site.collections.github_pages.title }} and {{ site.collections.github_pages.description }} don't render anything even though I think they should.
Can anyone point out my mistake please?
The problem is that title and description should be included in each collection, and not in _config.yml.
Check out Accessing Collection AttributesPermalink for further details.
update
title can be present in each collection metadata in _config.yml. The problem is how you are accessing those variables.
One approach is to have a specific layout for each collection, then you can access them like:
{% assign col = site.collections | where: 'label','github_pages' | first%}.
TITLE: {{ col.title }}.
DESCRIPTION: {{ col.description }}.