sqlalchemy / sqlalchemy/mako

json 'dumps' filter for creating javascript with mako

Open
#229 4 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug
Dominant language
Python
Stars
459
Forks
90
PR merge metrics
No merged PRs in 30d

Description

Migrated issue, originally created by jvanasco (@jvanasco)

I ran into some issues with Mako when trying to make javascript using the output of json.dumps

# in python
python_dict = { "hello" : """This is an example object I am using some 'single quotes' and "double quotes".""", }

usually i can just disable filtering in mako :

// in js template 
var payload = '${simplejson.dumps(python_dict)|n}';

unfortunately in this case the single-quotes break. we get this

// in js generated
var payload = '{"hello": "This is an example object I am using some 'single quotes' and \"double quotes\"."}'

this appears to work fine :

// in js template 
var payload = '${simplejson.dumps(python_dict).encode('string-escape')|n}';

which generates

// in js generated
var payload = {"hello": "This is an example object I am using some \'single quotes\' and \\"double quotes\\"."}

there are a handful of miscellaneous JS+Mako questions floating on StackOverflow and blogs... so i think it might be worthwhile to have an official json escaping method in mako templates.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

The issue names no files or tests. Start by locating Mako's existing filtering and escaping entry points and related template-output tests; use the examples to define the expected JSON-to-JavaScript escaping behavior, then add coverage showing that both single and double quotes remain valid.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, python
Domain
backend, web-dev
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.