使用模板引擎渲染HTML介面,模板引擎html介面
使用模板引擎渲染HTML介面by 伍雪穎
模板引擎:GRMustachepod '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;}
效果: