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

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.

The with statement rebinds the context (the dot) to .mail, and .mail.url does not exist. Use $ to get to root context.

{{ $myUrl := "" }}
{{ with .mail }}
 {{ $myUrl = print "mailTo:" $.url }}
{{ else }}
 {{ $myUrl = .url }}
{{ end }}

@jmooring

Thanks a lot for your response. I tried your provided code but getting the new error:


execute of template failed at <$.url>: can’t evaluate field url in type []interface {} 

I am getting this error for the following line:


{{ $myUrl = print "mailTo:" $.url }}

Can you please let me know whats wrong here?

I need to see more of your code. Can you share a link to your public repository?

@jmooring
Sorry, but It is not posted anywhere on the public repository. I am using the Docsy theme and following are the data defined within my config.toml:

[params.links]
# Developer relevant links. These will show up on right side of footer and in the community page if you have one.
[[params.links.developer]]
  name = "Example"
  url = "https://www.example.com/"
  mail = false
[[params.links.developer]]
  name = "GitHub"
  url = "https://github.com/example"
  mail = false
[[params.links.developer]]
  name = "Contact us"
  url = "info@example.com"
  mail = true

I am trying to check and add the with condition in my partials/footer.html:

<ul class="list-inline mb-0">
    {{ range . }}
        {{ $myUrl := "" }}
        {{ with .mail }}
            {{ $myUrl = print "mailTo:" $.url }}
        {{ else }}
            {{ $myUrl = .url }}
        {{ end }}
        {{ $myUrl }}
    {{ end }}
  </ul>

@jmooring
Thanks a lot for the response.
I have posted the code in the following GitHub repository: GitHub - Aravinda93/hugo-docsy-learn

I have added the provided code in the following file themes/docsy/layouts/partials/footer.html

This is true, but it occurs while iterating through an array with range (this is the detail missing from your original post). The simplest way to handle this is to use if instead of with because there is no need to rebind the context:

diff --git a/themes/docsy/layouts/partials/footer.html b/themes/docsy/layouts/partials/footer.html
index 5a6e34e..5a1a72b 100644
--- a/themes/docsy/layouts/partials/footer.html
+++ b/themes/docsy/layouts/partials/footer.html
@@ -30,8 +30,8 @@
 <ul class="list-inline mb-0">
   {{ range . }}
   {{ $myUrl := "" }}
-  {{ with .mail }}
-    {{ $myUrl = print "mailTo:" $.url }}
+  {{ if .mail }}
+    {{ $myUrl = print "mailTo:" .url }}
   {{ else }}
     {{ $myUrl = .url }}
   {{ end }}

Please take some time to understand context:
https://www.regisphilibert.com/blog/2018/02/hugo-the-scope-the-context-and-the-dot/

Also, instead of modifying the theme directly, it is better to override the files as needed by placing a copy in the layouts directory in the root of your project.

mkdir -p layouts/partials/
cp themes/docsy/layouts/partials/footer.html layouts/partials/

That way you can update the theme without losing your changes.

1 Like

@jmooring

Thanks a lot for the fix and for providing the link. I will surely go through it and understand the concepts more.

Also, thanks a lot for the advice related to overriding the files. I will make sure to override the files in my project root directory.

1 Like

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.