Skip to content

Instantly share code, notes, and snippets.

@awei01
Last active September 26, 2015 08:44
Show Gist options
  • Save awei01/aba796c4a00a7db41ae4 to your computer and use it in GitHub Desktop.
Save awei01/aba796c4a00a7db41ae4 to your computer and use it in GitHub Desktop.

Some simple helper method

Using a method

HTML

<template name="parent">
	<h2>parent template</h2>
	<p>{{ simpleMethod }}</p>
	<p>{{ methodReceivingParam "foo" "bar" "baz" }}</p>
	
	<!-- this will throw an error!
		<p>{{ methodReceivingParam "foo" "bar" "baz" }}</p>
	-->

	<!-- this will return "method receiving params: , , " because foo, bar and baz evaluate to falsey
		<p>{{ methodReceivingParam foo bar baz }}</p>
	-->
run this test:
	<p>{{ methodForHashAndParam foo="foo val" bar="bar val" }}</p>
	
</template>

JS (client-only)

Template.parent.helpers({
	simpleMethod: function() {
		return "simple method result";
	},
	methodReceivingParams: function() {
		var args = Array.prototype.slice.call(arguments);
		console.log(args.pop());
		// the final argument that gets passed is Spacebars.kw object
		return "method received param: " + args.join(', ');
	},
});

Result


parent template
simple method result
method received param: foo, bar, baz

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment