有没有办法用dust.js做多个级别的继承值覆盖? [英] Is there a way to do more than one level of inheritance value overrides with dust.js?

查看:128
本文介绍了有没有办法用dust.js做多个级别的继承值覆盖?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

模板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}

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

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

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

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?

推荐答案

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

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?

这篇关于有没有办法用dust.js做多个级别的继承值覆盖?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆