Giter Club home page Giter Club logo

nano's Introduction

NANO - Simplest Template Engine

Basic Usage

Assuming you have following JSON response:

data = {
  user: {
    login: "tomek",
    first_name: "Thomas",
    last_name: "Mazur",
    account: {
      status: "active",
      expires_at: "2009-12-31"
    }
  }
}

you can make:

nano("<p>Hello {user.first_name} {user.last_name}! Your account is <strong>{user.account.status}</strong></p>", data)

and you get ready string:

<p>Hello Thomas Mazur! Your account is <strong>active</strong></p>

Test page: testPage.html

Simple huh?

More Advanced Example

Displaying list of twitter search results (JSONP API)

http://jsfiddle.net/UXZDy/86/

nano's People

Contributors

defkode avatar fraunigai avatar insign avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

nano's Issues

Are you ready to adopt AMD standards?

code:
define('nano', [], function(){
return function (template, data) {
return template.replace(/{([\w.]*)}/g, function(str, key) {
var keys = key.split("."), v = data[keys.shift()];
for (var i = 0, l = keys.length; i < l; i++) v = v[keys[i]];
return (typeof v !== "undefined" && v !== null) ? v : "";
});
};
});

does not work if key has a space in it

thanks for this great piece of code, my only issue was that it does not work if the key has a space in it.
for example
{
"sku": "sdf3de",
"product name": "widget2"
}

it will work with sku, but not with "product name"

error in example ajax

var template = "

  • @{from_user} {original_text}
  • "
    var query = "beer OR vodka"
    var container = $("ul#tweets")

    $.getJSON("http://search.twitter.com/search.json?callback=?", {
    q: query
    }, function(data) {
    container.html("")
    $.each(data.results), function(i, item){
    container.append($.nano(template, item))
    }
    }

    }

    this is never work becouse wrong position of ')' after data.results
    in $.each(data.results),

    Ability to make it even more nano without any issues

    Just looked at nano.js and you could easily make it several characters. It is interesting that you spend time making should you do not output null or undefined, but don't check that undefined[keys[]] or null[keys[]] is not protected, you could put it in the while(i < l && ...) adds code though.

    function nano(template, data) {
    return template.replace(/{([\w.]*)}/g, function(str, key) {
    var keys = key.split("."), v = data[keys.shift()], i = 0, l = keys.length;
    while(i < l) v = v[keys[i++]];
    //simple while, hoisted variables up to avoid extra 'var', increment on use
    return (typeof v == "undefined" || v == null) ? '' : v;
    //change double negative save two chars.
    });
    }

    Just sharing what I happen to see, really not any issue and I can close out either way.

    current version don't work

    fiddle's version work, while github's version don't.

    please check the code. thank you.

    btw, this code snippet is very Q and powerful. good job!

    Way to Escape

    Way to escape. Current work around I found is to write it as "{output{}}" to display "{output}" need a cleaner way such as {{output}}

    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.