Coder Social home page Coder Social logo

dot's Introduction

doT.py

A python implementation of the famous js template engine. doT.js. http://olado.github.io/doT/index.html. It do excetly the same thing as doT.js except written in python. Thus, it can be used in python web framework.

doT.py compile the template to a pure javascript function in server side; therefore client side can evaluate the template later without any dependency. Which means it saves the time for client to load template engine and to load template file. In short, doT.py allows using client side template tech without include a template engine in client side.

Installation

pip install doT-js-py

Here is an example:

Use client side template

<html>
<!-- load template engine -->
<script type="text/javascript" src="doT.js"></script>
<div id="container">
<script type="text/javascript">
     // Compile template function
     var tempFn = doT.template("<h1>Here is a sample template {{=it.foo}}</h1>");
     var resultText = tempFn({foo: 'with doT'});
     document.getElementById('container').innerHtml = resultText;
</script>
</html>

Use doT.py, you write:

<html>
<!-- without loading template engine -->
<div id="container">
<script type="text/javascript">
     // Compile template function
     var tempFn = {{ js_template('<h1>Here is a sample template {{=it.foo}}</h1>') }};
     var resultText = tempFn({foo: 'with doT'});
     document.getElementById('container').innerHtml = resultText;
</script>
</html>

it will automatically compiled to

<html>
<!-- without loading template engine -->
<div id="container">
<script type="text/javascript">
     // Compile template function
     var tempFn = function anonymous(it) { var out='"<h1>Here is a sample template '+(it.foo)+'</h1>"';return out; };
     var resultText = tempFn({foo: 'with doT'});
     document.getElementById('container').innerHtml = resultText;
</script>
</html>

Django Support:

Jinja2 Support:

Commandline Support:

dot's People

Contributors

lucemia avatar

Stargazers

Pavel Tišnovský avatar Rorschach avatar Tzerjen Wei avatar mosu avatar tim avatar Nathan Rijksen avatar  avatar Nikolay Georgiev avatar Muhammed K K avatar Jason Ling avatar Freddy Knuth avatar Panayiotis Tzagkarakis avatar  avatar

Watchers

 avatar James Cloos avatar Alan Cristhian avatar  avatar

Forkers

paulway

dot's Issues

Conditional missmatch with original dotJS

I see a missmatch with the original dotJS behavior:

const doT = require('dot');

doT.templateSettings.strip = false

var tempFn = doT.template("{{?it.options == 1\n}}Option 1{{?? it.options == 2\n}}Option 2{{??\n}}Other option{{?}}:\n\n More text");
console.log(tempFn.toString())

which prints:

function anonymous(it
) {
var out='';if(it.options == 1){out+='Option 1';}else if(it.options == 2){out+='Option 2';}else{out+='Other option';}out+=':\n\n More text';return out;
}

Note that the output contains an if, an else if and an else.

Now, if you do the same with this Python version, the last else is converted to an else if:

from doT import template_settings, template

template_settings["strip"] = False

INPUT = r"{{?it.options == 1\n}}Option 1{{?? it.options == 2\n}}Option 2{{??\n}}Other option{{?}}:\n\n More text"
print(template(INPUT, template_settings))

the result is:

function anonymous(it) {var out='';if(it.options == 1\n){out+='Option 1';}else if(it.options == 2\n){out+='Option 2';}else if(\n){out+='Other option';}out+=':\n\n More text';return out;}

This seems to be caused because of using newlines \n in the input, but the dotJS processes it well.

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.