Reputation: 21
By using java i want to convert javascript template (i try to make ) to pure javascript
Here is example : Input
<? function test()
{
return "Just a message";
}
if("a"=="b")
{
?>
<h1><?=test()?></h1>
<?
}
?>
<?=test()?>
</head></html>
output pure js example
out.print("<html><head>");
function test()
{
return "Just a message";
}
out.print("<h1>");
if("a"=="b")
{
out.print(test());
}
out.print("</h1>");
out.print("</head></html>");
I need a function to convert javascript template to pure javascript an eval it later . p/s a example using javascript function here http://ejohn.org/blog/javascript-micro-templating/ but i'm not sure it work perfectly when given conplex template
Upvotes: 2
Views: 177
Reputation: 775
You can match every pair. And each pair is javascript and the rest is outputted using out.print(...). A simple regex can do the job.
Or you can look at template engines which are fully tested and supported, such as: http://mustache.github.io/
Upvotes: 1