Skip to content

Instantly share code, notes, and snippets.

@harrylove
Last active April 21, 2018 11:52
Show Gist options
  • Save harrylove/4381571 to your computer and use it in GitHub Desktop.
Save harrylove/4381571 to your computer and use it in GitHub Desktop.
Nested template problem, deployed to http://nestedtemplate.meteor.com/
<head>
<title>conditional templates</title>
</head>
<body>
Open the console. Session.set('active') to 'foo' or 'bar'. Then set the foo contents with Session.set('fooContents') to 'foofoo' and 'foobar'. Set bar contents with Session.set('barContents') to 'barfoo' or 'barbar', respectively.
{{> main }}
</body>
<template name="main">
{{{ contents }}}
</template>
<template name="foo">
<h2>Foos</h2>
{{ contents }}
</template>
<template name="foofoo">
The foo foos.
</template>
<template name="foobar">
The foo bars.
</template>
<template name="barfoo">
The bar foos.
</template>
<template name="barbar">
The bar bars.
</template>
<template name="bar">
<h2>Bars</h2>
{{ contents }}
</template>
if (Meteor.isClient) {
var getContainerContents = function() {
console.info('get container contents');
var active = Session.get('active') == 'foo' ? 'foo' : 'bar';
return Template[active]();
};
Template.main.contents = function() {
return getContainerContents();
};
Template.foo.contents = function() {
console.info('foo contents');
return fooContents();
};
var fooContents = function() {
var contents = Session.get('fooContents') == 'foo' ? 'foofoo' : 'foobar';
return Template[contents]();
};
Template.bar.contents = function() {
console.info('bar contents');
return barContents();
};
var barContents = function() {
var contents = Session.get('barContents') == 'foo' ? 'barfoo' : 'barbar';
return Template[contents]();
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment