Javascriptでヒアドキュメント

  • 208
    Like
  • 0
    Comment
More than 1 year has passed since last update.

Multi-line strings in JavaScript and Node.js

面白い記述のしかたを発見。

<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script src="http://ajax.microsoft.com/ajax/jquery.templates/beta1/jquery.tmpl.min.js"></script>
<style>
.title{
    width:100%;
    height:240px;
    line-height:240px;
    text-align:center;
    background:#ccc;
}
</style>
<body>


<script>
//ヒアドキュメント
var heredoc = (function () {/*
<div class="title">
    <h1>
        <a href="${url}">${title}</a>
    </h1>
</div>
*/}).toString().match(/(?:\/\*(?:[\s\S]*?)\*\/)/).pop().replace(/^\/\*/, "").replace(/\*\/$/, "");


var object = {
    url: "http://shimz.me/blog",
    title: "GUNMA GIS GEEK"
}
var html = jQuery.tmpl(heredoc, object);
$('body').append(html);

</script>
</body>
</htm>