node.js - Where should I define JS function to call in EJS template -
i working on template trying render template using express , ejs. standard structure of node app, have app.js file which contains functions following:
app.locals.getflag = function(country) { var flag_img_name = ""; if (country.tolowercase() == "us") { flag_img_name = "flag_us16x13.gif"; } else if (country.tolowercase() == "ca"){ flag_img_name = "flag_ca16x13.gif"; } return flag_img_name; }
i have some_template.ejs file calls function follows:
<img src="http://some_url_path/<%=getflag(data_point[0].country_name) %>" width="16" height="14" alt="country" >
and works fine. however, have around 15-20 functions , don't want define of them in app.js. there other place can define these functions , call them in template same way doing now? if yes, way define them accessible right now.
i new node, express , ejs , not sure of different techniques. if shed light on it, great. thank in advance.
just posting answer here might end on question while resolving same issue.
all have create new file (say functions.ejs
) , include in .ejs file want call function. so, have function in file named functions.ejs
:
<% getpricechgarrow = function(value) { arrow_img_name = ""; if (value < 0) { arrow_img_name = "arrow_down12x13.gif"; } else { arrow_img_name = "arrow_up12x13.gif"; } return arrow_img_name; } %>
then include functions.ejs
file want call function from. say, want call function in quote.ejs
file. so, include follows:
<% include *file_path*/functions %>
just use function @ appropriate location in ejs file want call it. example:
<img src = "http:/some_url/<% getpricechgarrow(data_point[0].value) %>" />
and set. hope helps someone.
Comments
Post a Comment