Ignore content when publishing

I’m categorizing this under feature because I’m not sure whether it’s already been implemented or not.

Context: whenever I’m writing an article, I keep a list of all the resources I used during the research phase (usually links to Wikipedia etc. and raw sketches of sentences and ideas that I want to keep to myself only). It’s important to me to keep those in source control - they help me improve the article down the line and reference those sources from other articles. It’s also a good way of keeping track of how the research was performed. When time comes to publish the blog, though, I don’t want that part of the document to be visible - I’d like it to be completely ignored and not included in the final HTML file. I’d actually like to have the same functionality when running hugo server locally.

Is there a feature for this? A partial or a block, maybe?

Thanks for your help!

A while back this tip was posted in the Forum. However the code is slightly wrong but the general idea is very good.

Here is my spin on it.

Contents of /layouts/shortcodes/comment.html
{{ with .Get 0 }}{{ .Inner }}{{ end }}

And in your markdown files use it like so:
{{% comment %}}Internal comment{{% /comment %}}
{{< comment >}}Internal comment{{< /comment >}}

This shortcode basically looks for a parameter from its input and it will render .Inner only if that parameter exists.

Simply not providing that parameter will hide everything within the shortcode input from the HTML output.

Also note my use of the with function. This ensures that no errors are caused by the missing parameter.


Works like a charm - thanks a ton!

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:

  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.

Because it is wrong. There is a right parenthesis in your original snippet:
{{ if 0) }}{{.Inner}}{{ end }}

Console message:

ERROR 2018/08/30 00:25:27 shortcodes/comment.html : template: shortcodes/comment.html:1: unexpected ")" in input

@alexandros Oh, I see, there was a typo in my Hugo forum post. My actual implementation is as quoted in my previous post on the current thread (no parenthesis):

{{ if 0 }}{{.Inner}}{{ end }}

For some reason, I was unable to edit my original post on that thread, so I added a reply with the correct code. Thanks.

No problem. I am the king of typos. And I already said that your tip was a pretty good idea.

I modified it with Get. But what you posted above is also great!


1 Like

{{ if 0 }}{{.Inner}}{{ end }} works pretty well for me. Thanks :slight_smile: