0
私はjavascriptを使用してhtmlテンプレートからレンダリングされた文字列を返す方法は?
function format(d) {
// `d` is the original data object for the row
return '<div class="expandedRow" style="padding: 10px; border-style: solid; border-width: 0px 1px; border-color: #f0f0f0;">' +
'<table cellpadding="5" cellspacing="0" border="0" style="width: 100%;">' +
'<tr>' +
'<td>Full name:</td>' +
'<td>' + d.name + '</td>' +
'</tr>' +
'<tr>' +
'<td>Extension number:</td>' +
'<td>' + d.extn + '</td>' +
'</tr>' +
'<tr>' +
'<td>Extra info:</td>' +
'<td>And any further details here (images etc)...</td>' +
'</tr>' +
'</table>'+
'<div>';
}
以下のようなJavaScript関数は明らかにそれがstring
を返しています。 しかし、htmlテンプレートはより複雑になるので、すべてをreturn文に入れるのは難しいです。
テンプレートを(template.html
)というhtmlファイルに作成し、パラメータ 'd
'のテンプレートをレンダリングして結果の文字列を返す方法はありますか?
私は
return renderTemplate('template.html', d);
変数を連結してその変数を返します。 – Rayon
ES6テンプレート文字列を使用すると、書きやすくなります。 – gcampbell
[MustacheJS](https://github.com/janl/mustache.js)などのテンプレートライブラリを参照してください –