# Evaluating a variable as Hugo code? \`eval\` or \`hugoify\` function?

**URL:** <https://discourse.gohugo.io/t/evaluating-a-variable-as-hugo-code-eval-or-hugoify-function/42686>\
**Category:** support\
**Created:** [January 29, 2023, 11:37pm UTC](https://discourse.gohugo.io/t/evaluating-a-variable-as-hugo-code-eval-or-hugoify-function/42686 "2023-01-29T23:37:19Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![pleasantone](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/pleasantone/32/10256_2.png) [@pleasantone](https://discourse.gohugo.io/u/pleasantone)\
**Post date:** [January 29, 2023, 11:37pm UTC](https://discourse.gohugo.io/t/evaluating-a-variable-as-hugo-code-eval-or-hugoify-function/42686/1 "2023-01-29T23:37:19Z")

</div>

Hi, I’m picking up some data from a data file (or possibly front matter?) and am trying to solve this in a generalized fashion…

we have markdownify and plainify to take the context of a variable and convert it, but I don’t see a “hugoify” or “eval” function.

I want to be able to do

data/stuff.yaml:

```auto
a: "Some regular text."
b: "{{ ref . `about` }}"
c: "this is a title -- {{ .Title }}"

```

and then in a theme later:

```auto
{{ with .Site.data.stuff.yaml }}
    Your slogan is {{ .a }} <br>
    It can be found at {{ .b }}<br>
    {{ .c }}
{{ end }}

```

It would currently generate:

```auto
Your slogan is: Some regular text.
It can be found at: {{ ref . `about` }}
This is a title {{ .Title }}

```

When I want to generate:

```auto
Your slogan is: Some regular text.
It can be found at: localhost://my-about-page/
This is a title -- The Title of this Page Context

```

Again, this needs to be a general solution, NOT a type/field specific solution.  
Is this possible?

For full context, I’m auto-generating [schema.org](http://schema.org) compatible JSON+LD data at  
[timer-hugo/seo\_schema.html at master · shockwavefound/timer-hugo · GitHub](https://github.com/shockwavefound/timer-hugo/blob/master/layouts/partials/seo_schema.html)

---

<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:** [January 30, 2023, 12:05am UTC](https://discourse.gohugo.io/t/evaluating-a-variable-as-hugo-code-eval-or-hugoify-function/42686/2 "2023-01-30T00:05:13Z")

</div>

> [@pleasantone](#):
>
> Is this possible?

No, it is not.

You could do something like this, but I wouldn’t…

```plaintext
a:
  type: string
  content: Some regular text.
b:
  type: pageReference
  content: "/about"
c:
  type: pageProperty
  content: title

```

I came up with an eval function a while ago, but I only use for testing and some documentation:  
[https://discourse.gohugo.io/t/eval-function/32148/5](https://discourse.gohugo.io/t/eval-function/32148/5)

---

<div class="post-metadata">

**Author:** ![pleasantone](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/pleasantone/32/10256_2.png) [@pleasantone](https://discourse.gohugo.io/u/pleasantone)\
**Post date:** [January 30, 2023, 12:20am UTC](https://discourse.gohugo.io/t/evaluating-a-variable-as-hugo-code-eval-or-hugoify-function/42686/3 "2023-01-30T00:20:56Z")

</div>

Thanks, I’m sorry I missed the earlier thread, not googled enough.  
Best,

Paul

---

<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:** [January 30, 2023, 12:23am UTC](https://discourse.gohugo.io/t/evaluating-a-variable-as-hugo-code-eval-or-hugoify-function/42686/4 "2023-01-30T00:23:54Z")

</div>

> [@jmooring](#):
>
> an eval function

I don’t think that will do what you want… it doesn’t handle action delimiters `{{ }}`. You could probably do some regex magic to extract the action contents.

---

<div class="post-metadata">

**Author:** ![pleasantone](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/pleasantone/32/10256_2.png) [@pleasantone](https://discourse.gohugo.io/u/pleasantone)\
**Post date:** [January 30, 2023, 12:25am UTC](https://discourse.gohugo.io/t/evaluating-a-variable-as-hugo-code-eval-or-hugoify-function/42686/5 "2023-01-30T00:25:33Z")

</div>

yes, I was thinking this would be a good test of the findRESubmatch code

---

<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:** [January 30, 2023, 12:27am UTC](https://discourse.gohugo.io/t/evaluating-a-variable-as-hugo-code-eval-or-hugoify-function/42686/6 "2023-01-30T00:27:33Z")

</div>

So, if you’re gonna do it, use this instead of what was posted way back when:

layouts/partials/eval.html

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

```

Example:

```auto
{{ $s := slice "add 5 1" "sub 6 2" "mul 7 3" "div 8 4"}}
{{ range $s }}
  {{ $r := partial "eval" . }}
  {{ printf "<pre>%s = %s</pre>" . $r | safeHTML }}
{{ end }}

```

Result:

```auto
add 5 1 = 6
sub 6 2 = 4
mul 7 3 = 21
div 8 4 = 2

```

I had to use `now.UnixNano` to get the correct result on a multilingual site, but it works fine with a monolingual site as well.

---

<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:** [February 1, 2023, 12:27am UTC](https://discourse.gohugo.io/t/evaluating-a-variable-as-hugo-code-eval-or-hugoify-function/42686/7 "2023-02-01T00:27:58Z")

</div>

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