When i write some tag inside of the if statement, the tag is render like text and not like html tag, how can i solve this?

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

First pls quote your code using theree backticks or the code button

Your code is missing the closing tag :slight_smile:

I suppose this is a shortcode.

Call it using {{< >}} in your markdown not {{% %}}.

The real code and call context could help further

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 &lt;em&gt;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?

Yes, with the other, is rendered like text and not the semantic markdown, ### like this and not h3 tag

Have you checked this answer?

When you call a shortcode using the {{% .. %}} notation, the template code is executed and the result is processed as Markdown. That means:

  1. Anything indented by 4 or more spaces is an indented code block. That’s why you are seeing <article> tags on the rendered page.
  2. 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 }}

Produces this HTML:

<article class="first-text">
  <p>a <strong>bold</strong> word</p>
</article>
<article class="second-text">
  <p>an <em>emphasized</em> word</p>
</article>
<article class="second-text">
  <p>an <a href="https://example.org">example</a> link</p>
</article>

And because your shortcode is generating raw HTML, you must add this to your site configuration:

[markup.goldmark.renderer]
unsafe = true

It’s not unsafe if you control the conent.

1 Like

mmh, as always with coding multiple variants for a solution

any thoughts about other impacts/performance /preferred?

{{< >}} + RenderString

  • no indentation problems with the layout file (no Trim calls)
  • multiple calls of RenderString (one for each part)

{{% %}}

  • proper indentation necessary
  • render config setting
  • Just one internal call of the Renderer

I would prefer option 1 cause I autoformat my code and this will break indents
or am I missing something terrible

Depends. You need to use the {{% .. %}} approach if you want, for example, Markdown headings to contribute to the page’s TableOfConents.

If users are going to mix Mardown and raw HTML, they need to read that portion of the CommonMark spec.

1 Like

Yes, is like you said, exactly like that, but the things is that said jmooring, but I understand now, thank you very much.

1 Like

Thank you so much!