簡體   English   中英

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

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

如果可能的話,我想知道如何逐步集成Google Closure Template的打印插件(又名Soy),主要是因為我對Java很不好。 下面的頁面說明了如何執行此操作,但是我需要更多詳細信息。

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

  • 可以將print指令簡單地用作`{myprintformat $ var}'。
  • (其他問題)您認為我們可以將`goog.require('xxx')'語句編譯為javascript嗎? 如果可以的話,我們可以提供功能並從soy.js要求它。

任何幫助表示贊賞。

您需要查看Clojure源代碼,以了解它如何創建自己的指令。 這很容易。

首先,您需要了解如何實施指令。 為此,請參見示例。 下載clojure模板源代碼並查看:

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

然后,您需要了解一點點Google Guice 創建一個Guice模塊以添加您的指令:

public class MySoyModule extends AbstractModule {

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

}

然后,使用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();

現在您可以調用模板:

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

而已。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM