1

独自のカスタム関数をejsに追加するにはどうすればよいですか?

<%- custom_function('test') %>

ありがとうございました!

4

1 に答える 1

2

独自のフィルターを ejs に追加できます。

ejs.filters.custom_function = function(str) {
    return str + ' custom string';
};

テンプレートでは、次のようにフィルターにアクセスできます。

<%=: 'somestring' | custom_function %>

コロンを使用して、追加のパラメーターを関数に渡すことができます。

ejs.filters.custom_function = function(str, postfix) {
    return str + postfix;
};

そしてあなたのテンプレートで:

<%=: 'somestring' | custom_function:' custom postfix' %>
于 2012-03-05T23:15:01.643 に答える