0
votes

I have two meteor apps. Each one has a smart-package that implements a certain feature, lets say user management, in an app-specific way. I also have a third meteor smart package that lives in a shared package directory. This third package has user management templates that are common to both apps. I use iron-router.

I need to combine the shared templates with the app templates. Iron-Router has the yield keyword. This is useful for combining templates. The problem is that this only works for special, nominated layout templates.

How do you combine, or nest, templates that are not layout templates using meteor and iron-router?

Example

In the simplified example below you can see three packages and their corresponding templates. The app-*:user packages implement app specific user templates and the shared:user package implements a shared user template.

app-1:users

appUser.tpl.jade

p This is some App-1 specific text

app-2:users

appUser.tpl.jade

p This is some App-2 specific text

shared:users

user.tpl.jade

h1 This is a shared title
+appUser

routes.coffee

Router.route '/user', name: 'user'

The problem here is that the nested template must be called appUser in both the dependent smart packages. This not only prevents the templates name being appropriately descriptive for the app but, more worryingly, tightly couples the template name and makes for fragile code.

It is to prevent this tight coupling of templates that iron-router allows us to use the yield keyword, but since we cannot use the yield keyword in this context (can we?) then I am left wondering if this truly is the only way to implement shared templates?

1
What is a non-layout template? Meteor templates are by definition HTML/blaze. - Michel Floyd
With iron-router certain templates can be designated as layout templates. Such templates can take advantage of the yield keyword to dynamically include route specific detail templates. A non-layout template is a template that has not been so designated and therefore cannot use the yield keyword. - biofractal
Blaze templates are really all the same, if there's a {{> yield}} then it can be used as the parameter to layoutTemplate: in i-r. Any template can nest another one directly with {{> templateName }} without using i-r. So what exactly are you trying to do? Can you show code and what's not working? - Michel Floyd
@MichelFloyd I have added an example. I hope that makes things clearer - biofractal

1 Answers

1
votes

You're probably going to benefit from dynamic templates. These are templates whose name can be passed in via a helper (defined globally or tied to the parent template) and whose data context can even be set by a helper.

{{> Template.dynamic template=myTemplate [data=myData] }}

Your code supplies myTemplate and myData dynamically here and the kerbobble (html, helpers, event handlers) then gets stuffed into the parent template.