# Render a string as a "hugo command"

**URL:** <https://discourse.gohugo.io/t/render-a-string-as-a-hugo-command/41857>\
**Category:** support\
**Created:** [December 11, 2022, 12:37pm UTC](https://discourse.gohugo.io/t/render-a-string-as-a-hugo-command/41857 "2022-12-11T12:37:09Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![HugoCharly](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/hugocharly/32/16638_2.png) [@HugoCharly](https://discourse.gohugo.io/u/HugoCharly)\
**Post date:** [December 11, 2022, 12:37pm UTC](https://discourse.gohugo.io/t/render-a-string-as-a-hugo-command/41857/1 "2022-12-11T12:37:09Z")

</div>

tl:dr : Can i compose a “hugo path” with strings passed as variables in the path ?

**I have this :**

(I fetch a specific data file going through my folder structure : Data → myPartial → en → mydata.yml)  
_index.html_ ↓

```auto
{{ partial "myPartial" (dict "ctx" . "data" (index .Site.Data.myPartial .Site.Language.Lang).mydata) }}

```

(then i access my data in a {{ with .data }} statement)  
_myPartial.html_ ↓

```auto
{{ with .data }}
  <div>
    <h3>{{ .title }}</h3>
    <p>{{ .text }}</p>
  </div>
{{ end }}

```

**I would like to get this :**

(I just indicate the file name in the key/value “data” → partial declaration gets dryer)  
_index.html_ ↓

```auto
{{ partial "myPartial" (dict "ctx" . "data" "mydata") }}

```

(then i access my data in a “recomposed hugo path”)  
_myPartial.html_ (PSEUDO code)↓

```auto
{{ with (index (printf site.Data.%s (hugorender "myPartial")) (printf .site.Language.Lang).%s (hugorender "data")) }}
  <div>
    <h3>{{ .title }}</h3>
    <p>{{ .text }}</p>
  </div>
{{ end }}

```

In essence “hugorender” would convert a string to an understandable “hugo command” at build time, i hope i’m clear.

What i’m asking is : is there an already possible way to do that (maybe with slice or delimit), or maybe i’ve overlooked something simple ?  
Or is this idea in contradiction with how Hugo and Go fondamentaly works ?

Thanks for reading me !

PS : I know i can do {{ with (index .Site.Data.myPartial .Site.Language.Lang).mydata }} in myPartial but what if i have multiple myPartial calls and multiple data files as sources ? Hence my post

---

<div class="post-metadata">

**Author:** ![jmooring](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/jmooring/32/4214_2.png) [@jmooring](https://discourse.gohugo.io/u/jmooring)\
**Post date:** [December 11, 2022, 3:47pm UTC](https://discourse.gohugo.io/t/render-a-string-as-a-hugo-command/41857/2 "2022-12-11T15:47:57Z")

</div>

The `index` function is _variadic_ —it accepts a variable number of arguments.

```nohighlight
{{ partial "my-partial" (dict "ctx" . "data" "my-data") }}

```

layouts/partials/my-partial.html

```nohighlight
{{ $partialName := "my-partial" }}
{{ with index site.Data $partialName .ctx.Language.Lang .data }}
  <h3>{{ .title }}</h3>
  <p>{{ .text }}</p>
{{ end }}

```

You could just use the string literal “my-partial” in the `index` function, but sometimes I find it useful to initialize a var, as shown above. That way I can include it in error or warning messages. While shortcodes know their own name (via the `.Name` method), other templates do not.

---

<div class="post-metadata">

**Author:** ![HugoCharly](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/hugocharly/32/16638_2.png) [@HugoCharly](https://discourse.gohugo.io/u/HugoCharly)\
**Post date:** [December 12, 2022, 9:51am UTC](https://discourse.gohugo.io/t/render-a-string-as-a-hugo-command/41857/3 "2022-12-12T09:51:17Z")

</div>

Thanks a lot for answering, this worked !!

I was handling the index function incorrectly and did not know you could add many parameters without parenthesis like that, i learned what variadic means today 🧐 thank you !

Heads up for anyone reading/using this :  
You have to populate ALL your data folders, even with an empty .yml otherwise the index will return “index of nil pointer” when indexing.  
 ![data](https://canada1.discourse-cdn.com/flex036/uploads/gohugo/original/3X/6/a/6a4ce232253bff8a9b5cd97ff7a8932e60b112b6.png)

This community feels great, and gives me confidence to further develop websites using Hugo.  
I have the utmost respect for you guys, the feeling is like walking among giants.

Have a good day

---

<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:** [December 14, 2022, 9:52am UTC](https://discourse.gohugo.io/t/render-a-string-as-a-hugo-command/41857/4 "2022-12-14T09:52:15Z")

</div>

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