npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

tplite

v1.1.2

Published

Component based on micro javascript template engine.

Downloads

100

Readme

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.

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"

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"

loader

webpack loader, to import tpl as compile function.

module.exports = {
  // 
  module: {
    rules: [
      {
        test: /\.tpl$/,
        loader: "tplite-loader"
      }
    ]
  }
};

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.