link_to and other view helpers not included with EJS (bundled with Express) for Node.js?

≡放荡痞女 提交于 2019-12-07 07:30:52

问题


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,

  1. Install express-helpers.

npm install --save express-helpers

  1. In app.js, write
var expressHelpers = require('express-helpers');expressHelpers(app);
  1. 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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!