0

このチュートリアルを使用して、アンダースコアを使用してテンプレートを使用しました。http://backbonetutorials.com/what-is-a-view/

ap タグのテキストを div 構造に追加したい状況があります。これはテンプレートではなく、いくつかの値を挿入する必要があるテキストです。アンダースコアを使用して、このテキストの変数を更新する方法はありますか? または、テキストをテンプレートとして作成し、html(_template) を使用してテンプレートを追加する必要がありますか?

<div id="popup">
<p>I want to add some text in here <%=variable1%>, Then add another variable text in here <%=variable2%></p>
</div>

アップデート:

このコードを使用して、テンプレート ドキュメントに基づいて実行してみました。

<html>
<head>
   <script type="text/javascript" src="js/jquery.min.js"></script>
   <script type="text/javascript" src="js/underscore.js"></script>
   <script type="text/javascript" src="js/core.js"></script>
</head>
<body>
    <div id="popup">
    <div id="template_holder"></div>
    </div>

<!-- TEMPLATES FOR CANCEL PAYMENT MODEL -->
<script type="text/template" id="card-success_tmp">
    <p>I want to add some text in here <%=variable1%>, Then add another variable text in here <%=variable2%></p>
</script>
</body>
</html>

次に、私のコアjsファイルで、ポップアップを起動するクリックイベント内に以下のコードがあります

var variables = {variable1:'[xxxx]', variable2:'[MM/YY]'};
var template = _.template( $("#card-success_tmp").html(), variables );
$('#popup #template_holder').html(template);

しかし、上記はまだ機能していません。

私も試してみました

var template = _.template( $("#card-success_tmp").html() );
$('#popup #template_holder').html(template({variable1:'[xxxx]', variable2:'[MM/YY]'}));

テキストはレンダリングされますが、渡された変数はレンダリングされません。

テンプレートをスクリプトタグからではなく文字列として追加した場合、それは機能します。問題は、なぜ script タグから機能しないのかということです。テキストをレンダリングしますが、渡された変数はレンダリングしません。

var template = _.template( "<p>I want to add some text in here <%=variable1%>, Then add another variable text in here <%=variable2%></p>" );
$('#popup #template_holder').html(template({variable1:'[xxxx]', variable2:'[MM/YY]'}));
4

2 に答える 2