Git Product home page Git Product logo

jquery.template's Introduction

Installation

bower install jquery.template.sp

What makes jquery.template different?

jquery.template takes a different approach to inserting values into templates, lessening the risk of XSS exploits.

Usage

Just pass a CSS selector for the template, and then an object with values. (Templates are cached after first use)

var $el = $.template('#player', {
  team: 'green',
  image: 'images/green.jpg',
  name: 'Gillian Green'
});

Instead of the usualy delimiters and keynames, we'll approach these templates differently. We'll be using CSS classes as directives. Let's start with this example.

<script type="text/template" id="player">
  <div class="class-team">
    <img class="src-image">
    <p class="text-name"></p>
  </div>
</script>

We see a <div> with the class class-team. What is happening here? Well, class- is a directive. We add team after, to signify a connection with the team value we passed earlier. This directive will add a new class to the <div>, named green. If you're following this so far, great. The rest should be easy.

We can also see the <img> & <p> tags, both with their own directives. As you might guess, src- is a directive allowing for updates to the src attribute. That leaves text-, which is a directive for updating the inner text of an element. Note: this is done in such a way where HTML inside of values is not parsed, since that leads to the classic security issues.

After all this, what do we get? A jQuery wrapped HTML element, ready to be given a new home. Let's be quaint, and append it to the <body>.

$('body').append($el);

What directives are supported?

So far:

  • class- (adds a class)
  • href- (updates href attribute)
  • src- (updates src attribute)
  • target- (updates target attribute)
  • text- (updates inner text)

Why not more directives?

Please, feel free to ask for more. Just make a PR or issue in GitHub.

jquery.template's People

Contributors

chrisantaki avatar benjamin555 avatar yvesgoeleven avatar

Stargazers

i !== get('it') avatar  avatar

Watchers

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