Last active
December 15, 2015 21:19
-
-
Save elijahmanor/5324961 to your computer and use it in GitHub Desktop.
Angry Birds of JavaScript: Orange Bird - Templating
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function( twitter, $, undefined ) { | |
var _selection; | |
twitter.init = function( $selection ) { | |
_selection = $selection; | |
}; | |
twitter.displayTweets = function( tweets ) { | |
var $list = $( "<ul/ >" ); | |
$.each( tweets || {}, function( index, tweet ) { | |
var html = "<i>" + moment( tweet.created_at ).fromNow() + "</i>: "; | |
html += "<b>" + tweet.user.name + "</b> - "; | |
html += "<span>" + tweet.text + "</span>"; | |
html += tweet.retweeted ? " <i class='icon-repeat'></i>" : ""; | |
html += tweet.favorited ? " <i class='icon-star'></i>" : ""; | |
$( "<li />", { html: html }).appendTo( $list ); | |
}); | |
_selection.empty().append( $list.children() ); | |
}; | |
}( window.twitter = window.twitter || {}, jQuery )); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<script src="tweets.tmpl.js"></script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var template = Handlebars.templates[ "tweets.tmpl" ], | |
markup = template( tweets ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<script id="tweets-handlebars" type="text/x-handlebars-template"> | |
<ul> | |
{{#each this}} | |
<li> | |
<i>{{fromNow this.created_at}}</i>: | |
<b>{{this.user.name}}</b> - | |
<span>{{this.text}}</span> | |
{{#if this.retweeted}}<i class="icon-repeat"></i>{{/if}} | |
{{#if this.favorited}}<i class="icon-star"></i>{{/if}} | |
</li> | |
{{/each}} | |
</ul> | |
</script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
twitter.init = function( $selection ) { | |
_selection = $selection; | |
Handlebars.registerHelper( "fromNow", function( time ) { | |
return moment( time ).fromNow(); | |
}); | |
}; | |
twitter.displayTweets = function( tweets ) { | |
var templateString = $( "#tweets-handlebars" ).html(), | |
template = Handlebars.compile( templateString ); | |
_selection.empty().append( template( tweets ) ); | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
npm install -g handlebars |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
handlebars tweets.tmpl -f tweets.tmpl.js |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<script id="tweets-underscore" type="text/template"> | |
<ul> | |
<% _.each( tweets, function( tweet ) { %> | |
<li> | |
<i><%= tweet.created_at %></i>: | |
<b><%= tweet.user.name %></b> - | |
<span><%= tweet.text %></span> | |
<% if ( tweet.retweeted ) { %><i class="icon-repeat"></i><% } %> | |
<% if ( tweet.favorited ) { %><i class="icon-star"></i><% } %> | |
</li> | |
<% }); %> | |
</ul> | |
</script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
twitter.displayTweets = function( tweets ) { | |
var templateString = $( "#tweets-underscore" ).html(), | |
template = _.template( templateString ); | |
tweets = _.map( tweets, function( tweet ) { | |
tweet.created_at = moment( tweet.created_at ).fromNow(); | |
return tweet; | |
}); | |
_selection.empty().append( template( { tweets: tweets } ) ); | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<script id="tweets-underscore" type="text/template"> | |
<ul> | |
<% _.each( tweets, function( tweet ) { %> | |
<li> | |
<i><%= moment( tweet.created_at ).fromNow() %></i>: | |
<b><%= tweet.user.name %></b> - | |
<span><%= tweet.text %></span> | |
<% if ( tweet.retweeted ) { %><i class="icon-repeat"></i><% } %> | |
<% if ( tweet.favorited ) { %><i class="icon-star"></i><% } %> | |
</li> | |
<% }); %> | |
</ul> | |
</script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
twitter.displayTweets = function( tweets ) { | |
var templateString = $( "#tweets-underscore" ).html(), | |
template = _.template( templateString ); | |
_selection.empty().append( template( { tweets: tweets } ) ); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment