0

I would like to know how to integrate a print plugin of Google Closure Template, a.k.a. 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.

1 Answers1

0

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.

feroult
  • 493
  • 5
  • 12