Change the default behavior of {{ .Content }}?


#1

I want to define a point in the content of a page beyond which it doesn’t render, I can do this by replacing {{ .Content }} with {{ (index (split .RawContent ".---") 0) | markdownify }}, so when I write .--- in the content of a page, anything below it doesn’t render.

What I’m wondering though is, does Hugo give me any power to make this the default behavior of {{ .Content }}, no matter where I place it? Or do I just have to write the latter line everywhere? Or are there perhaps also other options I’m missing?


#2

Hello @01AutoMonkey,

Hugo allows you to create a summary for your content by adding <!--more--> to your content file. Within a template you can use .Summary instead of .Content to show the content until <!--more-->.


#3

That will work, but not if I want separate behavior for summary rendering and content rendering, but in that case I can at least do {{ (index (split .RawContent ".---") 0) | markdownify }} for content and {{ .Summary }} for summary, which isn’t ideal but will work until better options come along (one option I have in mind is to create a shortcode for it, but I haven’t tried it, or shortcodes at all, yet, so not sure how they work.).


#4

Why?

Maybe knowing why can help me come up with some solution. The only thing I can think of so far is using summaries which is what digitalcraftsman mentioned.


#5

Having a no-render line allows me to store notes and half-written material at the bottom of my articles, which are frequently updated, without the reader having to be aware of it at all, nor myself needing to somehow manage and relate this content to an article from somewhere outside it. It separates reader-ready-content from writer-content.

In my own software I solved this by allowing admins to insert custom functions at various locations within the “markdown to html pipeline” (markdown -> markdown manipulation -> markdown-to-html -> html manipulation -> html), so the function would look something like the following and be placed in the “markdown manipulation” step:

function noRender(markdown) {
    return markdown.split(".---")[0]
}
pipe.mm.push(noRender);

Or it could have been implemented as a markdown extension using a plugin in the software responsible for the markdown-to-html step (e.g. markdown-it).

Having it be part of the universally used markdown to html pipeline also made sure that this feature would always be in place no matter how or where you rendered the markdown at various points in the webpage.


In addition to {{ .Summary }}, Shortcodes: {{< Content >}}, and {{ (index (split .RawContent ".---") 0) | markdownify }} it occurred to me that I could perhaps just comment everything out below a certain point (<!-- Commented Out Text -->), con: may brake syntax formatting and highlighting in some editors, making it harder to edit.


Just tested HTML comments, they work in every way except that choosing “view source” in a browser reveals the commented out text, which is not a desired behavior.


Additional option: Store writer-content in a front-matter field that never gets used, and if one is using Forestry.io one could set it as a textarea field.


#6

The solution I’ve settled on for now, which I’m rather happy with, is to use partials.

I have a Content partial containing: {{ (index (split .RawContent (or .Site.Params.noRender ".---")) 0) | markdownify }}

And when I want to render content I do {{ partial "Content" . }} instead of {{ .Content }}

The only con is that someone might accidentally write {{ .Content }} rather than {{ partial "Content" . }} at some point.


#7

For small notes, you could easily do them in HTML comment tags or within Go tags (I think they support that). However, if you’re managing the files for the site in Git, that’s what Git is for. Branch your code, and maintain in progress work in branches and merge to master when you’re ready to publish.

I’m not sure if you’re a dev or not but as one, that’s my first instinct and how my team and I manage content for several sites in both Hugo and Jekyll.