diff --git a/packages/blaze/view.js b/packages/blaze/view.js
index 7b60b7541c..378aebda84 100644
--- a/packages/blaze/view.js
+++ b/packages/blaze/view.js
@@ -359,6 +359,16 @@ Blaze.getCurrentView = function (kind) {
}
};
+// Gets the nearest ancestor view that corresponds to a template
+Blaze.getCurrentTemplateView = function () {
+ var view = Blaze.getCurrentView();
+
+ while (view && ! view.template)
+ view = view.parentView;
+
+ return view || null;
+};
+
Blaze.getParentView = function (view, kind) {
var v = view.parentView;
diff --git a/packages/spacebars-tests/template_tests.html b/packages/spacebars-tests/template_tests.html
index 0667b4384e..168018952f 100644
--- a/packages/spacebars-tests/template_tests.html
+++ b/packages/spacebars-tests/template_tests.html
@@ -833,3 +833,7 @@ Hi there!
{{.}}
+
+
+
+
diff --git a/packages/spacebars-tests/template_tests.js b/packages/spacebars-tests/template_tests.js
index 67f5142f98..6fc29bf69a 100644
--- a/packages/spacebars-tests/template_tests.js
+++ b/packages/spacebars-tests/template_tests.js
@@ -2132,7 +2132,7 @@ Tinytest.add(
);
Tinytest.add(
- "spacebars-tests - template_tests - access template instance from helper",
+ "spacebars-tests - template_tests - UI._templateInstance from helper",
function (test) {
// Set a property on the template instance; check that it's still
// there from a helper.
@@ -2154,7 +2154,7 @@ Tinytest.add(
);
Tinytest.add(
- "spacebars-tests - template_tests - access template instance from helper, " +
+ "spacebars-tests - template_tests - UI._templateInstance from helper, " +
"template instance is kept up-to-date",
function (test) {
var tmpl = Template.spacebars_test_template_instance_helper;
diff --git a/packages/templating/templating.js b/packages/templating/templating.js
index 2334308b7c..a47b9e973f 100644
--- a/packages/templating/templating.js
+++ b/packages/templating/templating.js
@@ -55,6 +55,14 @@ Template.__updateTemplateInstance = function (view) {
return tmpl;
};
+UI._templateInstance = function () {
+ var templateView = Blaze.getCurrentTemplateView();
+ if (! templateView)
+ throw new Error("No current template");
+
+ return Template.__updateTemplateInstance(templateView);
+};
+
Template.prototype.events = function (eventMap) {
var template = this;
template.__eventMaps = (template.__eventMaps || []);