AngelDoReMi / closure-templates

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

Templates with only optional parameters not callable in js #50

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
Let's say I have a template that accepts only optional parameters. Example:

/**
 * Renders a checkbox.
 * @param? checked {boolean} Whether to render the checkbox as checked.
 */
{template .checkbox}
<input type="checkbox" {if $checked}checked="checked"{/if}>
{/template}

/** Calls checkbox. */
{template .main}
 {call .checkbox /}
{/template}

This example causes a Javascript error: "Uncaught TypeError: Cannot read 
property 'undetermined' of null".

One proposed fix for this is to add, in the generated template js:
  opt_data=opt_data || {};
Another fix would be to pass {} for empty data instead of null. (Though that 
would possibly break hasData() and might not be a good idea).

A workaround exists, fortunately: change the call to be {call .checkbox 
data="[:]" /}. But it would be nice for the workaround to not be necessary.

Original issue reported on code.google.com by jacobly@google.com on 18 Oct 2011 at 7:45

GoogleCodeExporter commented 8 years ago
The hasData() function exists precisely for this case.
See http://code.google.com/closure/templates/docs/concepts.html#functions

Specifically in your case, rewrite it as
    {if hasData() and $checked}...{/if}

Original comment by kai.hu...@gmail.com on 18 Oct 2011 at 7:52

GoogleCodeExporter commented 8 years ago
Sure, that's another workaround. It's still in some sense breaking an 
abstraction, exposing a difference between templates with zero required 
arguments and templates with one required argument. It would be cleaner if the 
workaround were not required.

Original comment by jacobly@google.com on 18 Oct 2011 at 8:40

GoogleCodeExporter commented 8 years ago
Yes, it's something we probably should fix eventually, though not super high 
priority.

Original comment by kai.hu...@gmail.com on 21 Oct 2011 at 12:25