Tagscript

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 World

Pass + as the parameter to use + for spaces, which some search URLs expect:

{urlencode(+):Hello World}
# Hello+World

Building 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 throws on a malformed sequence such as %zz. The interpreter catches it and the render ends with the error as the body.

API reference

UrlEncodeParser, UrlDecodeParser

Last updated on

On this page

Edit on Github