Coder Social home page Coder Social logo

tplite's Introduction

tplite

Component based on micro javascript template engine.

render by mocro template engine. simple interface: render, mount, setState, trigger. using scope function to bind event to DOM.

The micro javascript template engine in just a few lines of code.

compressed in 415 byte without gzip.
cache the compile template in function array.
can run in both client and server side.

Template Syntax

In short, statements must be included between percent signs and expressions must be placed between brackets.

Variables and expressions

variables or expressions will be replaced

  <h1>
    {{title}}
  </h1>
  <b>{{ encodeURIComponent(title)}}</b>

Conditional and Loops

  <!-- just using plain javascript code, example for conditional and loops -->
  {% if (messages) { %}
    {% messages.forEach(function(message){ %}
      <p>{{message}}</p>
    {% })%}
    <!-- loops in another way -->
    {% for (var i in messages){ %}
      <p>{{messages[i]}}</p>
    {% }%}
  {% } %}

Set Variables

  <!-- set Variables and display it -->
  {% var testval = 'set var in template.' %}{{ testval }}

Issue

there's one known issue: can not using ' in html template.

Usage

create new instance

var template = new tplite.Template()

compile template into function

var compile = template("<h1>{{title}}</h1>")

render the compile template by using callback

var stringbuffer = new tplite.StringBuffer()
compile({title: 'Title !!!'}, stringbuffer)
console.log(stringbuffer.toString())

// render template and write to document
compile({title: 'Title !!!'}, function(s){document.write(s);})

Demo

please see result in "index.html"

Using with Component

bind add methods to click event in template.

    <a onclick="{{view(message)}}" href="javascript:;">View</a>
    <button onclick="{{add()}}">ADD</button>

init app with params: root node, template, init state, and methods will bind to elememt.

var root = document.getElementById("root")
  , tmpl = document.getElementById("tpl").innerHTML
  , initState = {title: 'Demo for mocro javascript template!', messages: ['test demo 1', 'test demo2']};

var app = new tplite.Component(tmpl, {
  view: function(message){
    alert(message)
  },
  add: function(message){
    var  messages = this.state.messages;
    messages.push('test demo' + (messages.length + 1))
    this.setState({ messages, messages })
  },
  remove: function(index){
    var  messages = this.state.messages;
    messages.splice(index, 1)
    this.setState({ messages, messages })
  },
  onUpdate: function(){
    // will trigger when component render
    console.log('update', this.state)
  }
}, initstate, root)

Demo with Component

please see result in "component.html"

Feature

  1. todo MVC demo, source code
  2. run in server side. and work with requirejs.

LICENSE

Copyright 2014-2017 @Lloyd Zhou

Released under the MIT and GPL (version 2 or later) Licenses.

tplite's People

Contributors

lloydzhou avatar

Watchers

James Cloos avatar Radomir Bednar 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.