如何使用ejs创建可复用的head.html


我想实现的功能是建立一个可复用的head.html,可以被多个页面共用。

我贴一下代码片段。
expres中
...


 res.render(rank.html, {basePath:  "http://127.0.0.1:3000"})

...

rank.html页面


 <%- include(
    "../common/head.html", 
    {   
      css: ["/module/issue/rank/css.js"]
    }) 
%>

引用了head.html.

head.html中


 <%# function 设置css文件的href %>
<% function linkCss( cssHref ) {%>
    <link rel="stylesheet" href= <%= cssHref %> >
<% } %>

...


 <%# add css %>
<% linkCss("/public/css/style.css") %>
<%# add css from include %>
<% css.map(linkCss) %>

...

最后希望输出" http://127.0.0.1:3000/module/issue/rank/css.js
但是不知道该怎么办。

或者有没有其他的思路来解决这个问题呢?

Express ejs node.js JavaScript

Cirno+⑨ 9 years, 8 months ago

css我是写在layout文件中的。
包括了header和footer部分,模板用的ejs引擎,不过是以html文件格式。
你看看。
https://github.com/luoyjx/gaoqi-blog/blob/master/views/layout.html

量产型路人甲 answered 9 years, 8 months ago

Your Answer