javascripttemplatesinheritancegoogle-closure-templatessoy-templates

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

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.

project.ButtonCircle needs little changes made in project.createButton template.

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.

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

Yet in this case we need to copy-paste all content from project.createButton to project.createCircleButton. That's terrible.

Also we tried using Delegate Templates, but it's not suited for inheritance.

What is approach towards this problem?


Solution

  • We just write preprocessor that add @extends to soy.