New*_*biZ 6 templates file objective-c
我正在开发一个iPhone应用程序,我使用HTML来显示格式化的文本.
我经常显示相同的网页,但内容不同.我想使用模板HTML文件,然后用我的不同值填充它.
我想知道Objective-C是否有类似于Ruby中的ERB的模板系统.
这样就可以做到这样的事情
模板:
<HTML>
<HEAD>
</HEAD>
<BODY>
<H1>{{{title}}}</H1>
<P>{{{content}}}</P>
</BODY>
</HTML>
Run Code Online (Sandbox Code Playgroud)
Objective-C(或者它可能在理想世界中)
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];
Run Code Online (Sandbox Code Playgroud)
结果字符串将包含:
<HTML>
<HEAD>
</HEAD>
<BODY>
<H1>My Title</H1>
<P>My text here</P>
</BODY>
</HTML>
Run Code Online (Sandbox Code Playgroud)
上面的例子可以通过一些文本替换来实现,但这将是一个难以维护.我还需要像模板中的循环一样的东西.例如,如果我要显示多个项目,我想生成多个div.
谢谢阅读 :)
您是否考虑过使用模板:
<HTML>
<HEAD>
</HEAD>
<BODY>
<H1>%@</H1>
<P>%@</P>
</BODY>
</HTML>
Run Code Online (Sandbox Code Playgroud)
然后:
// 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"];
Run Code Online (Sandbox Code Playgroud)
我认为这几乎是你想要的.
当然,您必须将模板文件添加为项目中的资源.
| 归档时间: |
|
| 查看次数: |
3715 次 |
| 最近记录: |