質問

Running node.js with Express and EJS for rendering here is my code:

<%
var choices = [ {value: '', text: ' ' }, 
            {value: 'HELD', text: 'HELD'},
            {value: 'CLEAR', text: 'CLEAR'}  ];
var selected = 0;
for (var i=0; i<choices.length; i++){
if (manifest.z_customs_status.trim() == choices[i].value){
    selected = i;
    break;
}
}
select_tag('z_customs_status', selected,  choices)
%>

When the code runs I get

select_tag is not defined

as an error within EJS. select_tag is documented here

https://code.google.com/p/embeddedjavascript/wiki/ViewHelpers

Is EJS still viable for node development with Express?

役に立ちましたか?

解決

It looks like these aren't even defined in the source, which makes me wonder why it's on their wiki page. I did find from another answer that there is the package called express-helpers, which when configured, allows access to all of the view helpers.

npm install express-helpers

In your app.js, configure it:

require('express-helpers')(app);

Finally, your view will be slightly different:

<% 
    var choices = [ 
        {value: 1,text: 'First Choice'}, 
        {value: 2,text: 'Second Choice'},
        {value: 3,text: 'Third Choice'}  
    ]
%>
<%- select_tag('mySelectElement', choices, { value: 2 }) %>

The select_tag has the following parameters:

name
choices
html_options
  id      
  name
  value (selected value)
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top