Skip to content

Instantly share code, notes, and snippets.

@Fedia

Fedia/LICENSE.txt

Forked from 140bytes/LICENSE.txt
Last active Oct 12, 2020
Embed
What would you like to do?
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

This comment has been minimized.

Copy link

@atk 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

This comment has been minimized.

Copy link
Owner Author

@Fedia 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

This comment has been minimized.

Copy link

@atk 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
You can’t perform that action at this time.