问题
I've set up a nodeJS server using the Express web framework, using the EJS template engine.
When I try to use the EJS link_to
view helper inside a view, I get a reference error. Is this because I am doing something wrong, or are the view helpers just not included?
I've tried the following
<# link_to("...", "...") #>
<#= link_to("...", "...") #>
<#- link_to("...", "...") #>
Note: I've configured custom tags for EJS...
server.configure(function ()
{
this.set("view engine", "ejs");
this.set("view options", { open: "<#", close: "#>" });
});
回答1:
try :
var helpers = require('express-helpers')
and then
helpers(app);
in your app.js
if still not working, try
<%- link_to
instead of <%= link_to
回答2:
Add https://github.com/mhayashi/express-helpers , that should solve the problem..
Had some problems with their : require('express-helpers').all function though, rolled my own. Besides that it works fine.
回答3:
Thank you @user-S and @mujaffars Just to write all the steps in one place,
- Install express-helpers.
npm install --save express-helpers
- In app.js, write
var expressHelpers = require('express-helpers');expressHelpers(app);
- Use it in ejs in this way,
Using <%= instead of <%- just displays the generated html anchor tag which is not we usually want.
来源:https://stackoverflow.com/questions/5553554/link-to-and-other-view-helpers-not-included-with-ejs-bundled-with-express-for