Split of split

Hi all,

I’m trying to split my .content 2 times at different points. Both work separately but I can’t manage to do it one after the other.Here is my code so far.

{{- $contents := split .Content `<p>` -}}
					{{/*  splitting content and displaying the first part (before the first <p> tag)  */}}
				{{ index $contents 0 | safeHTML }} 
					{{/* storing the rest of the content into article variable  */}}
				{{- $article :=  $contents 1 -}}
				{{/*  splitting article at  `<div class="footnotes" role="doc-endnotes">` */}}
				{{- $rest := split $article  `<div class="footnotes" role="doc-endnotes">` -}}
				{{/*  displaying both parts	  */}}
				{{ index $rest 0 | safeHTML }}
				{{ index $rest 1 | safeHTML }}

I’m getting the following error.

execute of template failed at <$contents>: can’t give argument to non-function $contents

I haven’t investigated the logic, but in the above do you mean:

{{- $article :=  index $contents 1 -}}

Yes it was missing indeed. But still not doing what I want. I think it’s splitting my content at every <p> but I want it to do it only at the first <p> it finds. Is it possible with .split?

You could split to get index 0, put index 1-N back together again using delimit with a empty string delimiter, then split again.

You might also look at the FindRESubmatch function.

Thank you. I tried the following with Delimit, but running into issues.

{{- $contents := split .Content `<p>` -}}
				{{/*  splitting content at first p }} */}}
				{{ index $contents 0 | safeHTML }} 
				{{/*create a slice of contents */}}
				{{ $s := slice $contents 1 $contents 2 $contents 3 }}
				{{ delimit $s "" }}
				{{ index $s  }}

This works but outputs unredered HTML . If I change it to {{ index $s | safeHTML }}I get the following error:

 execute of template failed at <safeHTML>: error calling safeHTML: unable to cast []interface {}{[]string{"<div class=\"article-img \">\n  <figure data-imgstate=\"dither\">\n   
``` etc. 

How to solve this error? 
And how to loop through all contents strings with 1-N ?

What exactly do you expect $s to be? From what I see, I’d guess it is
[$contents 1 $contents 2 $contents 3]
i.e. the content of $content, the number 1, the content of $content again, the number 2 etc.
You probably again forgot to use index in that context.

Why don’t you follow the advice @jmooring gave you?

put index 1-N back together again using delimit with a empty string delimiter

{{ $rest := delimit (after 1 $contents) ""}}

Now $rest should be .Content after the first p element. Though … well, you could have ps inside div, and then you’d end up with borken HTML. Perhaps you could tell us more about what you really want to achieve? Modifying .Content often smells fishy.

Aside: index requires two arguments. Thus, $index $s in the last line should throw an error (as should $index $s | safeHTML. And {{delimit $s ""}} doesn’t make sense, since it creates a string that you then ignore. You must assign that to a variable if you want to use it. There’s no in-place modification of $s happening here, as you might be expecting.

That leads me to suggest that you perhaps take a step back first and learn a bit more about the basics of Hugo syntax before delving head first into the more demanding things.