开发者

Objective-C builtin template system?

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 different values.

I wonder if Objective-C 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>

Objective-C (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 :)


Have you considered using as template:

<HTML>
  <HEAD>
  </HEAD>
  <BODY>
    <H1>%@</H1>
    <P>%@</P>
  </BODY>
</HTML>

And then:

// just to get file name right
NSString* fn = 
    [NSString stringWithFormat:@"%@/my_template.tpl", 
              [[ NSBundle mainBundle ] resourcePath ]];
// template
NSError *error;
NSString* template = 
    [NSString stringWithContentsOfFile:fn 
              encoding:NSUTF8StringEncoding error:&error];
// result
NSString* result = 
    [NSString stringWithFormat:template, 
              @"MyTitle", 
              @"MyText"];

I think it's pretty much what you want.

Of course you'll have to add your template files as resources in the project.


https://github.com/groue/GRMustache may help you.


No, Objective-C has no built-in template system. Generally for simple uses you'd just use textual replacement (possibly via stringWithFormat:) and for something more advanced you'd choose a full-fledged template system that suits your needs.


This works for me: http://mattgemmell.com/2008/05/20/mgtemplateengine-templates-with-cocoa/


Here is a lightweight template engine for objective-c : CCTemplate


In Swift 3.1

var fn: String = "\(Bundle.main.resourcePath)/my_template.tpl"
// template
var error: Error?
var template = try? String(contentsOfFile: fn, encoding: String.Encoding.utf8)
    // result
var result = String(format: template, "MyTitle", "MyText")


For many of you likely to be unlikely option, but I needed templates to generate code and chose to use java + ftp, http://freemarker.org/libraries.html

I ended up with a tool to generate tableviews, form views, collection views, google drive integration based on the data model xcdatamodeld file.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜