Qiita Teams that are logged in
You are not logged in to any team

Log in to Qiita Team
Community
OrganizationEventAdvent CalendarQiitadon (β)
Service
Qiita JobsQiita ZineQiita Blog
224
Help us understand the problem. What are the problem?

More than 1 year has passed since last update.

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>


Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
224
Help us understand the problem. What are the problem?