[stringtemplate-interest] filling a field ($...$) with data that also contains a (part of) a field ($...$test)

Jonathan Buhacoff jonathan at buhacoff.net
Mon Feb 1 22:05:29 PST 2010


I worked on a project where I had a similar setup.

I was able to render in just one pass by wrapping each value from the  
database with an ST object before adding it to the primary template's  
attributes.

Try  setAttribute(name, new StringTemplate(GetData());  ... as long as  
the return value of GetData() is actually valid template text.  If  
it's something else you'll have to recurse.

In my project I wrote a custom STG that wraps my values in ST on the  
fly.

Jonathan

On Feb 1, 2010, at 10:36 AM, Terence Parr wrote:

> Seems like doing it twice is the right way because you actually have  
> to templates: one that contains the template pulled in from a  
> database. Unless the outer template is simply a single hole like  
> $thedata$, it seems like you really do need to templates, which  
> means you need to render twice. Of course, you could patch the  
> templates together into one and then call toString once if you want.
> T
> On Feb 1, 2010, at 2:19 AM, Christophe Vanderhaeghen wrote:
>
>> Hi all,
>>
>> I have a template (myTemplate.st) which is filled with data from  
>> the database.
>> In the database, the fields might also contain stringtemplate- 
>> fields ($...$), thus allowing in the database some dynamics as well.
>>
>> What I do now is loop twice through the text where the second wave  
>> uses the string-result from the first wave. Like this:
>> StringTemplateGroup group = new StringTemplateGroup("Templates",  
>> Application.StartupPath +"/Templates/");
>> StringTemplate template = group.GetInstanceOf("/Module");
>> template.SetAttribute(GetData().GetType().Name, GetData()); //Get  
>> the data from the database
>> string firstWave = template.ToString(); //Fill the fields with the  
>> data that came from the database
>>
>> //Now do it for a second time so that the dynamic items from the  
>> database are formatted also:
>> StringTemplate secondWave = new StringTemplate(firstWave);
>> secondWave.SetAttribute(GetData().GetType().Name, GetData());
>> string x = secondWave.ToString();
>> Question: Is there an alternative way to get all fields filled at  
>> once so that I don’t have to StringTemplate twice?
>>
>> Thanks in advance,
>>
>> Christophe
>>
>>
>>
>> _______________________________________________
>> stringtemplate-interest mailing list
>> stringtemplate-interest at antlr.org
>> http://www.antlr.org/mailman/listinfo/stringtemplate-interest
>
> _______________________________________________
> stringtemplate-interest mailing list
> stringtemplate-interest at antlr.org
> http://www.antlr.org/mailman/listinfo/stringtemplate-interest



More information about the stringtemplate-interest mailing list