Closure Tools

Frequently Asked Questions

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

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.

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

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.

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

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

Is there a way to avoid repeating parameters when calling subtemplates?
Yes. Instead of this:
{call .mySubtemplate}
  {param aaa: $aaa /}
  {param bbb: $bbb /}

Simply use data="all", like this:
{call .mySubtemplate data="all" /}
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?

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.)

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

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

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

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.

Authentication required

You need to be signed in with Google+ to do that.

Signing you in...

Google Developers needs your permission to do that.