Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@Fedia
Forked from 140bytes/LICENSE.txt
Last active November 28, 2023 18:31
Show Gist options
  • Star 9 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save Fedia/20d41d8533e0903f0123 to your computer and use it in GitHub Desktop.
Save Fedia/20d41d8533e0903f0123 to your computer and use it in GitHub Desktop.
John Resig's Micro-Templating in 140 bytes

tmpl

John Resig's Micro-Templating in 140 bytes (138 actually).

Please see John's original article for template syntax and more examples.

with (data){ ... } implementation is longer, so the v variable must be used to access the supplied data like this: <%= v.foo || 'No foo' %>

function(
a // template string
){
return Function( // compile template as a function
"v,o", // data object is called "v" in templates: <%= v.foo %>
"o=" + // this variable will aggregate the output
JSON.stringify(a) // converting template to JavaScript with JSON
.replace(/<%=(.+?)%>/g, '"+($1)+"') // allow to print data: <%= v.foo || 'No foo' %>
.replace(/<%(.+?)%>/g, '";$1;o+="') // other logic: <% for (;;) { %> loopy <% } %>
+ ";return o" // return the evaluated template
)
}
function(a){return Function("v,o","o="+JSON.stringify(a).replace(/<%=(.+?)%>/g,'"+($1)+"').replace(/<%(.+?)%>/g,'";$1;o+="')+";return o")}
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
Version 2, December 2004
Copyright (C) 2015 Fedia <fedia@psih.ru>
Everyone is permitted to copy and distribute verbatim or modified
copies of this license document, and changing it is allowed as long
as the name is changed.
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
0. You just DO WHAT THE FUCK YOU WANT TO.
{
"name": "tmpl",
"description": "John Resig's Micro-Templating in 140 bytes",
"keywords": [
"template",
"render",
"html",
"140bytes"
]
}
<!DOCTYPE html>
<title>John Resig's Micro-Templating in 140 bytes</title>
<h2>Template</h2>
<textarea id="tpl" rows="9" cols="99"></textarea>
<h2>Result <button onclick="test()">Render</button></h2>
<div id="result"></div>
<script type="text/template" id="example">
<b><%= v.title %></b>
<ul>
<% for (var i=0; i < v.food.length; i++) { %>
<li>Item #<%= i + 1 %>: <%= v.food[i] %></li>
<% } %>
</ul>
</script>
<script>
var tmpl = function(a){return Function("v,o","o="+JSON.stringify(a).replace(/<%=(.+?)%>/g,'"+($1)+"').replace(/<%(.+?)%>/g,'";$1\no+="')+";return o")};
var $el = document.querySelector.bind(document);
var $editor = $el('#tpl'),
$result = $el('#result');
$editor.value = $el('#example').text;
function test() {
var render = tmpl($editor.value); // compile template
$result.innerHTML = render({
title: 'Grocery',
food: ['apple', 'banana', 'cucumber']
});
}
test();
</script>
@atk
Copy link

atk commented Feb 6, 2015

Is there a reason you use \n instead of another semicolon whithin the silent replacements (save 1 more byte)?

Also, you can save another byte by using recursive regex (which is allowed by the 140byt.es rules):

function t(a,b,c){return!c?Function('v,o','o='+JSON.stringify(a).replace(/<%(=?)(.+?)%>/g,t)+';return o'):b?'"+('+c+')+"':'";'+b+';o+="'}

@Fedia
Copy link
Author

Fedia commented Feb 7, 2015

@atk
\n allows double slash comment: <% for (;;) { // loop description %>. So no real reason for an extra byte.
Your code with recursive regex is extremely clever. Just wow.

@atk
Copy link

atk commented Feb 11, 2015

I have used recursive regex many times before - if you can utilize the callback's arguments, go for it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment