欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  web前端

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

程序员文章站 2022-05-06 13:17:55
...
使用模板引擎渲染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;

}


效果: