Printing selected paragraphs in {{ .Content }}

Is there any way to print only selected paragraphs using {{ .Content }} ? I would like to print first paragraph on one div style, and print remaining paragraphs in the later div in different style.

Appreciate any suggestions or references ?

You might have an easier time styling with CSS, and using :first or something:

Thanks for suggesting. But I still wonder how this works with hugo templates using .md content files.

Well .Content is a rendered string. Hugo has two regex functions replaceRe and findRe. I think you only need the latter for your use case.
I don’t have any implementation of this but by remembering similar ones I would say.

1. Store .Content in a variable you can work, say $content.

2. Use findRe to find that first <p>anything</p> tag in .Content.

{{ $first := findRE "{{<p>(.|\n)*?<\\p>" $content }}

You have that <p> tag and its content now at {{ $first = index $first 0 }} as findRe returns an array of strings.

3. Now you can remove $first from $content using replace

{{ $content = replace $content $first "" }}

4. You’re good to use $first wherever you like. and use $content as your new .Content.


I did not test the above, especially the RegEx. You’re on your own for that set of trials an error :wink:


This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.