1
0
mirror of https://github.com/gohugoio/hugo.git synced 2024-05-11 05:54:58 +00:00

Squashed 'docs/' content from commit f887bd7b

git-subtree-dir: docs
git-subtree-split: f887bd7b4e3e7c7e76cd63951e5b0d37d8fe0ac7
This commit is contained in:
Bjørn Erik Pedersen
2017-08-10 17:18:22 +02:00
commit ec4e6f9df2
839 changed files with 44703 additions and 0 deletions

View File

@@ -0,0 +1,31 @@
---
title: safeJS
# linktitle:
description: Declares the provided string as a known safe JavaScript string.
godocref: https://golang.org/src/html/template/content.go?s=2548:2557#L51
date: 2017-02-01
publishdate: 2017-02-01
lastmod: 2017-02-01
categories: [functions]
menu:
docs:
parent: "functions"
#tags: [strings]
signature: ["safeJS INPUT"]
workson: []
hugoversion:
relatedfuncs: []
deprecated: false
draft: false
aliases: []
---
In this context, *safe* means the string encapsulates a known safe EcmaScript5 Expression (e.g., `(x + y * z())`).
Template authors are responsible for ensuring that typed expressions do not break the intended precedence and that there is no statement/expression ambiguity as when passing an expression like `{ foo:bar() }\n['foo']()`, which is both a valid expression and a valid program with a very different meaning.
Example: Given `hash = "619c16f"` defined in the front matter of your `.md` file:
* <span class="good">`<script>var form_{{ .Params.hash | safeJS }};…</script>` &rarr; `<script>var form_619c16f;…</script>`</span>
* <span class="bad">`<script>var form_{{ .Params.hash }};…</script>` &rarr; `<script>var form_"619c16f";…</script>`</span>