What is the correct way to use soyutils with Google Closure?

1.2k Views Asked by At

I am trying to use Google Closure templates (Soy) with Google Closure.

I am including the soyutils_usegoog.js utilities file as instructed. This file provides a number of utilities used by the generated templates, notably soy.StringBuilder. Here's how it is creating it:

soy.StringBuilder = goog.string.StringBuffer;

The soyutils file requires goog.string.StringBuffer a few lines above, but when running in non-compiled mode this results in a runtime error because the JS file that StringBuffer resides in will not be loaded until after soyutils has executed.

Unless I am mistaken, JS files in Closure should not immediately access namespaces that they 'require'. The <script> tag is only added after the execution of the current script (in non-compiled mode) so immediate usage will result in a runtime error.

In short, how can I load in soyutils_usegoog.js without triggering a runtime error due to the early access of good.string.StringBuffer.

2

There are 2 best solutions below

0
On

Perhaps the question has been asked on http://groups.google.com/group/closure-templates-discuss?

0
On

Are you recreating deps.js with your compiled templates as an input (and soyutils_usegoog.js)? And then are you goog.require-ing your template? Something like this should work:

<script src="/closure-library/closure/goog/base.js"></script>
<script src="/closure-library/closure/goog/deps.js"></script><!-- might not need this line if base.js is setup to auto include deps.js -->
<script>
    goog.require('your.template');//this will pull in and execute all the dependencies (including StringBuffer) for your template
</script>
<script>
    alert(your.template());
</script>