Skip to content

andrewrk/jst-parser

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

14 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

 * Example usage

    var template = '<h1>hi</h1><p>foo is <%=foo%></p>';
    document.getElementById("output").innerHTML =
        Jst.evaluateSingleShot(template, {"foo":"yarrr"});

    // you can also pre-compile templates to save cpu
    var precompiled = Jst.compile(template);
    // then later
    document.getElementById("output").innerHTML =
        Jst.evaluate(precompiled, {"foo":"yarrr"});
    
    // you can use normal javascript in templates. any function that
    // is defined is available for you to use.
    <p>this is some template I'm making</p>
    <% if (user.logged_in) { %>
        <p>you are logged in.</p>
    <% } else { %>
        <p>not logged in.</p>
    <% } %>

    // Jst automatically escapes any html to protect you from h4xz0rz.
    // if you actually want to put html through a variable, use this syntax:
    <%+ html_code %>

 * How to minify

    A  Use this handy app: http://closure-compiler.appspot.com/home

    B  Or...

        1. Run the code through http://www.jslint.com/ and clean up.
        2. Download JSMin from http://www.crockford.com/javascript/jsmin.html.
        3. Download the .c file and compile for your platform. On linux:
           wget http://www.crockford.com/javascript/jsmin.c
           gcc -o jsmin jsmin.c
        4. It takes standard input and writes to standard output. So on linux:
           ./jsmin <jstparser.js >jstparser_min.js

 * How to create a "here document" in JavaScript:

    var string = (<r><![CDATA[

        The text string goes here.  Since this is a XML CDATA section,
        stuff like <> work fine too, even if definitely invalid XML.

        This is where you would put your templates pre-parsing. If using
        a web framework like Django, you could have an
        {% include 'template.html' %} here.

    ]]></r>).toString();

    http://mook.wordpress.com/2005/10/30/multi-line-strings-in-javascript/

    Upon investigation, this only works in Firefox currently. However
    it is simple to make a template filter in Django or a function
    in PHP to escape a file to make it fit for sticking in a JavaScript
    string. Here's what my typical Django templates look like:

        var template = "{% filter jsescape %}{% include 'template.jst.html' %}{% endfilter %}";

        // javascript code
    

    I have a python script file monitoring when this is saved which passes it
    through the Django templating system to generate a pre-parsed script, which
    sits in my media folder and actually gets served. See django/

About

JavaScript Templates (Jst) is a pure Javascript templating engine that runs in your browser using JSP-like syntax.

Resources

License

Stars

Watchers

Forks

Packages

No packages published