当前位置:Gxlcms > html代码 > 使用模板引擎渲染HTML界面_html/css_WEB-ITnose

使用模板引擎渲染HTML界面_html/css_WEB-ITnose

时间:2021-07-01 10:21:17 帮助过:135人阅读

使用模板引擎渲染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;

}


效果:

人气教程排行