使用模板引擎渲染HTML界面

来源:互联网 发布:怎么配置java开发环境 编辑:程序博客网 时间:2024/04/29 07:16

使用模板引擎渲染HTML界面

by 伍雪颖

模板引擎:GRMustache
pod 'GRMustache','~> 7.3.0'

html模板:
template.html
<HTML>
<HEAD>
</HEAD>
<BODY>
<H1> {{ name }}</H1>
<P> {{ content }}</P>
</BODY>
</HTML>

调用:
- (void)viewDidLoad {
    [superviewDidLoad];
   
NSString *path = [[NSBundlemainBundle]bundlePath];
   
NSURL *baseUrl = [NSURLfileURLWithPath:path];
   
NSString *htmlString = [selfdemoFormatWithName:@"wuxueying"value:@"hello"];
    [
self.webViewloadHTMLString:htmlStringbaseURL:baseUrl];
}

- (
NSString *)demoFormatWithName:(NSString *)name value:(NSString *)value {
   
NSString *fileName =@"template.html";
   
NSString *path = [[[NSBundlemainBundle]bundlePath]stringByAppendingPathComponent:fileName];
   
NSString *template = [NSStringstringWithContentsOfFile:pathencoding:NSUTF8StringEncodingerror:nil];
   
NSDictionary *renderObject =@{@"name":name,@"content":value};
   
NSString *content = [GRMustacheTemplaterenderObject:renderObjectfromString:templateerror:nil];
   
return content;
}

效果:


1 0
原创粉丝点击