简体   繁体   English

如何在大项目的闭包模板中继承而不继承?

[英]How can live without inheritance in closure templates in big project?

We use closure library and closure compiler, and we want to use closure templates. 我们使用闭包库和闭包编译器,我们想要使用闭包模板。

But closure templates haven't got inheritance. 但是闭包模板没有继承。 That's really a problem for us. 这对我们来说真的是一个问题。

As I understand, the reason why closure templates don't have inheritance, is because templates must be simple, and easy to read. 据我所知,闭包模板没有继承的原因是因为模板必须简单,易于阅读。

But how can you live without inheritance in big projects? 但是你怎么能在没有大项目继承的情况下生活?

For example, we have a template file button.soy that generates button with public template project.createButton and private templates: project.createOpenTag_ , project.createCSSClasses_ , project.createAttributes_ , project.createContent_ , project.createCloseTag_ . 例如,我们有一个模板文件button.soy ,它生成带有公共模板project.createButton和私有模板的按钮: project.createOpenTag_project.createCSSClasses_project.createAttributes_project.createContent_project.createCloseTag_

We have JavaScript class project.Button , and we have project.ButtonCircle (perhaps this separate class project.ButtonCircle seems unnecessary, but it's just an example) which extends project.Button . 我们有JavaScript类project.Button ,我们有project.ButtonCircle (也许这个单独的类project.ButtonCircle似乎没必要,但它只是一个例子)扩展了project.Button

project.ButtonCircle needs little changes made in project.createButton template. project.ButtonCircle需要在project.createButton模板中进行很少的更改。

Of course we can add new functionality to project.createButton , but it's a very bad idea, because such approach will create monster templates in the future. 当然我们可以为project.createButton添加新功能,但这是一个非常糟糕的主意,因为这种方法将来会创建怪物模板。

Or we can create public template project.createCircleButton in file button-circle.soy , call all private templates from project.createButton in it, and when we need to 'override' one of these private templates, (for example project.createCSSClasses_ ), we just create new private template in button-circle.soy with name project.createCSSClassesCirbleButton_ . 或者,我们可以创建公共模板project.createCircleButton文件button-circle.soy,呼吁各民营模板project.createButton在里面,当我们需要“越权”的这些私人模板之一,(例如project.createCSSClasses_ )我们只需在button-circle.soy中创建名为project.createCSSClassesCirbleButton_新私有模板。

Yet in this case we need to copy-paste all content from project.createButton to project.createCircleButton . 但在这种情况下,我们需要将project.createButton所有内容复制粘贴到project.createCircleButton That's terrible. 这太可怕了。

Also we tried using Delegate Templates, but it's not suited for inheritance. 我们也尝试使用委托模板,但它不适合继承。

What is approach towards this problem? 解决这个问题的方法是什么?

It's hard to gather your exact use case but having used soy/closure extensively at my time at Google I sympathize with your puzzlement as they're not my favorite. 很难收集你的确切用例,但在我在谷歌的时候广泛使用大豆/关闭我同情你的困惑,因为他们不是我的最爱。 I agree with @Francois-Richard's general suggestion for keeping common templates very small and composing multiple together. 我同意@Francois-Richard的一般建议,即保持常见模板非常小并将多个模板组合在一起。 The soy model (and many other JS templating systems I've used frankly) strongly favor composition over inheritance. 大豆模型(以及我曾经坦率使用的许多其他JS模板系统)强烈支持组合而不是继承。

Extension 延期

If a CircleButton is logically and stylistically the same but just with added functionality or styling, then composition will work great. 如果CircleButton在逻辑上和风格上相同,但只是添加了功能或样式,那么合成将很有效。

<div class="circle">
  {call .button}
</div>

Parameterization 参数

If a CircleButton is logically the same but stylistically different, why not allow Button to be parameterized by shape and reuse the same template? 如果CircleButton在逻辑上相同但风格上不同,为什么不允许Button按形状参数化CircleButton用相同的模板?

{call .button shape="circle" /}

Composition 组成

If a CircleButton is neither logically nor stylistically the same but merely share some base elements, then extract those to templates/classes and use composition. 如果CircleButton在逻辑上和风格上都不相同,只是共享一些基本元素,那么将它们提取到模板/类并使用组合。

<div class="circle">
  {call .buttonSharedA /}
  <span>{call .buttonSharedB /}</span>
</div>

Soy really doesn't jive as well with the rest of closure vis a vis OO-thinking IMHO, and just requires a different approach. 对于OO思维的恕我直言,大豆真的没有和其他关闭一样,只需要一个不同的方法。

I would isolate every common template functions/element and build templates with a factory module. 我会隔离每个常见的模板函数/元素,并使用工厂模块构建模板。 Every templates would be a composition a several small elements and functions. 每个模板都是一个组成几个小元素和功能。

我们只是编写将@extends添加到大豆的预处理器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM