Md file with shortcode snippet inside another md file

I created a shortcode template at layout/shortcode/changeText.html with simple text.

Christmas

My file is structured like this-

Introduction
About
_index.md
_index.md

I am using shortcode {{< changeText />}} in About file’s contnent and it renders Christmas properly and it does not works when using with headings like this-

some heading {{< changeText/>}}

Then I included About file inside Introduction file,

{{%exceprt-include “Introduction/About/_index.md” /%}}

Now Christmas is not displayed in Introduction file, the shortcode is not rendered instead it displays {{< changeText />}} everywhere i used in About file.

Please help me out with this.
Thanks in advance.

Don’t use shortcodes this way.

I know that isn’t advice on solving your issue, but you are trying to make conditional text appear in many places based on a shortcode, where if you just wrote “Christmas” it would all work.

If I needed conditional text to appear in multiple contexts, I’d produce the templates to make it happen. Shortcodes are going to cause you more trouble if you use them this way. :slight_smile:

Ok👍, then brother it would be great if you give me some reference or idea to do this problem as I am new to HUGO.

Please explain the problem. I don’t understand from the example. From the example the shortcode is not the answer. :slight_smile:

Just forget what I’ve tried.
I want some texts in .md content file to be replaced by any global variable and then the text changes when that variable’s value is changed.
Hope you get the idea what i am talking about.
I have no idea how to do that.
can you help me?

In that case, you would have your shortcode be something simple like:

{{.Params.variablename}}

I using {{ Params.myVaraible }} in my shortcode file, it gave error

execute of template failed: template: shortcodes/myShortcodes.html:1:11: executing "shortcodes/myShortcodes.html" at <.Params.myText>: nil pointer evaluating interface {}.myVaraiable

am I using .Params in wrong place, if yes, please correct me!

What is an example of the text?

Depending on the use case, a different solution may work.

If it’s a global variable, you can include it in the config file, in the params object.
Here’s a fragment of mine (it’s in TOML, yours might be YAML or JSON):

unsafe= true
[params]
BookShowToC= true
myCustomParam = “abc”
[related]
threshold = 80
includeNewer = false

You can now use {{ .Site.Params.myCustomParam }} to render the text “abc”.

Here’s the documentation on this: https://gohugo.io/variables/site/#the-siteparams-variable

1 Like

Thanks for .Site.Params
Now i have tried custom shortcode in .md file displaying some value like-
content/myFileName/_index.md
`+++
meta-data of file
+++

some content
{{< myShortcode >}}
some more content
`

it works fine.
Now i should include only myFileName’s content in another .md file with myShortcode’s value in it.
Is this possible in any simple way?
Thanks in advance.

I have a shortcode include.html like this:

{{$page := .Get 0}}
{{ if (fileExists $page)}}
{{ with .Site.GetPage $page }}{{ .Content | markdownify }}{{ end }}
{{ else }}
{{ errorf “INCLUDE file not found: %s” $page }}
{{ end }}

Called like this:
{{% include "/pathToFile/file.md" %}}

It is a bit capricious sometimes, so I can’t promise it’s the right solution or even a supported one. If you put a shortcode in the file targeted by include, it might not work the way you expect.

I use it to include small fragments of text, such as a single paragraph or a code snippet that I want to show on a few different pages.

1 Like

I created my include.html like this-

{{$file := .Get 0}}
{{ $file | readFile | markdownify }}

Works fine, but now my page displays “{{}}” in my parent page.
markdownify does not work here.

Any suggestion on how to do this or else try some different methods, if any please help
Thanks in advance

Did you try it the way I’m using it, by treating the target file as a page rather than a file? I think I tried the “as file” method in the beginning when I was creating my shortcode and found out it could only be rendered as plaintext.

1 Like