bqw5189 / closure-templates

Automatically exported from code.google.com/p/closure-templates
Apache License 2.0
0 stars 0 forks source link

Need ability to have soy use string property names #39

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?
1.JSON from 3rd party server, not compiled by closure
2.SOY only compiles using standard property name syntax
3.Resulting properties don't match JSON

Soy with phone number compiles to
return soy.$$escapeHtml(opt_data.Phone);

I would like a way to make it say 
return soy.$$escapeHtml(opt_data['Phone']);

Option 1: Use string inline, don't declare parameter.  This seems the easiest 
way to make it work, but may have typos not found by compiler.
${'Phone'} or ${obj_data['Phone'])

Option 2: Flag parameter or template so parameters use strings for property 
names.  Soy would then compile to use strings for properties so closure would 
not rename them:
/**
 * @param Phone Phone number
 * @norename
 */
{template .test}
{$Phone}
{/template}

Option 3: Option to automatically generate externs file.  This is sub-optimal 
because the names may be used elsewhere where they could be compiled and 
integrating the output into the build process is more difficult.  

Whether the object is from closure or not is known at compile time, managing 
externs for every possible property on all instances where non-compiled JSON is 
used is more work and I think more error prone than being able to set it in the 
template.  I could possibly write a patch, but I don't think the project is 
taking contributions.

Original issue reported on code.google.com by jason.go...@gmail.com on 6 Jul 2011 at 6:33

GoogleCodeExporter commented 9 years ago
Instead of using the JSON object directly as template data, please put the JSON 
object as one of the data values. I.e.

var templateData = {myJsonObj: ...};

Then within the template, use

{$myJsonObj['Phone']}

Original comment by kai.hu...@gmail.com on 7 Jul 2011 at 9:34

GoogleCodeExporter commented 9 years ago
Ah, I hadn't thought of that.  Thanks!

Original comment by jason.go...@gmail.com on 7 Jul 2011 at 9:42