ejs

Node.js, Express, EJS - Active class on current page in navigation

China☆狼群 提交于 2019-12-03 06:48:00
I'd like to render a 'current' class on each of my navigation links depending on which page I'm on in my layout.ejs template. Currently, my express controller index looks like this: // About exports.about = function(req, res) { res.render('content/about.ejs', { title: 'About' }); }; And in my layout.ejs I have the following, which I'd like be rendered dynamically. <ul class="nav" id="nav"> <li><a href="/">Home</a></li> <li class="current"><a href="/about">About</a></li> <li><a href="/">Contact</a></li> </ul> Any ideas of how to achieve this? You could include a page_name: 'about' in the res

Layouts in Express 3 and EJS

こ雲淡風輕ζ 提交于 2019-12-03 06:46:15
问题 In version 3 of Express some features were removed: the concept of a "layout" (template engine specific now) partial() (template engine specific) Changelog: https://github.com/visionmedia/express/wiki/Migrating-from-2.x-to-3.x The partial() can be changed for EJS own feature called include , but what is the alternative for layouts? 回答1: It seems that from Express 3, layout feature is delegated to the responsibility of template engines. You can use ejs-locals (https://github.com/RandomEtc/ejs

ejs how to iterate object

泄露秘密 提交于 2019-12-03 06:26:09
I have a simple object literal which is address as shown here address: { country: String, state: String, city: String, zip: String, street: String } and its inside an object which i'm passing with express.js render function. in my template page i'm trying to for loop inside this object as shown: <% for (var prop in artist.address ) { %> <%- artist.address[prop] %> <% } %> which output the data but includes the ejs functions as well like so: function () { return this.get(path); } function () { return this.get(path); } yafo 09988 jerusalem israel israeli [object Object] undefined undefined

How can I pass variable to ejs.compile

我的梦境 提交于 2019-12-03 06:11:44
My bottom_index.ejs looks like that: <div>The bottom section</div> In my code I declare ejs: ejs = require('ejs'); Then compile the function: var botom_index_ejs = ejs.compile(fs.readFileSync(__dirname + "/../views/bottom_index.ejs", 'utf8')); and then call it to get rendered html: botom_index_ejs() It works fine! Now I would like to change my template to: <div><%= bottom_text %></div> and be able to pass the parameter (bottom_text) to the bottom_index.ejs How should I pass the parameters? Thanks! Parameters are passed to EJS template as a JS plain object. For your example it sholud be: botom

nodeJs学习-08 模板引擎 jade、ejs

寵の児 提交于 2019-12-03 04:44:59
模板引擎   jade -破坏式、侵入式,强依赖;用了之后不能随便用别的引擎   ejs - 温和、非侵入时、弱依赖 jade使用 const jade = require('jade'); var str = jade.renderFile('./views/8.jade', { pretty: true, //美化 调试时使用 }); console.log(str); ejs使用 const ejs=require('ejs'); ejs.renderFile('./views/1.ejs', {name: 'blue'}, function (err, data){ if(err) console.log('编译失败'); else console.log(data); }); jade语法:   自动识别单标签 实例:属性script(src="a.js") link(href="a.css",rel="stylesheet")input(type="text",id="txt1",value="abc")div&attributes({title: 'aaa', id: 'div1'}) 标签内部值a(href="http://www.baidu.com/") 百度 样式div(style="width:200px;height:200px;background:red

Getting the url parameters in an ejs template

≯℡__Kan透↙ 提交于 2019-12-03 03:21:24
I am trying to create an ejs conditional based on a URL parameter, for example, if the test parameter exists at localhost:3000/page?test, then show a div, else dont show it. My ejs template looks something like: <% include header %> <div class="row"> <%if (title !== "homepage") {%> <%= title %> <div> <% include nav %> </div> <%}%> <div> </div> </div> <% include footer %> Is there a way to access the URL parameters directly from the ejs file? For example, the <%= title %> works fine, is there something like <%= req.query %>? I am also using express. You can pass it easily as an object in the

Change .ejs extension to .html using Parse.com Express.js

匿名 (未验证) 提交于 2019-12-03 03:08:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: How would I go about using .html extensions on my view files instead of .ejs when using Parse.com's Express.js? I changed the EJS delimiters to <? and ?> because I'm used to them from PHP. That worked fine, but I can't seem to change the file extension for my view files: I've tried the following: var express = require('express'); var ejs = require('ejs'); var app = express(); ejs.open = '<?'; ejs.close = '?>'; app.set('view engine', 'ejs'); app.engine('.html', ejs.renderFile); app.set('views', 'cloud/views'); app.use(express.bodyParser());

Nodejs EJS helper functions?

匿名 (未验证) 提交于 2019-12-03 02:46:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: Is there a way to register helper functions to EJS templates, so that they can be called from any EJS template? So, it should work something like this. app.js ejs.helpers.sayHi = function(name) { return 'Hello ' + name; }); index.ejs <%= sayHi('Bob') %> 回答1: Yes, in Express 3 you can add helpers to app.locals . Ex: app.locals.somevar = "hello world"; app.locals.someHelper = function(name) { return ("hello " + name); } These would be accessible inside your views like this: <% somevar %> <% someHelper('world') %> Note: Express 2.5 did helpers

Node.js express: confuse about ejs template

匿名 (未验证) 提交于 2019-12-03 02:27:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I put my ejs template file in views folder like: views | |--foo.html | |--layout.html so I config my ejs template: app.set('views', __dirname + '/views'); app.engine('html', require('ejs').renderFile); I render my foo template by this: res.render('foo.html', {title: 'test'}); I want use layout.html as the master layout template, I also add <%- body%> tag in layout.html , but is doesn't work, what I saw is only return the ended foo.html . Why the layout.html can't be the master layout?Or how can I set it to the master layout? 回答1: Ahah you

Webpack + Express + EJS: Error: Cannot find module “.”

匿名 (未验证) 提交于 2019-12-03 02:26:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I'm writing an express web app with webpack, typescript and ejs. When hitting one of the routes that's supposed to serve a .ejs file I get the following error: Error: Cannot find module "." at webpackMissingModule (/Users/max/Development/test/express-webpack/dist/server.js:20669:74) at new View (/Users/max/Development/test/express-webpack/dist/server.js:20669:152) at EventEmitter.render (/Users/max/Development/test/express-webpack/dist/server.js:18776:12) at ServerResponse.render (/Users/max/Development/test/express-webpack/dist/server.js