Frequently Asked Questions

Why Closure Templates?

  • Question: Why did Google create another template system for its AJAX applications instead of using an existing solution?

  • Answer: There are many reasons why we created a new solution:

    • Better performance. We wanted JavaScript templates that aren't parsed on the client. Closure Templates compiles the templates server-side into efficient JavaScript functions, which can then be rendered client-side.

    • One template for client and server code. We wanted templates that have a language-neutral syntax such that you can write a template once and reuse it from both server-side Java and client-side JavaScript. With Closure Templates, you can render your application's initial UI server-side and then have the client-side JavaScript modify specific parts of the UI by re-rendering some of the templates that make up parts of the page.

    • Natural for programmers. We wanted a template syntax that was comfortable for programmers, without the limitations of XML-based syntax. Closure Templates give programmers the ability to have many templates in the same source file and for templates to call each other like functions.

    • Easy-to-use but high-quality localization. In Closure Templates, placeholders in messages usually have meaningful names, rather than opaque names like {1}, {2}, etc, which improves the quality of translations. Furthermore, these placeholder names are automatically generated from the corresponding variable names, which means you don't have to specify them manually.

Generating JavaScript

  • Question: How do I make the Closure Templates compiler generate JavaScript code that works nicely with the Closure Library?

  • Answer: Use the compiler option --shouldProvideRequireSoyNamespaces, which generates calls to Closure Library's goog.provide and goog.require instead of JavaScript code to define the namespace objects.


  • Question: Can I use Closure Templates if I'm not using Closure Library and Closure Compiler?

  • Answer: Yes. By default, Closure Templates does not assume you're using any particular library or compiler for JavaScript.

  • Question: Can I use Closure Templates from a language other than Java or JavaScript?

  • Answer: Not at this time. However, it should be possible to add support for any other language because the template syntax is language-neutral.

Tips & tricks

  • Question: Is there a way to avoid repeating parameters when calling subtemplates?

  • Answer: Yes. Instead of this:

    {call .mySubtemplate}
      {param aaa: $aaa /}
      {param bbb: $bbb /}

    Simply use data="all", like this:

    {call .mySubtemplate data="all" /}
  • Question: My Java server serves JavaScript code that uses Closure Templates. Can I set up my server so that in development mode, modified templates are automatically recompiled to JavaScript when the UI is refreshed?

  • Answer: Yes. Your server can use the Java API to compile your templates to JavaScript. Specifically, you need to create a SoyFileSet that contains your application's template files and then call SoyFileSet.compileToJsSrc() each time you want to recompile. (You'll need to separately set up detection of when your source files have been modified.)

  • Question: Can the Closure Templates compiler generate JavaScript code that uses string concatenation instead of a StringBuilder?

  • Answer: Yes, you can pass the option --codeStyle concat to the template compiler. For details, please see the section in Closure Templates documentation on JavaScript code styles.