Template strings and templates

Axel Rauschmayer axel at rauschma.de
Thu Aug 2 12:02:56 PDT 2012


I love the new name “template strings” for “quasi literals”. Only “tag” seem inferior to “quasi handler”, because that former term is already used in HTML.

Template strings are not ideally suited for templates (think Mustache), as expressed by Nicholas Zakas [1]. Any ideas for helping here? The best I can come up with is:

    // Define
    let tmpl = html`
    <table>
        $for address in addresses ${
            html`<tr>${"first"}</tr><tr>${"last"}</tr>`
        }
    </table>`;

    // Use
    console.log(tmpl({
        addresses: [
            { first: Jane, last: Bond },
            { first: Lars, last: Croft },
        ]
    }));

Custom syntax: The dollar sign ($) starts a command. As substitutions always have to be in curly braces, that doesn’t clash with template string syntax.

This could be OK for quick and dirty templates stored inside source code, with the advantage of the ES.next parser handling nesting for you.

[1] http://www.nczonline.net/blog/2012/08/01/a-critical-review-of-ecmascript-6-quasi-literals/

-- 
Dr. Axel Rauschmayer
axel at rauschma.de

home: rauschma.de
twitter: twitter.com/rauschma
blog: 2ality.com

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20120802/fe34884a/attachment-0001.html>


More information about the es-discuss mailing list