bqw5189 / closure-templates

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

Allow calls to arbitrary templates by making template name an expression #40

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
I would like a way of dynamically calling a template based on an expression, 
rather than hard-coding a template reference in the code.

For example, at the moment we have templates that contain code like this:

    {switch $item.type}
        {case 'AA'}
            {call dynamicview.aa data="$item" /}
        {case 'BB'}
            {call dynamicview.bb data="$item" /}
        {case 'CC'}
            {call dynamicview.cc data="$item" /}
            .
            .
            .
        {default}
            <p>Unknown view.</p>
    {/switch}

It would be much easier to be able to specify the name of the template 
dynamically, something like this:

    {call $item.templateName data="$item" /}

This would be useful for us because we dynamically generate new templates at 
runtime, and so the names of our dynamic templates are not known during 
development. Instead we have a meta-template which we run through Closure 
Templates to generate the template code above, then we run that template 
through Closure Templates again to get our final compiled template.

It was a fairly complex process to set up this meta-template system, and it is 
becoming tricky to maintain. Being able to dynamically specify the template 
name would simplify things greatly.

Original issue reported on code.google.com by charlesg...@optusnet.com.au on 19 Aug 2011 at 7:27

GoogleCodeExporter commented 9 years ago
The compiler needs to know the targets of all calls at compile time. So this 
feature will not happen.

Original comment by kai.hu...@gmail.com on 19 Aug 2011 at 8:58

GoogleCodeExporter commented 9 years ago
Charles, if you need to compile templates only to JavaScript than you can 
define a custom JS function that will accept reference to template as parameter.

Example: 

// Expects param 'func' to be a function.
tmpl.util.callFunction = function (opt_data) {
    return opt_data.func.call(null, opt_data);
};

Usage:

{call tmpl.util.callFunction}
    {param func: $item.templateFunction /}
    {param item: $item /}
{/call}

Original comment by alex.tra...@gmail.com on 6 Jul 2012 at 3:42

GoogleCodeExporter commented 9 years ago
The deltemplate feature is available.
Doc: 
https://developers.google.com/closure/templates/docs/commands#delegates-with-var
iant

Original comment by viktor.m...@gmail.com on 23 Oct 2013 at 5:35

GoogleCodeExporter commented 9 years ago
deltemplate solves the issue both at JS and Java ends, but is it correct usage 
to have template name same always?

Original comment by divya.p...@gmail.com on 17 Sep 2014 at 8:23