将变量传递给EJS include [英] Pass variable to EJS include

查看:133
本文介绍了将变量传递给EJS include的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在几个地方使用了一个全局标头,我试图在呈现模板时可以通过的变量中定义其位置。

I have a global header used in a couple of places and I was trying to define its location in a variable that could be passed when rendering a template.

有什么地方像这样:

var headerLocation = 'some/location/header.ejs'; 
res.render( viewDir + '/index', {
        header: headerLocation 
    } );

在模板文件中:

<% include header %>

标题是与渲染一起传递的值。

header being the value passed in with the render.

似乎不可能,但也许我错过了一些东西,以为我想在这里问。

It doesn't seem to be possible but maybe I missed something so thought I'd ask here.

编辑:

下面的答案注释中提到了此问题,但总结起来,现在EJS版本2中可用。

This is mentioned in comments on answers below but to summarize, this is now available in version 2 of EJS.

请参见此处: https://github.com/mde/ejs#includes
以及此处的相关讨论:https://github.com/tj/ejs/issues/93

推荐答案

已添加此功能:如果它不是路径(找不到文件),则将其评估为变量名。 https://github.com/visionmedia/ejs/pull/156

This feature has been added: if it is not path (file not found), it is evaluated as variable name. https://github.com/visionmedia/ejs/pull/156

这篇关于将变量传递给EJS include的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆