Skip to content

Instantly share code, notes, and snippets.

@elrrrrrrr
Created September 12, 2014 15:28
Show Gist options
  • Select an option

  • Save elrrrrrrr/175de2b90c430840bba6 to your computer and use it in GitHub Desktop.

Select an option

Save elrrrrrrr/175de2b90c430840bba6 to your computer and use it in GitHub Desktop.
heredoc实现模版渲染
function heredoc(fn) {
return fn.toString().split('\n').slice(1,-1).join('\n') + '\n'
}
var a = heredoc(function(){/*
<%data.forEach(function(e){%>
<a class="item" href="pizzahut-detail.html?version=*&id=<%=e.pic%>">
<div class="cnt">
<img src="img/0<%=e.pic%>.jpg">
<div class="wrap">
<div class="wrap2">
<div class="content">
<div class="title"> <%=e.name%></div>
<div class="price">¥<em><%=e.prize.substring(1, e.prize.length-1)%></em>起</div>
</div>
</div>
</div>
</div>
</a>
<%})%>
*/})
var fs = require('fs');
var _ =require('lodash');
//out
console.log(_.template(a, {data: item}))
fs.writeFile('./t.html',_.template(a, {data: item}))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment