首页 > 代码库 > 使用模板引擎渲染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;
}

效果:
技术分享

使用模板引擎渲染HTML界面