URL encoding
Encode text for use in a URL, or decode it back.
urlencode makes text safe to drop into a URL. urldecode reverses it.
For template authors
Syntax
{urlencode:text}
{urlencode(+):text}
{urldecode:text}Examples
{urlencode:Hello World}
# Hello%20World
{urldecode:Hello%20World}
# Hello WorldPass + as the parameter to use + for spaces, which some search URLs expect:
{urlencode(+):Hello World}
# Hello+WorldBuilding a search link:
https://www.google.com/search?q={urlencode(+):{args}}Aliases
urlencode, encodeuri. Decoding is urldecode only.
For developers
Register the parsers
import { Interpreter, UrlEncodeParser, UrlDecodeParser } from 'tagscript';
const ts = new Interpreter(new UrlEncodeParser(), new UrlDecodeParser());Behaviour
Both require a payload. They call encodeURI and decodeURI, not the Component variants, so reserved URL characters such as &, =, ? and / pass through unchanged. That is fine for a whole URL and wrong for a single query parameter value, where an & in user input would start a new parameter. Write your own parser around encodeURIComponent if you need that.
decodeURI fails on a malformed sequence such as %zz. That is the template author's mistake, so it raises a TemplateError: urldecode was given a url it cannot decode replaces the tag, the rest of the template still renders, and the error is on response.errors.
API reference
Last updated on