# Shortcode adaptable (.Get) variable declaration

**URL:** <https://discourse.gohugo.io/t/shortcode-adaptable-get-variable-declaration/24645>\
**Category:** support\
**Created:** [April 11, 2020, 6:50am UTC](https://discourse.gohugo.io/t/shortcode-adaptable-get-variable-declaration/24645 "2020-04-11T06:50:44Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![do2blehelix](https://avatars.discourse-cdn.com/v4/letter/d/eb9ed0/32.png) [@do2blehelix](https://discourse.gohugo.io/u/do2blehelix)\
**Post date:** [April 11, 2020, 6:50am UTC](https://discourse.gohugo.io/t/shortcode-adaptable-get-variable-declaration/24645/1 "2020-04-11T06:50:44Z")

</div>

Hello,

Objective: I am trying to get carousel images displayed from the web (eg imgur, Flickr, unsplash …). Carousel code is from **SWIPER**.

Now, when I want to display an array of say 6 images, I can declare them in the shortcode directly as below:

```auto
  <div class="swiper-container">
    <div class="swiper-wrapper">
      <div class="swiper-slide"> <img src="{{.Get 0}}"> </div>
      <div class="swiper-slide"> <img src="{{.Get 1}}"> </div>
      <div class="swiper-slide"> <img src="{{.Get 2}}"> </div>
      <div class="swiper-slide"> <img src="{{.Get 3}}"> </div>
      <div class="swiper-slide"> <img src="{{.Get 4}}"> </div>
      <div class="swiper-slide"> <img src="{{.Get 5}}"> </div>
      <div class="swiper-slide"> <img src="{{.Get 6}}"> </div>
  </div>
</div>

```

This works pretty fine **if the number of images are fixed** (6, in this case). I can use this code above in multiple pages as long as the length of the number of images is 6.

What I want to achieve is a shortcode with variable parameters. It should adjust to the number of arguments passed.

Eg I should be able to **call the same shortcode with different arguments** when needed.

{{\< shortcd “link1” “link2” “link3” \>}}  
{{\< shortcd “link1” “link2” “link3” “link4” “link5” \>}}

I am new to Hugo and new to the world of web development. Please provide a solution to this.  
PS: I do not understand code much, and I am trying to. But it would be of much help if you could share the predefined code, so that I may copy and paste.

Thanks,  
Ayan

---

<div class="post-metadata">

**Author:** ![angus](https://avatars.discourse-cdn.com/v4/letter/a/b3f665/32.png) [@angus](https://discourse.gohugo.io/u/angus)\
**Post date:** [April 11, 2020, 7:44am UTC](https://discourse.gohugo.io/t/shortcode-adaptable-get-variable-declaration/24645/2 "2020-04-11T07:44:54Z")

</div>

Shortcodes have their own .Params variable, which is an array (a list, also called a slice in Go) of all the parameters passed into the short code.

> **[Create Your Own Shortcodes](https://gohugo.io/templates/shortcode-templates/#params)**
>
> You can extend Hugo's built-in shortcodes by creating your own using the same templating syntax as that for single and list pages.

A working example:

layouts/shortcodes/testShortcode.html:

```auto
{{- /* Test shortcode showing how to handle an unknown number of parameters /* -}}

{{- /* the .Params variable holds all the parameters passed to the shortcode */ -}}
{{- /* https://gohugo.io/templates/shortcode-templates/#params */ -}}

<p>The following params were passed into this short code:</p>
<ul>
{{ range .Params }}
  <li>{{ . }}</li>
{{ end }}
</ul>

```

to call this short code:

```auto
{{< testShortcode "param1" "param2" >}}

{{< testShortcode "param1" "param2" "param3" "param4" "parma5" >}}

```

And the following is output:

```auto
The following params were passed into this short code:
  - param1
  - param2
The following params were passed into this short code:
  - param1
  - param2
  - param3
  - param4
  - parma5

```

---

<div class="post-metadata">

**Author:** ![pamubay](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/pamubay/32/17838_2.png) [@pamubay](https://discourse.gohugo.io/u/pamubay)\
**Post date:** [April 11, 2020, 7:50am UTC](https://discourse.gohugo.io/t/shortcode-adaptable-get-variable-declaration/24645/3 "2020-04-11T07:50:25Z")

</div>

Put this into your shortcode:

```auto
<div class="swiper-container">
  <div class="swiper-wrapper">
{{- range $link := .Params -}}
   <div class="swiper-slide"><img src="{{ $link | safeHTML }}"></div>
{{- end -}}
</div>
</div>

```

Usage:

```auto
{{< shortcd "https://picsum.photos/seed/wws/200/300" "https://picsum.photos/seed/sssw/200/300" "https://picsum.photos/seed/pplk/200/300" >}}

```

---

<div class="post-metadata">

**Author:** ![do2blehelix](https://avatars.discourse-cdn.com/v4/letter/d/eb9ed0/32.png) [@do2blehelix](https://discourse.gohugo.io/u/do2blehelix)\
**Post date:** [April 11, 2020, 8:13am UTC](https://discourse.gohugo.io/t/shortcode-adaptable-get-variable-declaration/24645/4 "2020-04-11T08:13:44Z")

</div>

Thanks for the explanation @angus. The shortcode works, but I’m unable to get the intended swiper carousel. It works very well for the lists.

---

<div class="post-metadata">

**Author:** ![do2blehelix](https://avatars.discourse-cdn.com/v4/letter/d/eb9ed0/32.png) [@do2blehelix](https://discourse.gohugo.io/u/do2blehelix)\
**Post date:** [April 11, 2020, 8:14am UTC](https://discourse.gohugo.io/t/shortcode-adaptable-get-variable-declaration/24645/5 "2020-04-11T08:14:37Z")

</div>

Thank you so much @pamubay. Precisely what I was looking for 😍 👏

---

<div class="post-metadata">

**Author:** ![system](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/system/32/1_2.png) [@system](https://discourse.gohugo.io/u/system)\
**Post date:** [April 13, 2020, 8:14am UTC](https://discourse.gohugo.io/t/shortcode-adaptable-get-variable-declaration/24645/6 "2020-04-13T08:14:39Z")

</div>

Closed on April 13, 2020, 8:14am UTC

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