summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorkpdecker <kpdecker@gmail.com>2015-08-22 10:54:54 -0700
committerkpdecker <kpdecker@gmail.com>2015-08-22 11:13:08 -0700
commit495cd05a7e27bf0485ffc07cb32324402422868b (patch)
treec67149a36bc831e6e654b0af4993f3fb2ac0dd23
parent452afbf2ffdf32a6d7112b727cc95cc6b415e8a5 (diff)
downloadhandlebars.js-495cd05a7e27bf0485ffc07cb32324402422868b.zip
handlebars.js-495cd05a7e27bf0485ffc07cb32324402422868b.tar.gz
handlebars.js-495cd05a7e27bf0485ffc07cb32324402422868b.tar.bz2
Implement inline partials
Allows for partials to be defined within the current template to allow for localized code reuse as well as for conditional behavior within nested partials. Fixes #1018
-rw-r--r--lib/handlebars/decorators.js1
-rw-r--r--lib/handlebars/decorators/inline.js22
-rw-r--r--lib/handlebars/runtime.js4
-rw-r--r--spec/partials.js39
4 files changed, 66 insertions, 0 deletions
diff --git a/lib/handlebars/decorators.js b/lib/handlebars/decorators.js
index d5caefb..6f5a615 100644
--- a/lib/handlebars/decorators.js
+++ b/lib/handlebars/decorators.js
@@ -1,5 +1,6 @@
import registerInline from './decorators/inline';
export function registerDefaultDecorators(instance) {
+ registerInline(instance);
}
diff --git a/lib/handlebars/decorators/inline.js b/lib/handlebars/decorators/inline.js
new file mode 100644
index 0000000..2142466
--- /dev/null
+++ b/lib/handlebars/decorators/inline.js
@@ -0,0 +1,22 @@
+import {extend} from '../utils';
+
+export default function(instance) {
+ instance.registerDecorator('inline', function(fn, props, container, options) {
+ let ret = fn;
+ if (!props.partials) {
+ props.partials = {};
+ ret = function(context, options) {
+ // Create a new partials stack frame prior to exec.
+ let original = container.partials;
+ container.partials = extend({}, original, props.partials);
+ let ret = fn(context, options);
+ container.partials = original;
+ return ret;
+ };
+ }
+
+ props.partials[options.args[0]] = options.fn;
+
+ return ret;
+ });
+}
diff --git a/lib/handlebars/runtime.js b/lib/handlebars/runtime.js
index e1b069e..6ee5c84 100644
--- a/lib/handlebars/runtime.js
+++ b/lib/handlebars/runtime.js
@@ -239,6 +239,10 @@ export function invokePartial(partial, context, options) {
let partialBlock;
if (options.fn && options.fn !== noop) {
partialBlock = options.data['partial-block'] = options.fn;
+
+ if (partialBlock.partials) {
+ options.partials = Utils.extend({}, options.partials, partialBlock.partials);
+ }
}
if (partial === undefined && partialBlock) {
diff --git a/spec/partials.js b/spec/partials.js
index 314cca2..f3283ba 100644
--- a/spec/partials.js
+++ b/spec/partials.js
@@ -257,6 +257,45 @@ describe('partials', function() {
});
});
+ describe('inline partials', function() {
+ it('should define inline partials for template', function() {
+ shouldCompileTo('{{#*inline "myPartial"}}success{{/inline}}{{> myPartial}}', {}, 'success');
+ });
+ it('should overwrite multiple partials in the same template', function() {
+ shouldCompileTo('{{#*inline "myPartial"}}fail{{/inline}}{{#*inline "myPartial"}}success{{/inline}}{{> myPartial}}', {}, 'success');
+ });
+ it('should define inline partials for block', function() {
+ shouldCompileTo('{{#with .}}{{#*inline "myPartial"}}success{{/inline}}{{> myPartial}}{{/with}}', {}, 'success');
+ shouldThrow(function() {
+ shouldCompileTo('{{#with .}}{{#*inline "myPartial"}}success{{/inline}}{{/with}}{{> myPartial}}', {}, 'success');
+ }, Error, /myPartial could not/);
+ });
+ it('should override global partials', function() {
+ shouldCompileTo('{{#*inline "myPartial"}}success{{/inline}}{{> myPartial}}', {hash: {}, partials: {myPartial: function() { return 'fail'; }}}, 'success');
+ });
+ it('should override template partials', function() {
+ shouldCompileTo('{{#*inline "myPartial"}}fail{{/inline}}{{#with .}}{{#*inline "myPartial"}}success{{/inline}}{{> myPartial}}{{/with}}', {}, 'success');
+ });
+ it('should override partials down the entire stack', function() {
+ shouldCompileTo('{{#with .}}{{#*inline "myPartial"}}success{{/inline}}{{#with .}}{{#with .}}{{> myPartial}}{{/with}}{{/with}}{{/with}}', {}, 'success');
+ });
+
+ it('should define inline partials for partial call', function() {
+ shouldCompileToWithPartials(
+ '{{#*inline "myPartial"}}success{{/inline}}{{> dude}}',
+ [{}, {}, {dude: '{{> myPartial }}'}],
+ true,
+ 'success');
+ });
+ it('should define inline partials in partial block call', function() {
+ shouldCompileToWithPartials(
+ '{{#> dude}}{{#*inline "myPartial"}}success{{/inline}}{{/dude}}',
+ [{}, {}, {dude: '{{> myPartial }}'}],
+ true,
+ 'success');
+ });
+ });
+
it('should pass compiler flags', function() {
if (Handlebars.compile) {
var env = Handlebars.create();