简体   繁体   中英

How to integrate Print Directive of Google Closure Template (Soy)

I would like to know how to integrate a print plugin of Google Closure Template , aka Soy, step by step if you can, mainly because I'm pretty bad at Java. Below page explains how to do that, but I need more detail one.

https://developers.google.com/closure/templates/docs/plugins

  • It is all fine that the print directive is simply used as ` {myprintformat $var} '.
  • (Additional question) Do you think we can compile `goog.require('xxx')' statement out into javascript? If it could, we can provide functions and require it from soy.js.

Any helps are appreciated.

You'll need to take a look at the Clojure source code, to see how it create its own directives. It's quite easy.

First, you need to understand how to implement a Directive. To do that, see an example. Download the clojure templates source code and look into:

./java/tests/com/google/template/soy/basicdirectives/TruncateDirective.java

Then, you'll need to understand a litte bit of Google Guice . Create a Guice module to add your directives:

public class MySoyModule extends AbstractModule {

    @Override
    protected void configure() {        
        Multibinder<SoyPrintDirective> soyDirectivesSetBinder = Multibinder.newSetBinder(binder(), SoyPrintDirective.class);        
        soyDirectivesSetBinder.addBinding().to(DateDirective.class);
    }

}

Then, instantiate your builder, using the Guice injector, like this:

Injector injector = Guice.createInjector(new SoyModule(), new MySoyModule());
SoyFileSet.Builder sfsBuilder = injector.getInstance(SoyFileSet.Builder.class);
SoyFileSet sfs = sfsBuilder.add(SoyUtils.class.getResource(source)).build();

Now you can invoke your templates:

SoyTofu simpleTofu = sfs.compileToTofu().forNamespace("soy.examples.simple");

That's it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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