Hi,
I’m trying to understand how shortcodes work by creating my own before moving on to using complicated ones. I have this shortcode in layouts/shortcodes/img.html:
where I’m trying to print a test string and add added a supplied image with some alt text. I call it as such: {{% img src="/images/f1/track.jpg" %}}
but when I run hugo I get the error:
failed to render shortcode “img”: failed to process shortcode: “C:\Users\manas\Desktop\website-backend\framework\layouts\shortcodes\img.html:3:37”: execute of template failed at <“src”>: undefined variable: $image
I’m not sure why it can’t find the image. I do have an image at static/images/f1/track.jpg in my project. If I use vanilla markdown image reference, it works i.e., ![test_img](/images/f1/track.jpg).
What am I missing? Also, am I using the print statement correctly? Is it printed in the generated HTML? Or to the console?
with invocation {{% img src="/images/f1/track.jpg" %}}
this only prints /images/f1/track.jpg in the generated HTML. There is no img tag in the HTML.
What am I missing?
I apologize if this is a stupid question (it probably is) but I’m really not sure what is going on.
That gives this error: failed to render shortcode "img": failed to process shortcode: "C:\Users\manas\Desktop\website-backend\framework\layouts\shortcodes\img.html:4:19": execute of template failed at <$image.RelPermalink>: nil pointer evaluating resource.Resource.RelPermalink
Edit: googling this error says that I need to mark my image as an asset. I’ll try that.
But I still don’t understand why the original <img src="{{ $image }}" alt="test test test "> does not work.
But why does it find the image when I write ![test_img](/images/f1/track.jpg) and not find it when I use the shortcode? Is the default search path for shortcodes different?
Is this supposed to set the class to class=“something” from the markdown shortcode and if not set it to center? I didn’t know “or” could work there. Interesting.
I have been doing it like this:
{{- $alt := .Get “alt” | default page.Title }}
That syntax is valid, but wasn’t mentioned/documented on Hugo docs.
or
Returns the boolean OR of its arguments by returning the
first non-empty argument or the last argument, that is,
“or x y” behaves as “if x then x else y”. - Golang text template functions.