使用模板引擎渲染HTML界面
by 伍雪颖
模板引擎:GRMustache
pod 'GRMustache' , '~> 7.3.0'
html模板:
template.html
{{ name }}
{{ content }}
调用:
- ( void )viewDidLoad {
[ super viewDidLoad ];
NSString *path = [[ NSBundle mainBundle ] bundlePath ];
NSURL *baseUrl = [ NSURL fileURLWithPath :path];
NSString *htmlString = [ self demoFormatWithName : @"wuxueying" value : @"hello" ];
[ self . webView loadHTMLString :htmlString baseURL :baseUrl];
}
- ( NSString *)demoFormatWithName:( NSString *)name value:( NSString *)value {
NSString *fileName = @"template.html" ;
NSString *path = [[[ NSBundle mainBundle ] bundlePath ] stringByAppendingPathComponent :fileName];
NSString *template = [ NSString stringWithContentsOfFile :path encoding : NSUTF8StringEncoding error : nil ];
NSDictionary *renderObject = @{ @"name" :name, @"content" :value } ;
NSString *content = [ GRMustacheTemplate renderObject :renderObject fromString :template error : nil ];
return content;
}
效果: