search
LoginSignup
222
Help us understand the problem. What are the problem?

More than 3 years have passed since last update.

posted at

updated at

Javascriptでヒアドキュメント

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

テンプレート文字列 - 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>


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
What you can do with signing up
222
Help us understand the problem. What are the problem?