Skip to main content

Server-side JavaScript template compilation and packaging

Project description

The jstc Python package compiles and packages JavaScript templates for delivery to browsers for client-side evaluation.

Currently, only Handlebars and Mustache template formats are supported natively, however this is easily extended via jstc’s plugin mechanism.



$ pip install jstc

Optionally, the handlebars pre-compiler can be installed to pre-compile JavaScript templates for faster client-side rendering:

# OPTIONAL: install handlebars pre-compiler
$ npm install handlebars
$ export PATH="`pwd`/node_modules/.bin:$PATH"


The typical usage is to have something similar to the following in your HTML generation template (here, using Mako syntax):

<%! import jstc %>
  <!-- ... -->

    <div id="Main">Loading...</div>

    <!-- load all the *.hbs templates -->
    <div id="Templates" style="display:none">

    <script type="text/javascript">
      // your javascript that makes use of the templates



Given that the following files exist in the Python package myapp:

File myapp/static/templates/common/hello.hbs:

Hello, {{name}}!

File myapp/static/templates/common/inputs.hbs (a “multi-template” file):

##! text

  <input type="text" name="{{name}}" value="{{value}}"/>

##! checkbox

  <input type="checkbox" name="{{name}}" value="1" {{#if value}}checked="checked"{{/if}}/>

Then, the Python code (inline and precompile attributes used for output simplicity):

import jstc
  'myapp:static/templates/common/**.hbs', 'static/templates',
  force_inline=True, force_precompile=False)

Outputs the following HTML (whitespace and newlines added for clarity):

<script type="text/x-handlebars" data-template-name="common/hello">
  Hello, {{name}}!

<script type="text/x-handlebars" data-template-name="common/inputs/text">
  <input type="text" name="{{name}}" value="{{value}}"/>

<script type="text/x-handlebars" data-template-name="common/inputs/checkbox">
  <input type="checkbox" name="{{name}}" value="1" {{#if value}}checked="checked"{{/if}}/>

See for more documentation.

Project details

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

jstc-0.1.7.tar.gz (21.6 kB view hashes)

Uploaded source

Supported by

AWS AWS Cloud computing Datadog Datadog Monitoring Facebook / Instagram Facebook / Instagram PSF Sponsor Fastly Fastly CDN Google Google Object Storage and Download Analytics Huawei Huawei PSF Sponsor Microsoft Microsoft PSF Sponsor NVIDIA NVIDIA PSF Sponsor Pingdom Pingdom Monitoring Salesforce Salesforce PSF Sponsor Sentry Sentry Error logging StatusPage StatusPage Status page