Params in Shortcode with double quote

Hi,

I would like from a Shortcode to display a list of topics according to the categories and tags parameters provided.

I manage to display it with a category and a tag, but not with several tags or categories.
However, the rendering of my variable returns the tags in the form "tag1" "tag2", but not in display.

I tried escaping them as specified in a post on the forum and I also tried simple quotes to surround my tags. tag='"tag1" "tag2"'
Here is the shortcode and the template of the shortcode.
{{< selected_post title="title with markdown" tag="tag1" cat="cat1" >}}

{{ $title := (.Get "title" | markdownify) }}
{{ $tag := (.Get "tag") }}
{{ $cat := (.Get "cat") }}
<p>{{ $title }}</p>
<p>{{ $cat }}</p>
<p>{{ $tag }}</p>

<div class="container bg-light border border-2 rounded p-3">
  <h5 class="text-center" >{{ $title }}</h5>
  <ul class="list-unstyled">
      {{ range where (where .Site.RegularPages ".Params.categories" "intersect" (slice $cat)) ".Params.tags" "intersect" (slice $tag ) }}
      <li >
        <a class="text-decoration-none" href="{{ .Permalink }}">{{ .Title }}</a>
      </li>
      {{ end }}
    </ul>
</div>

Thank you in advance for your help.

you can use , comma as delimiter then split it to return slice.

{{< selected_post title="title with markdown" tag="tag1,tag2" cat="cat1,cat2" >}}

{{ $title := (.Get "title" | markdownify) }}
{{ $tag := (.Get "tag") }}
{{ $cat := (.Get "cat") }}

{{/* Generate slice of tags
  1. split by `,` comma
  2. trim any whitespaces i.e. "tag1, tag2" <-- tag2 has whitespace prefix
*/}}
++ {{ $tags := apply (split $tag ",") "trim" "." "\r\n\t "}}
++ {{ $cats := apply (split $cat ",") "trim" "." "\r\n\t " }}


<p>{{ $title }}</p>
<p>{{ $cat }}</p>
<p>{{ $tag }}</p>

<div class="container bg-light border border-2 rounded p-3">
  <h5 class="text-center" >{{ $title }}</h5>
  <ul class="list-unstyled">
++      {{ range where (where .Site.RegularPages ".Params.categories" "intersect" $cats) ".Params.tags" "intersect" $tags }}
      <li >
        <a class="text-decoration-none" href="{{ .Permalink }}">{{ .Title }}</a>
      </li>
      {{ end }}
    </ul>
</div>


2 Likes

Thanks for explain and example.

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