Files

source (sprinth.js)

sprinth.js

Replace %{foo} templates with hash["foo"] with escaping.

Provides minimal "templating" for javascript. Hash may also be a prototype.js extended hash. Template escapes:

%%   literal %
%{}  HTML-escape         replaces &, <, and > with &amp;, &lt;, and &gt;
%[]  JavaScript-escape   replaces \, ", and ' with \\, \", and \'
%<>  URI-escape          uses core encodeURIComponent() function
%«»  URI-escape          uses core encodeURI() function [leaves , / ? : @ & = + $ # alone]
%()  unescaped           inserts hash value with no substitutions

Escaping may be nested, for instance: %{[foo]} will js-escape then HTML escape the hash value.

Example:

element.innerHTML = sprinth(
  '<p onclick="alert(\'%{[bar]}\')">%{foo}</p>',
  {
    foo: 'f\'(x) < 23',
    bar: '\'e said, "<ook!>"'
  }
);