我如何使用“本地人”玉器compile()中的选项,客户端? [英] How can I use the "locals" option in jade compile(), client-side?
问题描述
服务器是Node.js应用程序。我想作为Jade模板助手(用于函数)或全局变量(用于对象/变量)与客户端共享代码。每个客户端模板中都应提供新的帮助程序和全局变量。这个问题不是关于在服务器端使用Jade时向Jade添加助手。
Server is a Node.js application. I'd like to share code with the client as Jade template helpers (for functions) or globals (for objects/variables). New helpers and globals should be available in every client template. This question is NOT about adding helpers to Jade when jade is used server-side.
I为Jade compile()
函数编写了RequireJS包装器,将 locals
对象(填充有要共享的属性)传递给选项
:
I wrote a RequireJS wrapper for Jade compile()
function, passing the locals
object (filled with properties to share) inside options
:
// ./wrappers/jade.js
define(['jade', 'moment', 'lodash'], function (Jade, moment, _) {
return {
compile: function (str, options) {
options.locals = _.extend({
moment: moment, // moment function
bar: 'foo', // Custom bar variable
foo: function () { return 'bar'; } // Custom foo function
}, options.locals);
return Jade.compile(str, options);
}
};
});
问题和奇怪之处
当我尝试时使用时刻
效果很好:
p!= moment().format() //- Works: <p>2013-07-10T13:57:12+02:00</p>
但是,访问自定义变量 bar
我没有错误,但有一个空的< p>
:
However, accessing the custom variable bar
gives me no errors but an empty <p>
:
p #{bar} //- No errors but: <p></p>
当我尝试调用自定义函数 foo()
它给我一个错误( 未定义不是函数):
When I try to call the custom function foo()
it gives me an error ("undefined is not a function"):
p= foo() //- Uncaught TypeError: undefined is not a function
那么,我应该如何使用(客户端)Jade compile()
函数中的locals选项在服务器和服务器之间共享代码
So, how am I supposed to use the locals options in the (client) Jade compile()
function, for sharing code between the server and the client?
更新1 :根据@explunit注释,将属性 moment
重命名为 localmoment
(在 options.locals内部
),给我一个错误 undefined不是函数,其模板为 p = localmoment()。format()
。我不明白为什么...
Update 1: as per @explunit comment, renaming the property moment
into localmoment
(inside options.locals
), gives me an error undefined is not a function with the template p= localmoment().format()
. I can't understand why...
更新2 :@ Isaac Suttell提出的解决方案(用表达函数代替匿名函数)没有不起作用,我在 p = foo()
时遇到相同的错误。
Update 2: the solution proposed by @Isaac Suttell (expression function instead of anonymus one) doesn't work, I'm getting the same error with p= foo()
.
推荐答案
对 moment
是全局变量的调用仅对 moment()。format()
有效,因此它根本不会传递到您的模板中。
The call to moment().format()
only works because moment
is a global variable, so it's not being passed in at all into your template.
我知道将函数引入模板的唯一方法是通过自身的模型。
也许有更好的方法,但是我可以使用它。
The only way i know to get a function in to the template is trough the model it self. There might be better ways but i got this to work.
define(['jade', 'moment', 'lodash'], function (Jade, moment, _) {
return {
compile: function (str, options) {
var compileFunction = Jade.compile(str, options);
return function(){
// augment the model with the functions/vars you need
arguments[0] = _.extend(arguments[0] || {}, {
foo: function() { return "bar"; },
bar: "foo"
});
return compileFunction.apply(null, arguments)
}
}
};
});
然后,如果您这样定义模板
Then if you define your template like this
p #{bar}
p= foo()
一切正常。
这篇关于我如何使用“本地人”玉器compile()中的选项,客户端?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!