We're hiring!

We're actively seeking developers & designers for our new Detroit location. Learn more

jquery.expand Examples


jquery.expand is an extremely powerful structural templating library. Looking back on when I first started using it, I realize I was only taking advantage of about 20% of its potential. Here are some examples that should help anyone new to jquery.expand get a head start.

Classes

template

<div id="my-template">
  <h2 class="name"></h2>
  <p class="content"></p>
</div>
view raw template.html This Gist brought to you by GitHub.

expand

$("#my-template").expand({
     name: "Cooking with Crowley",
  content: "Step 1, light the grill. Step 2..."
})
view raw expand.js This Gist brought to you by GitHub.

result

<div>
  <h2 class="name">Cooking with Crowley</h2>
  <p class="content">Step 1, light the grill. Step 2...</p>
</div>
view raw result.html This Gist brought to you by GitHub.

Nesting

template

<div id="my-template">
  <h2 class="name"></h2>
  <span class="author">
    Written by <span class="name"/>
  </span>
</div>
view raw template.html This Gist brought to you by GitHub.

expand

$("#my-template").expand({
     name: "Cooking with Crowley",
   author: { name: "Shawn Crowley" }
})
view raw expand.js This Gist brought to you by GitHub.

result

<div>
  <h2 class="name">Cooking with Crowley</h2>
  <span class="author">
    Written by <span class="name">Shawn Crowley</span>
  </span>
</div>
view raw result.html This Gist brought to you by GitHub.

Selectors

template

<div id="my-template">
  <h2 class="name"></h2>
  <div class="content">
    <p class="excerpt"/>
  </div>
</div>
view raw template.html This Gist brought to you by GitHub.

expand

$("#my-template").expand({
  "$h2": "Cooking with Crowley",
  "$.content p.excerpt": "Step 1, light the grill. Step 2..."
})
view raw expand.js This Gist brought to you by GitHub.

result

<div>
  <h2 class="name">Cooking with Crowley</h2>
  <div class="content">
    <p class="excerpt">Step 1, light the grill. Step 2...</p>
  </div>
</div>
view raw result.html This Gist brought to you by GitHub.

Setting Attributes

template

<div id="my-template">
  <span class="author">
    Written by <a class="name" href=""></a>
  </span>
</div>
view raw template.html This Gist brought to you by GitHub.

expand

$("#my-template").expand({
   name: { "@href": "http://atomicobject.com/pages/Shawn+Crowley" }
})
view raw expand.js This Gist brought to you by GitHub.

result

<div>
  <span class="author">
    Written by <a class="name" href="http://atomicobject.com/pages/Shawn+Crowley"/>
  </span>
</div>
view raw result.html This Gist brought to you by GitHub.

Setting Properties

template

<div id="my-template">
  <input type="checkbox" name="like" /> Like
</div>
view raw template.html This Gist brought to you by GitHub.

expand

$("#my-template").expand({
   "$input[name=like]": { ":checked": true }
})
view raw expand.js This Gist brought to you by GitHub.

result

<div>
  <input type="checkbox" name="like" checked/> Like
</div>
view raw result.html This Gist brought to you by GitHub.

Calling Functions

template

<div id="my-template">
  <span class="author"></span>
  <input type="text" name="comment" />
</div>
view raw template.html This Gist brought to you by GitHub.

expand

$("#my-template").expand({
  author: { "addClass()": "awesome" },
  "$input[name=comment]": { "val()": "nice post!" }
})
view raw expand.js This Gist brought to you by GitHub.

result

<div>
  <span class="author awesome"></span>
  <input type="text" name="comment" value="nice post!" />
</div>
view raw result.html This Gist brought to you by GitHub.

Removing Elements

template

<div id="my-template">
  <span class="author"></span>
</div>
view raw template.html This Gist brought to you by GitHub.

expand

$("#my-template").expand({ author: false })
view raw expand.js This Gist brought to you by GitHub.

result

<div></div>
view raw result.html This Gist brought to you by GitHub.

Arrays

template

<div id="my-template">
  <span class="content"></span>
  <div class="comments">
    <div class="comment"></div>
  </div>
</div>
view raw template.html This Gist brought to you by GitHub.

expand

$("#my-template").expand({
  content: "Step 1, light the grill. Step 2...",
  comments: ["Sounds delicious!", "What next?!?"]
})
view raw expand.js This Gist brought to you by GitHub.

result

<div>
  <span class="content">Step 1, light the grill. Step 2...</span>
  <div class="comments">
    <div class="comment">Sounds delicious!</div>
    <div class="comment">What next?!?</div>
  </div>
</div>
view raw result.html This Gist brought to you by GitHub.

Functions

template

<div id="my-template">
  <span class="content"></span>
  <div class="comments">
    <div class="comment"></div>
  </div>
</div>
view raw template.html This Gist brought to you by GitHub.

expand

var comments = ["Sounds delicious!", "What next?!?"];

// or, if there are no comments
// var comments = [];

$("#my-template").expand({
  content: "Step 1, light the grill. Step 2...",
  comments: function(el) {
    if (comments.length == 0) {
      return false; // this will remove the comments div
    } else {
      return comments;
    }
  }
})
view raw expand.js This Gist brought to you by GitHub.

result w/comments

<div>
  <span class="content">Step 1, light the grill. Step 2...</span>
  <div class="comments">
    <div class="comment">Sounds delicious!</div>
    <div class="comment">What next?!?</div>
  </div>
</div>

result w/o comments

<div>
  <span class="content">Step 1, light the grill. Step 2...</span>
</div>

Nesting and Functions and Arrays, oh my!

template

<div id="my-template">
  <span class="content"></span>
  <div class="comments">
    <div class="comment">
      <span class="commenter"></span>
      <div class="body"></div>
    </div>
  </div>
</div>
view raw template.html This Gist brought to you by GitHub.

expand

var comments = [
  { author: "Dave Crosby", approved: true, text: "Sounds Delicous!" },
  { author: "Micah Alles", approved: false, text: "What next?!?" },
  { author: "Drew Colthorp", approved: true, text: "Thanks for the info." }
];

$("#my-template").expand({
  content: "Step 1, light the grill. Step 2...",
  comments: function(el) {
    var approvedComments = [];
    $.each(comments, function(index, comment) {
      if (comment.approved) {
        approvedComments.push({ commenter: comment.author, body: comment.text });
      }
    });
    if (approvedComments.length == 0) {
      return false;
    } else {
      return approvedComments;
    }
  }
})
view raw expand.js This Gist brought to you by GitHub.

result

<div>
  <span class="content">Step 1, light the grill. Step 2...</span>
  <div class="comments">
    <div class="comment">
      <span class="commenter">Dave Crosby</span>
      <div class="body">Sounds Delicous!</div>
    </div>
    <div class="comment">
      <span class="commenter">Drew Colthorp</span>
      <div class="body">Thanks for the info.</div>
    </div>
  </div>
</div>
view raw result.html This Gist brought to you by GitHub.
This entry was posted in Design & Development, Tools, Web and tagged , , , , . Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.

2 Comments

  1. Posted July 12, 2011 at 9:18 am

    What’s the value in using a client side templating system? Offline use with local database storage?

  2. Posted July 14, 2011 at 10:46 pm

    Topher – Client side templates can be very useful when the front end is pulling/pushing data to/from a JSON API. By rendering the HTML on the client side you can better decouple the user interface from the server side data processing. And as you mentioned, it is the only way to render HTML if your web application is going to run offline with no access to the web server.

    In addition, client side templates are a good way to distribute the processing requirements of web pages to the users’ machines, as opposed to having to do all of the work on the web servers. I attended a presentation at RubyConf 2010 where a guy from Twitter talked about this being one of the big motivations for them transitioning to a single-page app.

One Trackback

  1. By Getting Started with jQuery Mobile | Atomic Spin on August 11, 2011 at 4:44 pm

    [...] WorkWith the basics out of the way, you can easily begin incorporating dynamic page building with templating engines. If nothing else, you can use jQuery to do some modifications to the content. Once you get the hang [...]

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="" highlight="">