Giter Site home page Giter Site logo

iq-scm / pwt.jinja2js Goto Github PK

View Code? Open in Web Editor NEW

This project forked from mkerrin/pwt.jinja2js

0.0 0.0 0.0 180 KB

Compiles Jinja2 templates to JavaScript

Home Page: http://pypi.python.org/pypi/pwt.jinja2js/

Shell 1.63% JavaScript 48.29% Python 45.22% CSS 4.85%

pwt.jinja2js's Introduction

About

pwt.jinja2js is an extension to the Jinja2 template engine that compiles valid Jinja2 templates containing macros to JavaScript. The JavaScript output can be included via script tags or can be added to the applications JavaScript.

Nutshell

Here a small example of a Jinja template:

{% namespace ns1 %}

{% macro printusers(users) %}
<ul>
{% for user in users %}
    <li><a href="{{ user.url }}">{{ user.username }}</a></li>
{% endfor %}
</ul>
{% endmacro %}

Which after running through the pwt.jinja2js compiler we need up the following JavaScript:

if (typeof ns1 == 'undefined') { var ns1 = {}; }

ns1.printusers = function(opt_data, opt_sb, opt_caller) {
   var output = '';
   output += '\n<ul>\n';
   var userList = opt_data.users;
   var userListLen = userList.length;
   for (var userIndex = 0; userIndex < userListLen; userIndex++) {
       var userData = userList[userIndex];
       output += '\n   <li><a href="' + userData.url + '">' + userData.username + '</a></li>\n';
   }
   output += '\n</ul>\n';
   return output;
}

By slipping a switch we can produce Java Script that takes advantage of Closure Library:

goog.provide('ns1');

goog.require('soy');

ns1.printusers = function(opt_data, opt_sb) {
   var output = opt_sb || new soy.StringBuilder();
   output.append('\n<ul>\n');
   var userList = opt_data.users;
   var userListLen = userList.length;
   for (var userIndex = 0; userIndex < userListLen; userIndex++) {
       var userData = userList[userIndex];
       output.append('\n   <li><a href="', userData.url, '">', userData.username, '</a></li>\n');
   }
   output.append('\n</ul>\n');
   if (!opt_sb) return output.toString();
}

More user documentation is available on the pwt.jinja2js PyPI package page. The Jinja2 site is also available to learn more about the Jinja2 syntax.

Devloper information

In order to get started with this project and to see what it can do:

  • git clone git://github.com/mkerrin/pwt.jinja2js.git
  • cd pwt.jinja2js
  • python bootstrap
  • ./bin/buildout

Testing

To run the tests run

  • ./bin/test -v

To run a basic server in order to run the JavaScript tests.

  • ./bin/serve --reload

To run the JavaScript tests open a browser and load the url:

http://localhost:8000/

There is links to two test suites from there.

pwt.jinja2js's People

Contributors

cuu508 avatar mkerrin avatar natedub avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.