Is there a way to do more than one level of inheritance value overrides with dust.js?
Asked Answered
B

1

4

I'm using dust templates and one aspect of the design has been bugging me. This makes me wonder if I'm "doing it wrong" so I thought I would ask S.O. Is there a way to create multiple-level inheritance in dust.js with blocks and inline partials? Lets say you have a base template with layout, an inheriting template that overrides some content, and then yet another template inheriting from that template that wishes to selectively override some content. Normally I would imagine it works by the last inheriting template providing the final overriding values. The inline partial syntax, however, seems to only work on a single level. Here's a contrived example that should show what I'm talking about.

template 1 test_base.dust:

<h1>
{+document_title/}
</h1>
{+content}
<p>Some great content.</p>
{/content}

template 2 test_level1.dust:

{>test_base/}
{<document_title}Level 1{/document_title}
{<content}
<p>Other great content</p>
{/content}

template 3 test_level2.dust:

{>test_level1/}
{<document_title}Level 2{/document_title}

Rendering these templates I get the following results:

dust.render('test_base', {}, function(err, data) { console.log(data); } );
"<h1></h1><p>Some great content.</p>"

dust.render('test_level1', {}, function(err, data) { console.log(data); } );
"<h1>Level 1</h1><p>Other great content</p>"

dust.render('test_level2', {}, function(err, data) { console.log(data); } );
"<h1>Level 1</h1><p>Other great content</p>"

I've read over the docs a few times but it seems like you can inherit templates from multiple levels but you can only override a single template defined block. So am I "doing it wrong"? Is there a way to accomplish multiple level inheritance with selective overrides using blocks and inline partials? Is there another way to do this and keep the templates DRY?

Bibliotheca answered 4/9, 2012 at 17:28 Comment(0)
B
2

Unfortunately it looks like this inheritance model isn't supported. Found this discussion on the linkedin fork: https://github.com/linkedin/dustjs/issues/101

That discussion referenced a particular passage in the docs:

... Inline partials never output content themselves, and are always global to the template in which they are defined, so the order of their definition has no significance. They are passed to all templates invoked by the template in which they are defined.

So I have two interpretations of this: one the order is non-deterministic and possibly based on how the templates are compiled. The second potential interpretation is that the global definition always supersedes for the template in which it's defined and any templates that template calls. If that's the correct interpretation then inline partials in parent templates will always 'win' if the block they're overriding is at that same level or higher in the inheritance hierarchy. That's the opposite of what I would expect and makes using inline partials for multi-level inheritance impossible (at least with the same named block).

Seems like this is a dead end, any other ideas on how this behavior could be accomplished in Dust?

Bibliotheca answered 4/9, 2012 at 18:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.