(Cont.) Shortcode “Container with Title” & if parm exists Help

I am trying to make a shortcode that wraps some text into a div container that has styling applied to a title heading and the paragraph text.

The goal here is that I want to check for a “url” and a “title” parameter in the shortcode tag and if both exists then style the header and text. If no “title” parameter exists I just want to style the text that is wrapped in the shortcode.

I posted a similar request and someone pointed me to get familiar with hugo shortcode templates, which I did. The shortcode works great when the parameters are included, but if a parameter does not exist I get some go related debugging information in the output of the hugo server:

Building sites … panic: reflect: call of reflect.Value.Int on string Value [recovered]
        panic: reflect: call of reflect.Value.Int on string Value

goroutine 521 [running]:
text/template.errRecover(0xc042b9d7f0)
        /usr/local/go/src/text/template/exec.go:143 +0x1c1
panic(0xdca320, 0xc0437867a0)
        /usr/local/go/src/runtime/panic.go:502 +0x237
reflect.Value.Int(0xd91b20, 0xc04542cb30, 0x98, 0x0)
        /usr/local/go/src/reflect/value.go:931 +0x12e
github.com/gohugoio/hugo/tpl/collections.(*Namespace).IsSet(0xc042484620, 0xd86a80, 0xc04297c400, 0xd91b20, 0xc04542cb30, 0x0, 0xc042b9cd60, 0x4117ec)
        /go/src/github.com/gohugoio/hugo/tpl/collections/collections.go:322 +0x123
github.com/gohugoio/hugo/tpl/collections.(*Namespace).IsSet-fm(0xd86a80, 0xc04297c400, 0xd91b20, 0xc04542cb30, 0x0, 0x0, 0x0)
        /go/src/github.com/gohugoio/hugo/tpl/collections/init.go:90 +0x59
reflect.Value.call(0xdce4a0, 0xc0421a1cd0, 0x13, 0xec43ab, 0x4, 0xc042ab4ed0, 0x2, 0x2, 0xeb9100, 0xde2401, ...)
        /usr/local/go/src/reflect/value.go:447 +0x970
reflect.Value.Call(0xdce4a0, 0xc0421a1cd0, 0x13, 0xc042ab4ed0, 0x2, 0x2, 0x10a0520, 0xc042737b00, 0xd91b20)
        /usr/local/go/src/reflect/value.go:308 +0xab
text/template.(*state).evalCall(0xc042b9d770, 0xe15940, 0xc0428b19a0, 0x16, 0xdce4a0, 0xc0421a1cd0, 0x13, 0x10a02a0, 0xc042748180, 0xc04218d591, ...)
        /usr/local/go/src/text/template/exec.go:667 +0x4f8
text/template.(*state).evalFunction(0xc042b9d770, 0xe15940, 0xc0428b19a0, 0x16, 0xc0427481b0, 0x10a02a0, 0xc042748180, 0xc042737b40, 0x3, 0x4, ...)
        /usr/local/go/src/text/template/exec.go:535 +0x17d
text/template.(*state).evalCommand(0xc042b9d770, 0xe15940, 0xc0428b19a0, 0x16, 0xc042748180, 0x0, 0x0, 0x0, 0x1920558, 0x452290, ...)
        /usr/local/go/src/text/template/exec.go:432 +0x51d
text/template.(*state).evalPipeline(0xc042b9d770, 0xe15940, 0xc0428b19a0, 0x16, 0xc0425a0a50, 0xc042580c00, 0x0, 0x15c9c00)
        /usr/local/go/src/text/template/exec.go:405 +0x116
text/template.(*state).walkIfOrWith(0xc042b9d770, 0xa, 0xe15940, 0xc0428b19a0, 0x16, 0xc0425a0a50, 0xc04273f770, 0xc04273f860)
        /usr/local/go/src/text/template/exec.go:260 +0xc3
text/template.(*state).walk(0xc042b9d770, 0xe15940, 0xc0428b19a0, 0x16, 0x10a03a0, 0xc042737d00)
        /usr/local/go/src/text/template/exec.go:236 +0x315
text/template.(*state).walk(0xc042b9d770, 0xe15940, 0xc0428b19a0, 0x16, 0x10a03e0, 0xc04273f710)
        /usr/local/go/src/text/template/exec.go:239 +0x124
text/template.(*Template).execute(0xc042737ac0, 0x1092f40, 0xc0469be700, 0xe15940, 0xc0428b19a0, 0x0, 0x0)
        /usr/local/go/src/text/template/exec.go:194 +0x1ed
text/template.(*Template).Execute(0xc042737ac0, 0x1092f40, 0xc0469be700, 0xe15940, 0xc0428b19a0, 0xc0427a6200, 0x460247)
        /usr/local/go/src/text/template/exec.go:177 +0x5a
html/template.(*Template).Execute(0xc04273f6b0, 0x1092f40, 0xc0469be700, 0xe15940, 0xc0428b19a0, 0x42b050, 0x8)
        /usr/local/go/src/html/template/template.go:122 +0x93
github.com/gohugoio/hugo/tpl.(*TemplateAdapter).Execute(0xc043786700, 0x1092f40, 0xc0469be700, 0xe15940, 0xc0428b19a0, 0x0, 0x0)
        /go/src/github.com/gohugoio/hugo/tpl/template.go:94 +0x7d
github.com/gohugoio/hugo/hugolib.renderShortcodeWithPage(0x10968e0, 0xc043786700, 0xc0428b19a0, 0x0, 0x0)
        /go/src/github.com/gohugoio/hugo/hugolib/shortcode.go:759 +0xcd
github.com/gohugoio/hugo/hugolib.renderShortcode(0xc04242f80e, 0x2, 0xec4527, 0x4, 0xec510f, 0x4, 0xc042ba68a0, 0x25, 0xc0427a4d20, 0x0, ...)
        /go/src/github.com/gohugoio/hugo/hugolib/shortcode.go:361 +0x549
github.com/gohugoio/hugo/hugolib.prepareShortcodeForPage.func1(0xc042a99000, 0xe3fd20, 0xc042a990c0, 0xc0431ce240)
        /go/src/github.com/gohugoio/hugo/hugolib/shortcode.go:281 +0x5e
github.com/gohugoio/hugo/hugolib.(*shortcodeHandler).executeShortcodesForDelta(0xc0426e5720, 0xc042bfe0d0, 0x1b, 0xc042e4fd98)
        /go/src/github.com/gohugoio/hugo/hugolib/shortcode.go:442 +0xc8
github.com/gohugoio/hugo/hugolib.handleShortcodes(0xc042bfe0d0, 0xc042d2e000, 0xf04, 0xf04, 0xf04, 0xf04, 0xc042e4ff50, 0xc042840900, 0xc04231d760)
        /go/src/github.com/gohugoio/hugo/hugolib/hugo_sites.go:597 +0x210
github.com/gohugoio/hugo/hugolib.(*Page).prepareForRender(0xc0430c4000, 0xf789c0, 0xc0429d0b70)
        /go/src/github.com/gohugoio/hugo/hugolib/page.go:1268 +0x131
github.com/gohugoio/hugo/hugolib.(*Page).initContent.func1.1(0xc0430c4000, 0xc0453d97a0)
        /go/src/github.com/gohugoio/hugo/hugolib/page.go:303 +0x91
created by github.com/gohugoio/hugo/hugolib.(*Page).initContent.func1
        /go/src/github.com/gohugoio/hugo/hugolib/page.go:298 +0xf4

I was wondering if someone can point me to the right place to debug this. I have no idea how to debug these kinds of errors. My github site is here and the shortcode is:

alert.html.

<div class="alert-container">
  <h4>
    {{ if isset .Params "url" }}
      <a href="{{ .Get "url" }}">{{.Get "title"}}</a>
    {{ else if (isset .Params "title") }}
      {{.Get "title"}}
    {{ else }}
      no title
    {{ end }}
  </h4>
  {{ .Inner }}
</div>

The page I am using the shortcode is at the top of my about.md page:

{{% alert title="My response to the COVID-19 Crisis" url="/updates/update-covid-19" %}}Due to my particular field, being an essential service to the public, I am remaining in operation. I am a one man shop in isolation, and I operate by appointment, which allows me to control the stream of customers, I am taking every precaution necessary to prevent the spread of this virus[...more](/updates/update-covid-19){{% /alert %}}


{{% alert title="We Have the Right to Repair Everything We Own" url="https://www.ifixit.com/Right-to-Repair/Intro" %}}
  Once you’ve paid money for a product, the manufacturer shouldn’t be able to dictate how you use it—it’s yours. Ownership means you should be able to open, hack, repair, upgrade, or tie bells on it.
Support sane [copyright legislation](https://www.fightforthefuture.org/fixcopyright/).{{% /alert %}}

{{% alert %}} Are you being affected by the Northern California fires? If you are a first responder for the Northern California fires, or a victim of a <a href="updates/update-fema-scam">FEMA Scam</a>, or need help <a href="services/data/recovery">recovering lost, or damaged data</a>, I can help. <a href="#contact">Contact me</a> to setup a free evaluation of your situation. First Responders & Fire victims receive 25% off, use the FIRECODE25 when paying and I will take 25% off your bill of service.{{% /alert %}}

In your shortcode, isset is causing the problem but I don’t know why. Use this instead:

<div class="alert-container">
{{- if and (.Get "title") (.Get "url") }}
  <h4><a href="{{ .Get `url` }}">{{ .Get "title" }}</a></h4>
{{- else if .Get "title" }}
  <h4>{{ .Get "title" }}</h4>
{{- end -}}
{{- if in (string .Inner) "\n\n" }}
  {{ .Inner | markdownify -}}
{{- else }}
  <p>{{ .Inner | markdownify }}</p>
{{- end }}
</div>

and call the shortcode like this:


{{< alert title="foo" url="bar" >}}
baz
{{< /alert >}}

{{< alert title="foo" >}}
baz
{{< /alert >}}

{{< alert >}}
baz
{{< /alert >}}

Notes:

  1. This approach avoids an empty <h4> element if the title is not provided.
  2. Due to changes that were made in Hugo 0.55, I use the {{< shortcode >}} notation instead of the {{% shortcode %>}} notation, then pipe the contents to markdownify within the shortcode itself.
  3. If the content is a single line it will be wrapped in a <p> because markdownify doesn’t add <p> for single lines.

Thank you that works great. Im still using Hugo 0.40.3 so I kept the % symbols instead of the <.

I looked at the introduction to Hugo Templates I could not see what the dashes are for in the beginning of each statement:

{{- if and (.Get "title") (.Get "url") }}

Is there a resource that I can read that goes into hugo/go in more detail? Im still struggling with how hugo/go works.

https://golang.org/pkg/text/template/#hdr-Text_and_spaces

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