简体   繁体   English

如何集成Google Closure模板的打印指令(Soy)

[英]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. 如果可能的话,我想知道如何逐步集成Google Closure Template的打印插件(又名Soy),主要是因为我对Java很不好。 Below page explains how to do that, but I need more detail one. 下面的页面说明了如何执行此操作,但是我需要更多详细信息。

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

  • It is all fine that the print directive is simply used as ` {myprintformat $var} '. 可以将print指令简单地用作`{myprintformat $ var}'。
  • (Additional question) Do you think we can compile `goog.require('xxx')' statement out into javascript? (其他问题)您认为我们可以将`goog.require('xxx')'语句编译为javascript吗? If it could, we can provide functions and require it from soy.js. 如果可以的话,我们可以提供功能并从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. 您需要查看Clojure源代码,以了解它如何创建自己的指令。 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: 下载clojure模板源代码并查看:

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

Then, you'll need to understand a litte bit of Google Guice . 然后,您需要了解一点点Google Guice Create a Guice module to add your directives: 创建一个Guice模块以添加您的指令:

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: 然后,使用Guice注入器实例化您的构建器,如下所示:

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. 而已。

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

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