# Eval function

**URL:** <https://discourse.gohugo.io/t/eval-function/32148>\
**Category:** feature\
**Created:** [April 3, 2021, 1:17pm UTC](https://discourse.gohugo.io/t/eval-function/32148 "2021-04-03T13:17:27Z")\
**Posts on this page:** 9\
**Page:** 1

<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:** [April 3, 2021, 1:17pm UTC](https://discourse.gohugo.io/t/eval-function/32148/1 "2021-04-03T13:17:27Z")

</div>

I would like to do this:

```auto
{{ $s := slice "add 5 1" "sub 6 2" "mul 7 3" "div 8 4" }}
{{ range $s }}
  {{ printf "%s = %d" . (eval .) }}
{{ end }}

```

To produce this:

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

```

The primary use cases are documentation development and testing.

Comments?

---

<div class="post-metadata">

**Author:** ![bep](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/bep/32/3332_2.png) [@bep](https://discourse.gohugo.io/u/bep)\
**Post date:** [April 3, 2021, 2:10pm UTC](https://discourse.gohugo.io/t/eval-function/32148/2 "2021-04-03T14:10:59Z")

</div>

> [@jmooring](#):
>
> Comments?

What about `resources.ExecuteAsTemplate`?

---

<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:** [April 3, 2021, 3:30pm UTC](https://discourse.gohugo.io/t/eval-function/32148/3 "2021-04-03T15:30:32Z")

</div>

👍

This:

```auto
{{ $s := slice "add 5 1" "sub 6 2" "mul 7 3" "div 8 4" }}
{{ range $s }}
  {{ $f := printf "%d.tmp" now.UnixNano }}
  {{ $r := printf "{{ %s }}" . | resources.FromString $f }}
  {{ $r = $r | resources.ExecuteAsTemplate $f . }}
  {{ printf "<pre>%s = %s</pre>" . $r.Content | safeHTML }}
{{ end }}

```

Produces this:

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

```

Notes:

1. This uses the same target path (a temp file) for `resources.FromString` and `resources.ExecuteAsTemplate`.
2. The temp file had to be unique for each iteration of the range.
3. When running with `hugo server` the temp file had to be unique for each rebuild and each iteration of the range.
4. Items #2 and #3 lead me to `now.UnixNano` for the temp file name.

This seems fairly clean, but I’m sure there’s room for improvement.

---

<div class="post-metadata">

**Author:** ![bep](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/bep/32/3332_2.png) [@bep](https://discourse.gohugo.io/u/bep)\
**Post date:** [April 3, 2021, 3:37pm UTC](https://discourse.gohugo.io/t/eval-function/32148/4 "2021-04-03T15:37:11Z")

</div>

> [@jmooring](#):
>
> ` {{ $f := printf "%d.tmp" now.UnixNano }}`

How about:

```auto
{{ $f := printf "%s.txt" ($s | md5) }}

```

Note that using a MIME type that is defined (e.g. `text/plain`) is much faster. We have a slow path for unknown mime types.

---

<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:** [April 3, 2021, 4:45pm UTC](https://discourse.gohugo.io/t/eval-function/32148/5 "2021-04-03T16:45:32Z")

</div>

Thanks. Works great. Revised code:

```auto
{{ $s := slice "add 5 1" "sub 6 2" "mul 7 3" "div 8 4" }}
{{ range $s }}
  {{ $f := printf "%s.txt" (. | md5) }}
  {{ $r := printf "{{ %s }}" . | resources.FromString $f }}
  {{ $r = $r | resources.ExecuteAsTemplate $f . }}
  {{ printf "<pre>%s = %s</pre>" . $r.Content | safeHTML }}
{{ end }}

```

Note that `resources.FromString` does not overwrite an existing file when ranging through the slice of commands or between rebuilds when running `hugo server`. That’s why I initially used `now.UnixNano` for the temporary file name.

When we use a hash value (`md5`) for the temporary file name, the code above does not do what we think it does when our slice of commands contains duplicate items. For example:

```auto
{{ $s := slice "add 5 1" "add 5 1" }}

```

When processing the second item, `resources.ExecuteAsTemplate` uses the resource created for the first item. But that’s OK because the expected result is the same.

Thank you very much for your help.

---

<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:** [April 3, 2021, 5:11pm UTC](https://discourse.gohugo.io/t/eval-function/32148/6 "2021-04-03T17:11:12Z")

</div>

And finally, abstracted to a function…

layouts/partials/eval.html

```auto
{{ $f := printf "%s.txt" (. | md5) }}
{{ $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

```

---

<div class="post-metadata">

**Author:** ![bep](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/bep/32/3332_2.png) [@bep](https://discourse.gohugo.io/u/bep)\
**Post date:** [April 3, 2021, 5:34pm UTC](https://discourse.gohugo.io/t/eval-function/32148/7 "2021-04-03T17:34:04Z")

</div>

> [@jmooring](#):
>
> When processing the second item, `resources.ExecuteAsTemplate` uses the resource created for the first item. But that’s OK because the expected result is the same.

I don’t think your reasoning is correct, which I think would be clear if you do `add now.UnixNano 1` or something. The use of the MD5 leads to the reasource and the result of parsing the template getting cached, which is a good thing and what you want.

---

<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:** [April 3, 2021, 5:41pm UTC](https://discourse.gohugo.io/t/eval-function/32148/8 "2021-04-03T17:41:45Z")

</div>

> [@bep](#):
>
> a good thing and what you want

I agree. This is going to be really useful. Thanks again!

---

<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:** [April 5, 2021, 5:42pm UTC](https://discourse.gohugo.io/t/eval-function/32148/9 "2021-04-05T17:42:24Z")

</div>

Closed on April 5, 2021, 5:42pm UTC

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