Skip to content

Instantly share code, notes, and snippets.

@saevarom
Created January 20, 2016 10:20
Show Gist options
  • Save saevarom/2d625376a99aea782489 to your computer and use it in GitHub Desktop.
Save saevarom/2d625376a99aea782489 to your computer and use it in GitHub Desktop.
django template escaping and un-escaping for project template templates
#!/bin/sh
# Convert a regular django template to a project-template template
# Usage: ./django_template_escape.sh filename
# The file is edited in-place.
sed -i '' "s/{%/openblock/g" "$1"
sed -i '' "s/%}/closeblock/g" "$1"
sed -i '' "s/openblock/{% templatetag openblock %}/g" "$1"
sed -i '' "s/closeblock/{% templatetag closeblock %}/g" "$1"
sed -i '' "s/{{/{% templatetag openvariable %}/g" "$1"
sed -i '' "s/}}/{% templatetag closevariable %}/g" "$1"
sed -i '' "s/{#/{% templatetag opencomment %}/g" "$1"
sed -i '' "s/#}/{% templatetag closecomment %}/g" "$1"
#!/bin/sh
# Convert django project-template template to a regular template
# Usage: ./django_template_unescape.sh filename
# The file is edited in-place.
sed -i '' "s/{% templatetag openblock %}/{%/g" "$1"
sed -i '' "s/{% templatetag closeblock %}/%}/g" "$1"
sed -i '' "s/{% templatetag openvariable %}/{{/g" "$1"
sed -i '' "s/{% templatetag closevariable %}/}}/g" "$1"
sed -i '' "s/{% templatetag opencomment %}/{#/g" "$1"
sed -i '' "s/{% templatetag closecomment %}/#}/g" "$1"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment