Skip to content

proposal: Deprecate support for go data interpolation inside script tags #1408

Description

@whalenrp

There have been a number of fixes for handling proper parsing of javascript code (#1245 , #1250 , #1252 ) . I encountered a new issue when upgrading from an older version of templ where a division operator and a string with a slash on the same line leads to an opaque parsing error. A </script> tag is parsed as JavaScript content, producing a malformed template. I've pinned the behavior in two failing tests on this branch (https://github.com/a-h/templ/compare/main...whalenrp:templ:bug/script-division-regex-false-match?expand=1).

Rather than trying to patch the issue I encountered, I wanted to check whether this feature is worth the ongoing maintenance. The recent fixes are all improving heuristics around the same underlying problem: distinguishing / as a regex delimiter vs. a division operator. The past patches all mention that we're just improving the heuristics we're using for parsing JS, but fundamentally templ is trying to approximate a JS language parser to handle interpolation properly. The docs already have other examples of passing data between Go and Javascript, and mention that Go data interpolation isn't really the preferred way to pass data anyway:

It's better to pass data to the client in a HTML attribute or a script tag, as this separates the data from the JavaScript code, making it easier to maintain and debug.

What do you guys think? I'm happy to put up more tests and a patch for the issue I saw, but wanted to check in and see which of the following you're leaning:

  1. Improving heuristics as bugs come in
  2. Investing in proper JS parsing or using an external dependency to do this
  3. Deprecate {{ }} handling inside <script> tags in favor of templ.JSONScript, templ.JSFuncCall, etc.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions