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,
Now Christmas is not displayed in Introduction file, the shortcode is not rendered instead it displays {{< changeText />}} everywhere i used in About file.
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.
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?
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):
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.
{{$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.
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.