我应该在哪里定义JS函数来调用EJS模板 [英] Where should I define JS function to call in EJS template

查看:143
本文介绍了我应该在哪里定义JS函数来调用EJS模板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用一个模板,我试图使用express和ejs来渲染模板。对于节点应用程序的标准结构,我有app.js文件,其中包含以下函数:

  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;
}

我有一个调用此函数的some_template.ejs文件,如下所示:

 < img src =http:// some_url_path /<%= getFlag(data_point [0] .country_name)%> width =16height =14alt =country> 

它的工作原理很好。但是,我有大约15-20个这样的函数,我不想在app.js中定义它们。有没有其他地方可以定义这些功能,并按照我现在所做的相同方式调用模板?如果是的话,那么定义它们的方法是什么,以便他们现在可以访问。



我是新来的节点,快递和ejs,不知道不同的技术。如果有人能够轻视它,那将是巨大的。谢谢你提前。

解决方案

只需将这个答案提供给那些在解决同一问题时可能会遇到这个问题的人。



您只需创建新文件(例如 functions.ejs ),并将其包含在.ejs文件,您想要调用该功能。所以,我有这样的文件命名为 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;
}
%>

然后将 functions.ejs 加入文件你想要调用函数。说,我想在 quote.ejs 文件中调用这个函数。所以,我会包括如下:

 <%include * file_path * / functions%> 

只需在您要调用它的ejs文件的适当位置使用此功能即可。例如:

 < img src =http:/ some_url /<%getPriceChgArrow(data_point [0] .value) %>中/> 

,你都设置好了。希望这有助于某人。


I am working on a template where I am trying to render template using express and ejs. As to the standard structure of node app, I have app.js file which which contains functions like 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 which calls this function like follows:

<img src="http://some_url_path/<%=getFlag(data_point[0].country_name) %>" width="16" height="14" alt="country" >

and it works just fine. However, I have around 15-20 functions like this and I don't want to define all of them in app.js. Is there any other place where I can define these functions and call them in the template same way as I am doing now? If yes, what would be the way to define them so that they are accessible like they are right now.

I am new to node, express and ejs and not sure of different techniques. If someone could shed a light over it, it would be great. Thank you in advance.

解决方案

Just posting this answer here for someone who would might end up on this question while resolving same issue.

All you have to do is create new file (say functions.ejs) and include it in the .ejs file where you want to call that function. So, I have function like this 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 into the file you want to call function from. Say, I want to call this function in quote.ejs file. So, I would include it as follows:

<% include *file_path*/functions %> 

Just use this function at appropriate location in your ejs file from where you want to call it. For example:

<img src = "http:/some_url/<% getPriceChgArrow(data_point[0].value) %>" />

and you are all set. Hope this helps someone.

这篇关于我应该在哪里定义JS函数来调用EJS模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆