This is the example:
{{ range $index,$content := split .InnerDeindent “<—>” }}
{{ if lt $index 2 }}
```< i put this here because it doesn´t show the article tag`
article class=“{{ if eq $index 0 }}first-text{{ else }}second-text{{ end }}”>{{ $content | safeHTML }}
{{ end }}
{{ end }}
In the page is rendered like this:
<article class="first-text">
all the text content, must be inside of the HTML tag
In addition to what irkcode said about mismatched tags, you are right, it does appear in the browser as <article class="first-text"> - but if you view source, I strongly suspect, it is actually <em>article .... Hugo (actually Go) is trying its best to protect you so that injections like <alert>You have been pawned</alert> are rendered harmless by converting the tag into a simple string. May I suggest trying something like this (tested) which I think does what you want
{{ range $index, $value := slice "a" "b" "c" }}
{{ if eq $index 0 }}
<article class="first">{{ $value }}</artcle>
{{ else }}
<article class="second">{{ $value }}</article>
{{ end }}
{{ end }}
Of course, I am assuming that you consider <article> to be a safe tag and sidestep the entire check for safety. If you still want to use your construct, I would suggest that you pipe the entire string through safeHTML and not just content.
indeed that’s user supplied text in the .Inner of the Shortcode - but if that’s the use case the purpose of the shortcode. usage of safeHTML there states he trusts.
p.s. there’s no hardcoded slice inside … but variable text in the shortcode
{{< article> >}}
this is article one
<-->
this is article two
{{< /article >}
— and rethinking
I doubt our OP really wants to have real HTML within the .Inner of the Shortcode but maybe Markdown, so I would change the stuff to:
markdown
# My Articles
{{< article >}}
## Article One
and a paragraph
<-->
## Article Two
- some
- list
- items
{{< /article >}}
article.html
{{ range $index,$content := split .InnerDeindent "<--->" }}
{{ if lt $index 2 }}
<article class="{{ if eq $index 0 }}first-text{{ else }}second-text{{ end }}">
{{ $content | $.Page.RenderString }}
</article>
{{ end }}
{{ end }}
Thanks for your help, i notice something so curious.
`
{{ range $index, $value := split .InnerDeindent "<--->" }}
{{ if eq $index 0 }}
<article class="first-text">{{ $value }}</article>
{{ else }}
<article class="second-text">{{ $value }}</article>
{{ end }}
{{ end }}
`
This above works perfectly, but if I indent this like this,
`
{{ range $index, $value := split .InnerDeindent "<--->" }}
{{ if eq $index 0 }}
<article class="first-text">{{ $value }}</article>
{{ else }}
<article class="second-text">{{ $value }}</article>
{{ end }}
{{ end }}
`
It is like before, article as a text and not as a HTML tag, I going crazy with this, is normal? is so picky with the indentation or there some configuration to fix it?
It sounds like your code is within a shortcode template, and that you are calling the shortcode from your Markdown using the {{%..%}} notation. Is that correct?
When you call a shortcode using the {{% .. %}} notation, the template code is executed and the result is processed as Markdown. That means:
Anything indented by 4 or more spaces is an indented code block. That’s why you are seeing <article> tags on the rendered page.
When mixing Markdown with raw HTML, as you have done in your shortcode, you must separate HTML blocks from the Markdown with a blank line. In the referenced documentation, see the start and end conditions for the sixth type of HTML block.
This Mardkown:
{{% foo %}}
a **bold** word
<--->
an _emphasized_ word
<--->
an [example](https://example.org) link
{{% /foo %}}
With this shortcode:
{{ range $k, $v := split .InnerDeindent "<--->" }}
{{ if not $k }}
<article class="first-text">
{{ strings.Trim $v "\n\r " }}
</article>
{{ else }}
<article class="second-text">
{{ strings.Trim $v "\n\r "}}
</article>
{{ end }}
{{ end }}