瀏覽代碼

feat: allow to register global "functions" to be accessed from mustache global instance.

pull/802/head
Mejia1994 3 年之前
父節點
當前提交
0b2dbd0a4b
共有 2 個文件被更改,包括 61 次插入1 次删除
  1. +32
    -1
      mustache.js
  2. +29
    -0
      test/render-test.js

+ 32
- 1
mustache.js 查看文件

@@ -12,6 +12,16 @@ function isFunction (object) {
return typeof object === 'function';
}

function putFunctionsIntoView (view, functions) {
for (var fn in functions){
if (!view.hasOwnProperty(fn)){
view[fn] = functions[fn];
}
}

return view;
}

/**
* More correct typeof string handling array
* which normally returns typeof 'object'
@@ -703,6 +713,7 @@ var mustache = {
Scanner: undefined,
Context: undefined,
Writer: undefined,
globalFunctions: {},
/**
* Allows a user to override the default caching strategy, by providing an
* object with set, get and clear methods. This can also be used to disable
@@ -749,7 +760,27 @@ mustache.render = function render (template, view, partials, config) {
'argument for mustache#render(template, view, partials)');
}

return defaultWriter.render(template, view, partials, config);
return defaultWriter.render(template, putFunctionsIntoView(view, mustache.globalFunctions), partials, config);
};

mustache.registerFunction = function registerFunction (name, fn) {
if (typeStr(name) !== 'string') {
throw new TypeError('String expected on first argument to mustache.registerFunction');
}

if (!isFunction(fn)){
throw new TypeError('Function expected on second argument to mustache.registerFunction');
}

if (hasProperty(mustache.globalFunctions, name)){
console.warn('Function "' + name + '" is already registered, it will be overridden');
}

mustache.globalFunctions[name] = function globalFunction () {
return function run (text, render) {
return fn.call(null, render(text));
};
};
};

// Export the escaping function so that the user may override it.


+ 29
- 0
test/render-test.js 查看文件

@@ -227,6 +227,35 @@ describe('Mustache.render', function () {
});
});

describe('globals functions', function () {
it('the name of the function must be a string', function () {
assert.throws(function () {
Mustache.registerFunction(['wrong name'], function () {});
}, TypeError, 'String expected on first argument to mustache.registerFunction');
});

it('the function parameter must be a Function', function () {
assert.throws(function () {
Mustache.registerFunction('wrong name', ['wrong function']);
}, TypeError, 'Function expected on second argument to mustache.registerFunction');
});

it('should register a function', function () {
Mustache.registerFunction('upperCase', function (str) {
return str.charAt(0).toUpperCase() + str.slice(1);
});

assert.ok(Mustache.globalFunctions.hasOwnProperty('upperCase'));
});

it('should render a template with a registered function', function () {

var template = 'Hello from {{#upperCase}}{{country}}{{/upperCase}}';

assert.equal(Mustache.render(template, {country: 'nicaragua'}), 'Hello from Nicaragua');
});
});

tests.forEach(function (test) {
var view = eval(test.view);



Loading…
取消
儲存