# Using shortcode code1.html within an other shortcode code2.html

**URL:** https://discourse.gohugo.io/t/using-shortcode-code1-html-within-an-other-shortcode-code2-html/16639
**Category:** support
**Created:** [January 25, 2019, 2:16pm UTC](https://discourse.gohugo.io/t/using-shortcode-code1-html-within-an-other-shortcode-code2-html/16639 "2019-01-25T14:16:00Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![gillesbonnet](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/gillesbonnet/32/7075_2.png) [@gillesbonnet](https://discourse.gohugo.io/u/gillesbonnet)
#### Post date: [January 25, 2019, 2:16pm UTC](https://discourse.gohugo.io/t/using-shortcode-code1-html-within-an-other-shortcode-code2-html/16639/1 "2019-01-25T14:16:00Z")

</div>

The issue I have seems to be the same as the one raised [in this other question](https://discourse.gohugo.io/t/programmatically-use-shortcode-inside-another-shortcodes-definition-file/11580), but I did not really manage to follow the answers and understand if this has been really answered.

Let say I have a shortcode `code2.html` and I want to use this within the code of an other shortcode `code1.html`. Note that here I consider very simple shortcodes: they are called without arguments and do not require closing shortcodes.

I want to write in my file `code1.html`

```
some code ...
{{< code2 >}}
... some code

```

This returns an error `unexpected "<" in command`. I have read the documentation to create your own shortcode but I could not find a way to solve this problem. Is it possible to do something like this (in a simple way), or is it out the scope of what Hugo does?

---

<div class="post-metadata">

### Author: ![zwbetz](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/zwbetz/32/16088_2.png) [@zwbetz](https://discourse.gohugo.io/u/zwbetz)
#### Post date: [January 25, 2019, 2:28pm UTC](https://discourse.gohugo.io/t/using-shortcode-code1-html-within-an-other-shortcode-code2-html/16639/2 "2019-01-25T14:28:30Z")

</div>

I would recommend going about this a different way. Here are two ideas:

(1) Pass the 2nd shortcode as the `.Inner` of the 1st shortcode. For example:

```nohighlight
{{< shortcode-1 >}}
{{< shortcode-2 >}}
{{< /shortcode-1 >}}

```

(2) Put the 2nd shortcode logic into a partial, then call that partial from your 1st shortcode. See an example of that [here](https://discourse.gohugo.io/t/link-as-a-function-markdown/16604/8?u=zwbetz).

---

<div class="post-metadata">

### Author: ![gillesbonnet](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/gillesbonnet/32/7075_2.png) [@gillesbonnet](https://discourse.gohugo.io/u/gillesbonnet)
#### Post date: [January 25, 2019, 3:02pm UTC](https://discourse.gohugo.io/t/using-shortcode-code1-html-within-an-other-shortcode-code2-html/16639/3 "2019-01-25T15:02:37Z")

</div>

Thank you very much @zwbetz for your reactivity.

I have troubles to understand how solution (1) works exactly. This `.Inner` function still look a bit strange to me. Sorry!

I do understand how solution (2) works. I also see that I might have oversimplified a bit too much my problem. So let me add the additional constraint that `code2` is actually called with one argument. I don’t think that solution (1) can handle that because partial do not take argument. Tell me if I am wrong, and please let me know if you think that solution (1) could handle this well.

So now ´code2´ is called with some argument(s), and ´code1´ should look like:

```
some code ...
{{< code2 arg1 >}}
... some code ...
{{< code2 arg2 >}}
... some code ...
{{< code2 arg3 >}}
... some code ...
...
... some code ...
{{< code2 arg10 >}}
... some code
```

---

<div class="post-metadata">

### Author: ![zwbetz](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/zwbetz/32/16088_2.png) [@zwbetz](https://discourse.gohugo.io/u/zwbetz)
#### Post date: [January 25, 2019, 3:09pm UTC](https://discourse.gohugo.io/t/using-shortcode-code1-html-within-an-other-shortcode-code2-html/16639/4 "2019-01-25T15:09:54Z")

</div>

I could help you more if I knew what these shortcodes should be doing, because maybe there is a better way. Do you have some concrete examples?

---

<div class="post-metadata">

### Author: ![gillesbonnet](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/gillesbonnet/32/7075_2.png) [@gillesbonnet](https://discourse.gohugo.io/u/gillesbonnet)
#### Post date: [January 25, 2019, 3:33pm UTC](https://discourse.gohugo.io/t/using-shortcode-code1-html-within-an-other-shortcode-code2-html/16639/5 "2019-01-25T15:33:47Z")

</div>

Thank you for your support.

I have in my data folder several YAML files, say `lecturer1.yaml`, `lecturer2.yaml`, …

On a content page `program.md` where I present the program of a workshop, I am calling a shortcode `{{< timeTable >}}`. This is my `code1` from the messages above.  
In `timeTable.html` I have some html code to define a table in which I have different information, and in particular a series of cells which should display in a uniform way information extracted from the files `lecturerX.yaml`, such as _name of lecturer_, _university of lecturer_, _title of the talk of the lecturer_,…  
My idea was to create a shortcode `timeTableEntry.html` (`code2`from the above messages). This shortcode should have as argument the name of the lecturer and display the corresponding information.

---

<div class="post-metadata">

### Author: ![zwbetz](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/zwbetz/32/16088_2.png) [@zwbetz](https://discourse.gohugo.io/u/zwbetz)
#### Post date: [January 25, 2019, 3:43pm UTC](https://discourse.gohugo.io/t/using-shortcode-code1-html-within-an-other-shortcode-code2-html/16639/6 "2019-01-25T15:43:07Z")

</div>

Thanks for the info. So I think you could accomplish this more cleanly with a single shortcode. You could pass each lecturer as an arg to `timeTable`, then in the shortcode definition, [range through each arg](https://gohugo.io/templates/shortcode-templates/#params) and build your HTML table.

A quick example. Let’s say you passed 3 lecturers to your shortcode:

```nohighlight
{{< timeTable "lecturer1" "lecturer2" "lecturer3" >}} 

```

Then in your shortcode definition:

```nohighlight
<!-- start of your HTML table -->
{{ range .Params }}
  {{ . }} <!-- do something with each lecturer passed in -->
{{ end }}
<!-- end of your HTML table -->

```

---

<div class="post-metadata">

### Author: ![gillesbonnet](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/gillesbonnet/32/7075_2.png) [@gillesbonnet](https://discourse.gohugo.io/u/gillesbonnet)
#### Post date: [January 25, 2019, 3:58pm UTC](https://discourse.gohugo.io/t/using-shortcode-code1-html-within-an-other-shortcode-code2-html/16639/7 "2019-01-25T15:58:42Z")

</div>

Thanks.  
I though about it this at first. I might end doing something like this, but it is not yet entirely clear to me how. The problem with ranging through the participants given as arguments is to deal with all the non-lecture-info such as coffee break, lunch break, poster session, social dinner… they should also appear in the time table.

Well. This might have not answered as I wished. **In particular, I understand that we cannot easily call a shortcode within a shortcode (as described above) in an elementary way.** But this discussion has brought some new perspectives and way to tackle my problem. Thanks again.

---

<div class="post-metadata">

### Author: ![zwbetz](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/zwbetz/32/16088_2.png) [@zwbetz](https://discourse.gohugo.io/u/zwbetz)
#### Post date: [January 25, 2019, 4:02pm UTC](https://discourse.gohugo.io/t/using-shortcode-code1-html-within-an-other-shortcode-code2-html/16639/8 "2019-01-25T16:02:04Z")

</div>

Welcome. Wishing you good luck. And although it doesn’t answer your question, [this](https://zwbetz.com/create-an-html-table-from-a-toml-data-file-in-hugo/) may be useful to you when building your HTML table.

---

<div class="post-metadata">

### Author: ![gillesbonnet](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/gillesbonnet/32/7075_2.png) [@gillesbonnet](https://discourse.gohugo.io/u/gillesbonnet)
#### Post date: [January 25, 2019, 4:04pm UTC](https://discourse.gohugo.io/t/using-shortcode-code1-html-within-an-other-shortcode-code2-html/16639/9 "2019-01-25T16:04:42Z")

</div>

Thanks for the link. Indeed it gives me some ideas of new things to try. 🙂
