首页 > 代码库 > 使用模板引擎渲染HTML界面
使用模板引擎渲染HTML界面
使用模板引擎渲染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;
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;
}
效果:
使用模板引擎渲染HTML界面
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。