Closed GoogleCodeExporter closed 8 years ago
In case anyone is running into the same problem, I'm post-processing code with
this command:
sed -e 's/opt_data\.\([a-zA-Z0-9]\+\)/opt_data["\1"]/g' test.js
Original comment by hugo.gfi...@gmail.com
on 18 Jul 2010 at 9:26
This is not a bug. The Closure Templates compiler (Soy compiler) is working
correctly. Let me explain the cases below.
Case 1 (general answer): In order to successfully use Closure Compiler with
Closure Templates, you should be compiling ALL of your JavaScript code together
with Closure Compiler -- this means both your hand-written JS code that calls
the templates and the JS code generated by Closure Templates. Then the name
'myParam' will be renamed to the same short name in both your hand-written JS
code and in the template JS code. Does that make sense?
Case 2: In the case where it's absolutely impossible for you to compile all
your JS together using the Closure Compiler (e.g. if you're getting template
data from the server so that your data keys cannot be processed by the Closure
Compiler), you'll want to use bracket notation and string keys to access the
data in your template code, which is perfectly valid syntax. However, your
template param names (the top level names in your template data) cannot be
accessed using bracket notation and string keys. Therefore, you'll need to
wrap the json data with the un-renamed keys in another object before passing it
to the template. For example:
var jsonData = ... // retrieve your JSON data from server
var templateData = {jsonData: jsonData};
var output = myNamespace.test1(templateData);
Here's what your template would look like:
{namespace myNamespace}
/** @param jsonData */
{template .test1}
Test1 {$jsonData['myParam']}
{/template}
Does that make sense?
Original comment by kai.hu...@gmail.com
on 18 Jul 2010 at 11:35
Yes, you nailed it!
I am using both cases.
For case 1, although all the code was compiled together, some template calls
were in the form:
myNamespace.test1({"myParam": "foo"})
rather than:
myNamespace.test1({myParam: "foo"})
so just making sure I allowed the closure compiler to do the renaming of all
instances of myParam solved the issue (rather than forcing the compiler not to
rename any instance of myParam).
Case 2: I was not aware of this json wrapping technique / notation. Thanks for
sharing, I'll use it!
Thanks for your time Kai!!
Original comment by hugo.gfi...@gmail.com
on 19 Jul 2010 at 2:45
No problem. Now you understand some of the tricks in using Closure Compiler
and Closure Templates together.
Original comment by kai.hu...@gmail.com
on 19 Jul 2010 at 7:46
Original issue reported on code.google.com by
hugo.gfi...@gmail.com
on 18 Jul 2010 at 8:58