この内容は古いバージョンです。最新バージョンを表示するには、戻るボタンを押してください。
バージョン:3
ページ更新者:guest
更新日時:2026-06-10 18:12:53

タイトル: テンプレートの共通化(組み込み)
SEOタイトル: EJSにおけるテンプレートの共通化(組み込み)

この本文には実行可能なタグ等が含まれるため、安全のためHTMLソースとして表示しています。本文データは変更していません。

<h2>サーバー</h2>

<table border="1" cellpadding="1" cellspacing="1" style="width:500px">
	<thead>
		<tr>
			<th scope="col" style="background-color:#ffffff"><span style="font-size:16px">server.js</span></th>
		</tr>
	</thead>
	<tbody>
		<tr>
			<td style="background-color:#000000">
			<p><span style="color:#ffffff">var http = require('http');<br />
			var ejs = require('ejs');<br />
			var fs = require('fs');</span></p>

			<p><span style="color:#ffffff">var </span><span style="color:#2ecc71">contents_template </span><span style="color:#ffffff">= </span><span style="color:#f1c40f">fs.readFileSync('contents.ejs', 'utf-8')</span><br />
			<span style="color:#ffffff">var </span><span style="color:#2ecc71">common_template </span><span style="color:#ffffff">= </span><span style="color:#f1c40f">fs.readFileSync('common.ejs', 'utf-8')</span></p>

			<p><span style="color:#ffffff">var server = http.createServer(function (req, res) {<br />
			&nbsp; &nbsp; var templates = </span><span style="color:#f1c40f">ejs.render(<br />
			&nbsp; &nbsp; &nbsp; &nbsp; </span><span style="color:#2ecc71">common_template</span><span style="color:#ffffff">, {<br />
			&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </span><span style="color:#e74c3c">contents</span><span style="color:#ffffff">: </span><span style="color:#f1c40f">ejs.render(<br />
			&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </span><span style="color:#2ecc71">contents_template</span><span style="color:#ffffff">, {<br />
			&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </span><span style="color:#e74c3c">key1</span><span style="color:#ffffff">: 'val1',<br />
			&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }<br />
			&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </span><span style="color:#f1c40f">)</span><br />
			<span style="color:#ffffff">&nbsp; &nbsp; &nbsp; &nbsp; }<br />
			&nbsp; &nbsp; </span><span style="color:#f1c40f">)</span></p>

			<p><span style="color:#ffffff">&nbsp; &nbsp; res.writeHead(200, {'Content-type': 'text/html'});<br />
			&nbsp; &nbsp; res.write(templates);<br />
			&nbsp; &nbsp; res.end();<br />
			});</span></p>

			<p><span style="color:#ffffff">server.listen(8000);<br />
			console.log('サーバー起動中...')</span></p>
			</td>
		</tr>
	</tbody>
</table>

<p>&nbsp;</p>

<h2>EJS</h2>

<table border="1" cellpadding="1" cellspacing="1" style="width:500px">
	<thead>
		<tr>
			<th scope="col" style="background-color:#ffffff"><span style="font-size:16px">common.ejs</span></th>
		</tr>
	</thead>
	<tbody>
		<tr>
			<td style="background-color:#000000">
			<p><span style="color:#ffffff"><html><br />
			&nbsp; &nbsp; <head><br />
			&nbsp; &nbsp; &nbsp; &nbsp; <meta charset='utf-8'><br />
			&nbsp; &nbsp; </head><br />
			&nbsp; &nbsp; <body></span></p>

			<p><span style="color:#ffffff">&nbsp; &nbsp; &nbsp; &nbsp; </span><span style="color:#f1c40f"><%-</span><span style="color:#ffffff"> </span><span style="color:#e74c3c">contents </span><span style="color:#f1c40f">%></span></p>

			<p><span style="color:#ffffff">&nbsp; &nbsp; </body><br />
			</html></span></p>
			</td>
		</tr>
	</tbody>
</table>

<p>&nbsp;</p>

<table border="1" cellpadding="1" cellspacing="1" style="width:500px">
	<thead>
		<tr>
			<th scope="col" style="background-color:#ffffff"><span style="font-size:16px">contents.ejs</span></th>
		</tr>
	</thead>
	<tbody>
		<tr>
			<td style="background-color:#000000">
			<p><span style="color:#ffffff"><p>contents key1: </span><span style="color:#f1c40f"><%=</span><span style="color:#ffffff"> </span><span style="color:#e74c3c">key1 </span><span style="color:#f1c40f">%></span><span style="color:#ffffff"></p></span></p>
			</td>
		</tr>
	</tbody>
</table>

<p>&nbsp;</p>

<h2>起動</h2>

<table border="1" cellpadding="1" cellspacing="1" style="width:1000px">
	<tbody>
		<tr>
			<td style="background-color:#000000">
			<p><span style="color:#ffffff">node server.js</span></p>
			</td>
		</tr>
	</tbody>
</table>

<p>&nbsp;</p>

<h2>出力結果</h2>

<p><img alt="" src="/uploadImage/20181215232849_7162759.png" /></p>