Skip to content

How to render Go-side strings inside <script> tags? #206

Answered by a-h
metaleap asked this question in Q&A
Discussion options

You must be logged in to vote

Hi, and sorry for the late reply.

<script> tags are not currently interpreted by templ, i.e. there's currently no way to place Go strings directly inside script tags within templ.

This is for safety. The script handling templates that templ does provide allow for the creation of JavaScript functions that accept parameters. These functions use JSON escaping for Go inputs to those functions to prevent malicious user-controlled strings from being used in unsafe contexts as per Google's SafeHTML.

It could be possible to update templ to do context-aware escaping within script elements in future, but it's not supported today.

However, templ is flexible, you can do whatever you want in Go code, …

Replies: 1 comment 2 replies

Comment options

You must be logged in to vote
2 replies
@iburris
Comment options

@a-h
Comment options

Answer selected by metaleap
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants