Created
July 11, 2014 17:45
-
-
Save TooManyBees/bcc38a724bcf9bcbcd2c to your computer and use it in GitHub Desktop.
roll your own templater?
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
<div class="entry columns $col_classes bN" data-beacon="{'p':{'mpid':'$i'}}"> | |
<div class="panel"> | |
<a href="clickable" href="$entry_path"></a> | |
<div class="bN $img_size-img" data-beacon="{'p':{'lnid':'img'}}"> | |
<div class="image-container" data-img-path="$img_path"></div> | |
</div> | |
<div class="panel-text"> | |
<h5 class="cf bN" data-beacon="{'p':{'lnid':'hldn'}}">$headline</h5> | |
</div> | |
</div> | |
</div> | |
<!-- | |
var templ = Template(thatbloboftextupthere); | |
var output = templ({ | |
i: i, | |
col_classes: "small-6" || "small-12 large-6", | |
entry_path: "/us/entry/whatever", | |
img_size: "large" || "small", | |
img_path: "http://s.huffingtonpost.com/whatever", | |
headline: "Barry Odertsky's Empassioned Speech on Accounting Practices" | |
}); | |
--> |
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() { | |
function warn(str, key) { | |
console.warn("Template \""+str.substr(0, 20)+"...\" rendered with missing key: "+key); | |
} | |
function exec(str, params) { | |
params || (params = {}); | |
return str.replace(/\$(\w+)/g, function(match, key) { | |
if (params[key] === undefined) { warn(str, key); } | |
return params[key]; | |
}); | |
} | |
function make(str) { | |
var trimmed = str.replace(/\s*?\n\s*/gm, ""); // Compress whitespace | |
return function(params) { | |
return exec(trimmed, params); | |
} | |
} | |
window.Template = make; | |
})() |
raphaeleidus
commented
Jul 11, 2014
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment