Questions about .RenderString

If I want to switch from markdownify to .RenderString:

  • Where am I supposed to place .RenderString, on the templates’ .Content? then I can remove markdownify from the shortcodes’ .Inner?
  • When I do the above, I have some strange behavior compared to markdownify: some ULs are missing, some Ps are inserted inside LIs… any idea to fix that?
  • Is this the proper way to go if I want a “global approach” where all markup is parsed to Markdown? I tried to apply .RenderString directly from baseof.html to embrace everything (header, main block, footer…) but I didn’t manage to.

The “later”?

I edited to be clearer. By the first I meant markdownify and by the later I meant .RenderString.

It took me a little tinkering until I learned that .RenderString must be called on the actual Page object and not the string itself, i.e. switching my shortcodes from {{ .Text | markdownify }} to {{ $.Page.RenderString .Text }}.

Haven’t used it on a template but it should be something similar, like {{ .Page.RenderString .Content }}? Trial and error always gets me there eventually :wink:

1 Like

If you add options to the mix, I think it’s easier to reason about if you use the pipe syntax, e.g.

{{ .Text | $.Page.RenderString $options }}

And for people asking “why complicate stuff by needing the page context”, the answer is that the render hooks are very much tied to a Page (template lookups, the page context sent into the hooks etc.), so a stateless function will not work (as well).

1 Like

In fact simply {{ .Content | .RenderString }} in template files seems to work, no need to markdownify shortcodes’ .Inner then, nor to use the % shortcode delimiters.

But it messes up my lists, wrapping LIs with <p> </p>, adding </DIV>s and so on, whereas it was fine with markdownify. Using the % shortcode delimiters also messes up.

Indentations in the shortcode seems to cause the problem (whereas it was kind of fine with markdownify).

I created a test for you to check locally: can you have a look?
Just clone my test branch on my website and go to /test.

Assuming this is .Content on a content page, the above is what we in Norway would call “smør på flesk”.

Haha ok but I cannot make it work on templates otherwise: {{ $.Page.RenderString }} throws an error.
And the problem still occurs when only using % for shortcodes.

In the meantime I was wondering why the functionality of | .RenderString was not applied in .Content by default, which would remove the need to add it on every .Content of our templates…

Well it doesn’t work consistently.
Can we provide a guide to switch from {{ .Inner | markdownify }} (in shortcodes) to using .RenderString (in templates I guess but I’m not sure where else I could put it) in order to benefit from Render Hooks, and more generally about how to use .RenderString?

.RenderString is a method on Page

doesn’t seem to tell me how to use it properly (maybe because I am not really familiar with Go, yet I’ve been using and enjoying Hugo for years now).

I’ve tried the following without success:

{{ define "main" }}
  <main>
    {{ .RenderString .Content }}
    {{ .Page.RenderString .Content }}
    {{ $.Page.RenderString .Content }}
    {{ .Content | .RenderString  }}
    {{ .Content | .Page.RenderString  }}
    {{ .Content | $.Page.RenderString  }}
  </main>
{{ end }}

I’m sorry to not understand but where am I supposed to put what?

.Content will already render the markup (mardkown image, links, shortcodes etc…). .RenderString has been introduced so that any string can be “rendered” and not only the body “content” of a content file.

So simply calling {{ .Content }} from your template files should have the expected effect of rendering the “Content” part of your markdown file (anyting below the Front Matter).

Let’us this thead to group question about the recently released method.

I looked through the repos for potential issues which would have held some answers for me but could not find any.

I was under the impression that .RenderString would also render shortcodes. But when I try it on the following Front Matter string:

block_string:  "Here is [My Link](https://google.com) and {{< youtube w7Ft2ymGmfc >}}!"

With:

  {{ with .Params.block_string }}
    {{ $.RenderString . }}
  {{ end }}

I get the following output:

 Here is <a href="https://google.com">My Link</a> and {{&lt; youtube w7Ft2ymGmfc &gt;}}!

I tried safeHTML on the string before using .RenderString on it but I get the same results.

So, someone has any news about how to make this method render shortcodes?
And where/how to apply it to render .Content ?

1 Like