Call javascript functions on another file from an EJS templates in Harp.js

若如初见. 提交于 2019-12-22 08:38:02

问题


Trying to make a website with Harp.js here. I use ejs templates, and want to store some useful javascript functions in a central file. How do I do that? I tried to use

<% include _render_util.js %>

But it does not work (seems like js file is not parsed).

Any ideas? Thanks!


回答1:


Although there are ways of making this work (sometimes), it's not something that was deliveratly built into Harp.js. Forcing this behaviour often takes time to debug and causes unexpected issues.

Here is a quick experiment I made that works (I didn't throughly test it):

helpers.ejs

I created a say_hello function that takes a name and outputs the string Hello, {name}.

<%
say_hello = function (name) {
  return 'Hello, ' + name;
}
%>

index.ejs

I include helpers.ejs (the file mentioned above) in the first line and then use the function in the second line. That outputs <h1>Hello, beautiful</h1>.

<% include helpers.ejs %>
<h1><%= say_hello("beautiful") %></h1>

Example gist: https://gist.github.com/jorgepedret/816c2b3985ad12cef022

There's an open issue on GitHub discussing this issue https://github.com/sintaxi/harp/issues/272

This example is more of a hack than a recommended solution. I've seen cases where it breaks in unexpected ways.



来源:https://stackoverflow.com/questions/22704502/call-javascript-functions-on-another-file-from-an-ejs-templates-in-harp-js

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