質問

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!

役に立ちましたか?

解決

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.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top