# Transform a string variable into the variable itself

**URL:** <https://discourse.gohugo.io/t/transform-a-string-variable-into-the-variable-itself/33741>\
**Category:** support\
**Created:** [July 8, 2021, 3:53pm UTC](https://discourse.gohugo.io/t/transform-a-string-variable-into-the-variable-itself/33741 "2021-07-08T15:53:58Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![davidsneighbour](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/davidsneighbour/32/24507_2.png) [@davidsneighbour](https://discourse.gohugo.io/u/davidsneighbour)\
**Post date:** [July 8, 2021, 3:53pm UTC](https://discourse.gohugo.io/t/transform-a-string-variable-into-the-variable-itself/33741/1 "2021-07-08T15:53:58Z")

</div>

Sorry, I do not have the vocabulary to describe (or search for) this properly:

I have a string variable `$command` with a value like for instance `.IsHome`. Is there ANY way in gotemplates to transform it into its variable equivalent inside of a template?

For instance:

```auto
{{ $variable := ".IsHome" }}
{{ range $pages }}
  {{ $$variable }} <-- echoes the value of .IsHome inside of the current page object
{{ end }}

```

In PHP I think that’s called a variable variable. You define the variablename with `$variable` and get the variable with that name using `$$variable`.

Is that possible within Hugo (or Go itself)?

---

<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:** [July 8, 2021, 4:13pm UTC](https://discourse.gohugo.io/t/transform-a-string-variable-into-the-variable-itself/33741/2 "2021-07-08T16:13:20Z")

</div>

layouts/partials/eval.html

```auto
{{ $f := printf "%d.txt" now.UnixNano }}
{{ $r := printf "{{ %s }}" .cmd | resources.FromString $f }}
{{ $r = $r | resources.ExecuteAsTemplate $f .ctx }}
{{ return $r.Content }}

```

template

```auto
{{ $variable := ".IsHome" }}
{{ partial "eval" (dict "ctx" . "cmd" $variable) }} --> true/false

{{ $cmd := "add 5 1" }}
{{ partial "eval" (dict "ctx" . "cmd" $cmd) }} --> 6

```

---

<div class="post-metadata">

**Author:** ![davidsneighbour](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/davidsneighbour/32/24507_2.png) [@davidsneighbour](https://discourse.gohugo.io/u/davidsneighbour)\
**Post date:** [July 8, 2021, 4:25pm UTC](https://discourse.gohugo.io/t/transform-a-string-variable-into-the-variable-itself/33741/3 "2021-07-08T16:25:56Z")

</div>

Perfect. `ExecuteAsTemplate` makes much more sense now.

---

<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:** [July 10, 2021, 4:26pm UTC](https://discourse.gohugo.io/t/transform-a-string-variable-into-the-variable-itself/33741/4 "2021-07-10T16:26:04Z")

</div>

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