关于Express 3.* EJS include 的疑问
发布于 11 年前 作者 lgyhitler 10551 次浏览 最后一次编辑是 8 年前

一开始这样写

var html = ejs.render(tmp, {user:{name:'test'});

ejs模板是这样的:

<html>
<head>
<% include head.ejs %>
</head>
<body>
	<% include top_bar.ejs %>
	<h1><%=user.name %></h1>
	<ul>
		<li>11111</li>
		<li>22222</li>
		<li>33333</li>
		<li>44444</li>
	</ul>
	<% include foot.ejs %>
</body>
</html>

结果控制台报错:

Error: filename option is required for includes

然后去看github上的API发现有个filename参数,试着改成

var html = ejs.render(tmp, {user:{name:'test'},filename:"ejs/head.ejs"});

结果居然正常了!??问题是我模板文件里include的3个文件,为什么参数里只写了一个就行了呢? API里这段话没看懂,求大家指点一二:

Includes are relative to the template with the include statement, for example if you have "./views/users.ejs" and "./views/user/show.ejs" you would use <% include user/show %>. The included file(s) are literally included into the template, no IO is performed after compilation, thus local variables are available to these included templates.
回到顶部