I posted the internal-comment comment
shortcode tip that the previous reply referenced. @alexandros I’m not sure why you wrote that the code is wrong? I’ve been using the original implementation ({{ if 0 }}{{.Inner}}{{ end }}
) for over a year and it works well. Note that if 0
wasn’t meant to evaluate a shortcode parameter, it’s just a dummy condition that always evaluates to true, like #if 0
in C.
I also defined a condition
shortcode that allows using custom $.Site.Params.Filters
map or environment-variable filters to dynamically include or exclude specific content by changing the value of the filter or removing its definition. @claudiobizzotto I think this might be more appropriate for your use case, because you can control the filter value and related doc processing by changing the filters map variable in the config file or the value of an environment variable, which you can also set in the build command, without modifying your MD code.
NOTE: The current implementation works, but it isn’t the cleanest because of some unexpected behavior that I had encountered with Hugo’s parameters processing. See, for example, this support issue that I opened. I think some of the issues might have been fixed in later releases; (e.g., Hugo v0.40 added support for optional positional parameters, which I was really missing), but I hadn’t had time to check it yet.
condition.html theme shortcode
<!-- The shortcode has two alternative conditional-processing paths:
- Doc-filter site-param condition, using "filter" and "filterval" params -
process the shortcode content if $Site.Params.Filters has a key whose name matches the "filter" param and whose value equals the "filterval" param value.
- Environment-variable condition, using "env" and "envval" params - process the
shortcode content if there's a defined environment variable whose name
matches the "env" param and whose value equals the "envval" param value.
Note: See also the not-condition.html shortcode for a negative condition logic.
We also added conditional logic to specific shortcodes such as xref.
-->
{{- if or (and (.Get "filter") (eq (.Get "filterval") (index $.Site.Params.Filters (.Get "filter")))) (and (.Get "env") (eq (getenv (.Get "env")) (.Get "envval"))) -}}
{{- printf "%s" .Inner | markdownify -}}
{{- end -}}
Then, if your config.toml file has the following Params.Filters
definition, for example:
[Params.Filters]
testfilter = "true"
You can use the following in your Markdown file:
{{% condition filter="testfilter" filterval="true" %}}
Show this text only when `$.Site.Params.Filters.testfilter == "true"`.
{{% /condition %}}
OR, you can define a TESTFILTER
enviornment variable, for example, and use this MD code:
{{% condition env="FILTERVAR" envval="true" %}}
Show this text only when `$TESTFILTER == "true"`.
{{% /condition %}}
I also defined a similar not-condition
shortcode to render the content if the condition evaluates to false; currently I don’t use it because all my filters are Boolean and I can use condition
to test for "false"
, but it could be useful in other cases:
not-condition.html theme shortcode
{{- if or (and (.Get "filter") (ne (.Get "filterval") (index $.Site.Params.Filters (.Get "filter")))) (and (.Get "env") (ne (getenv (.Get "env")) (.Get "envval"))) -}}
{{- printf "%s" .Inner | markdownify -}}
{{- end -}}
I added similar optional filter
and filterval
variables also to my xref
cross-reference shortcode to allow using xref
with an invalid link target within a comment
or false condition
shortcode call (which can be useful during development); if I don’t apply the filter also to the xref
call, Hugo produces an error or warning for the unresolved link target.