Generic template with multiple tests or multiple templates?


#1

Hello everyone,

I have a denormalized database, my collections often have elements with
common fields but also with several different ones. In a normalized
database, they will be in several tables with inheritance.

Here is an exemple to understand the situation:
Let’s say the Element collection contains three “types” of “elements”:

TypeA(_id, text, owner, username, type, sharedProperty1, sharedProperty2, propertyA1, ..., propertyA5),
TypeB(_id, text, owner, username, type, sharedProperty1, sharedProperty2, propertyB1, ..., property B4),
TypeAB(_id, text, owner, username, type, sharedProperty1, sharedProperty2, propertyA1, propertyA2, propertyB1, propertyB3)

I was wondering what was the best (most efficient) way to render these elements between:

  1. multiple tests in a same generic template called for each elements

    {{#each typeA}}{{>element}}{{/each}}
    some stuff
    {{#each typeB}}{{>element}}{{/each}}
    some stuff
    {{#each typeAB}}{{>element}}{{/each}}

  2. multiple templates called for each type

    {{#each typeA}}{{>templateA}}{{/each}}
    some stuff
    {{#each typeB}}{{>templateB}}{{/each}}
    some stuff
    {{#each typeAB}}{{>templateAB}}{{/each}}

I can’t use only few tests in a generic template in order to render what I want, I also need to separate them.

The problem with the second solution is that I need to duplicate code (helpers) since these elements share properties or I need to share these helpers with Template.registerHelper which seems clumsy to me and that’s the problem.

For the moment, I’ve choose the second solution, because I think several tests (around 10 “if” and “unless” operations) for each “element” will slow the application. I don’t know (but I’ll learn it for sure) how to profile meteor apps yet, that’s why I’m asking this question.

Edit: I just thought about using the first solution with some redudancy inside the template:

<template name="element">
{{#if typeA}}
    code of templateA
{{else}}
    {{#if typeB}}
        code of templateB
    {{else}}
        code of templateAB
    [[/if}}
{{/if}}
</template>

and still:
{{#each typeA}}{{>element}}{{/each}}
some stuff
{{#each typeB}}{{>element}}{{/each}}
some stuff
{{#each typeAB}}{{>element}}{{/each}}

What about this last solution ?
Is redundancy in templates a problem for bandwidth ?