ObjectiveC builtin template system ?
Posted
by Aurélien Vallée
on Stack Overflow
See other posts from Stack Overflow
or by Aurélien Vallée
Published on 2010-03-29T16:25:20Z
Indexed on
2010/03/29
16:53 UTC
Read the original article
Hit count: 1237
I am developing an iPhone application and I use HTML to display formatted text.
I often display the same webpage, but with a different content. I would like to use a template HTML file, and then fill it with my diffent values.
I wonder if ObjectiveC has a template system similar to ERB in Ruby.
That would allow to do things like
Template:
<HTML>
<HEAD>
</HEAD>
<BODY>
<H1>{{{title}}}</H1>
<P>{{{content}}}</P>
</BODY>
</HTML>
ObjectiveC (or what it may be in an ideal world)
Template* template = [[Template alloc] initWithFile:@"my_template.tpl"];
[template fillMarker:@"title" withContent:@"My Title"];
[template fillMarker:@"content" withContent:@"My text here"];
[template process];
NSString* result = [template result];
[template release];
And the result string would contain:
<HTML>
<HEAD>
</HEAD>
<BODY>
<H1>My Title</H1>
<P>My text here</P>
</BODY>
</HTML>
The above example could be achieved with some text replacement, but that would be a pain to maintain. I would also need something like loops inside templates. For instance if I have multiple items to display, i would like to generate multiple divs.
Thanks for reading :)
© Stack Overflow or respective owner