0

I'll try to make my problem seem simple, hoping that the solution will be.

Let's state that we have two templates as following :

html(data,value,charts) ::= <<
    <!DOCTYPE HTML>
    <html>
    [...]
        var $data$ = $value$;
        $charts$
    [...]
    </html>
>>

chart(chartname,data,categoryname,graphs) ::= <<
    var $chartname$;
    $chartname$.dataProvider = $data$;
    [...]
>>

In Java, I have to do something like:

ST myPage = group.getInstanceOf("html");
ST chart1 = group.getInstanceOf("chart");
ST chart2 = group.getInstanceOf("chart");
myPage.add("data", xxx );
chart1.add("data", xxx );
chart2.add("data", xxx );

BUT, "data" is the same for all (code duplication!), so I would like to apply the "chart" template on data in my "html" template with something like :

html(data,value,charts) ::= <<
    [...]
        var $data$ = $value$;
        $charts(data)$
    [...]
>>

Which is not possible, because the "chart" template as more parameter than just "data".

My question is : How to call the "chart" template, using "html"."data" for "chart"."data" but by keeping the usual way to set parameter at runtime with .add(String, Object) ..?

Thank you for your time !

Yohannan
  • 1
  • 1

1 Answers1

0

One option is to remove the data parameter from the chart template. Doing so would allow cause the reference to data within chart to look up the attribute in the calling context (e.g. the html template). However, this would mean the following as well:

  1. You could not explicitly set the data argument for an instance of a chart template (the last 2 lines in your Java code would throw an exception).
  2. The chart template could not be rendered independently. If you needed to render a chart template without an html template, you'd need to define a new template like standaloneChart(data, chart) that would allow you to render the chart template in a context that defines data.
Sam Harwell
  • 97,721
  • 20
  • 209
  • 280
  • As I have an composition relation between my chart and my html page, I can deal with those two consequences. Can you please develop your advice about "look up the attribute in the calling context" ? How could I do ? (Thank you by the way ) – Yohannan Oct 28 '13 at 14:08