[stringtemplate-interest] File based templates and maps.
Caleb Lyness
caleb.lyness at ezswitch.net
Thu Oct 19 10:08:01 PDT 2006
Hi,
Thank you for the quick response. I managed to do what I wanted (using a
group file).
Here is another interesting thing I have been playing with:
$(headerTemplate)(); null=headers/default()$
It includes a default header template if the attribute has not been set.
My first try was this:
$(headerTemplate; null="headers/default")()$
This does not work? should it though? cf: </(*expr*)/(/argument-list/)>
I think it would be nice for future newbies if there was an example
along side the file based
template (.st) method in the source. Would speed the learning curve up a
bit.
cf: src\org\antlr\stringtemplate\test.
Here is quick .stg example:
te.stg:
group groupName;
typeInitMap ::= [
"Integer":"0",
"long":"1",
default:"null"
]
variable(type,name) ::= <<
<b>$type$</b> $name$ = $typeInitMap.(type)$;$\n$
>>
t.java:
import java.io.File;
import java.io.FileReader;
import org.antlr.stringtemplate.StringTemplate;
import org.antlr.stringtemplate.StringTemplateGroup;
import org.antlr.stringtemplate.language.DefaultTemplateLexer;
public class t {
/* Quick and very nasty example: */
public static void main(String[] args)
throws Exception
{
File templateFileName = new File("te.stg");
StringTemplateGroup group = new StringTemplateGroup(
new FileReader(templateFileName),
DefaultTemplateLexer.class //Leave away if you want to
use angle brace notation
);
StringTemplate template = group.getInstanceOf("variable");
template.setAttribute("type", "Integer");
template.setAttribute("name", "id");
System.out.println(template.toString());
}
}
>> Hi,
>>
>> I have been trying to use a map for file based templates. So far no
>> luck. Is this actually
>> possible or have I missed the point. I have tried too many
>> combination to list them here, so
>> I just list the last thing I tried:
>>
>> hash map defined in reason.st:
>> ---
>> $[
>> "1":"reason for 1",
>> "2":"reason for 2",
>> "D":"reason for D",
>> default : "unknown reason"
>> ]$
>> ---
>>
>> Now this would make some sense, no?
>>
>
> Hi, sorry. Those maps only work in group file formats (.stg files).
>
> Terence
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org:8080/pipermail/stringtemplate-interest/attachments/20061019/918c06a5/attachment.html
-------------- next part --------------
A non-text attachment was scrubbed...
Name: smime.p7s
Type: application/x-pkcs7-signature
Size: 3269 bytes
Desc: S/MIME Cryptographic Signature
Url : http://www.antlr.org:8080/pipermail/stringtemplate-interest/attachments/20061019/918c06a5/attachment.bin
More information about the stringtemplate-interest
mailing list