模板 – 有没有办法用dust.js做多个级别的继承值覆盖?

我正在使用灰尘模板,设计的一个方面一直困扰着我.这让我想知道我“做错了”所以我想我会问S.O.有没有办法在dust.js中使用块和内联部分创建多级继承?假设您有一个带有布局的基本模板,一个覆盖某些内容的继承模板,然后是另一个继承自该模板的模板,希望有选择地覆盖某些内容.通常情况下,我认为它可以通过最后一个继承模板来提供最终的覆盖值.但是,内联部分语法似乎只能在单个级别上工作.这是一个人为的例子,应该展示我在说什么.

模板1 test_base.dust:

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

模板2 test_level1.dust:

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

模板3 test_level2.dust:

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

渲染这些模板我得到以下结果:

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>"

我已经阅读了几次文档,但似乎你可以从多个级别继承模板,但是你只能覆盖单个模板定义的块.所以我“做错了”?有没有办法使用块和内联部分使用选择性覆盖来完成多级继承?还有另一种方法可以做到这一点,并保持模板DRY?

最佳答案 不幸的是,它看起来不支持这种继承模型.在linkedin fork:
https://github.com/linkedin/dustjs/issues/101上找到了这个讨论

该讨论引用了文档中的特定段落:

… 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.

所以我对此有两种解释:一种是非确定性的,可能是基于模板的编译方式.第二种可能的解释是全局定义总是取代它所定义的模板和模板调用的任何模板.如果这是正确的解释,那么父模板中的内联部分将始终“赢”,如果它们覆盖的块在继承层次结构中处于同一级别或更高级别.这与我期望的相反,并且使得内联部分用于多级继承是不可能的(至少使用相同的命名块).

似乎这是一个死胡同,关于如何在尘埃中完成这种行为的任何其他想法?

点赞