LoginSignup
224
222

More than 3 years have passed since last update.

Javascriptでヒアドキュメント

Last updated at Posted at 2013-05-17

そろそろ「テンプレート文字列」を使用しても問題なくなってきたので、こちらを使用しまよう。

テンプレート文字列 - JavaScript | MDN


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>


224
222
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
224
222