<p class="sycode"> Use template engine to render HTML interface </p> <p class="sycode"> by Wu Xueying </p> <p class="sycode"> <br> </p> <p class="sycode"> </p> <p class="sycode"> Template engine: GRMustache </p> <p class="sycode"> pod 'GRMustache' , '~> ; 7.3.0' </p> <p class="sycode"> <br> </p> <p class="sycode"> html template: </p> <p class="sycode"> template.html </p> <p class="sycode"> <HTML> </p> <p class="sycode"> < HEAD> </p> <p class="sycode"> </HEAD> </p> <p class="sycode"> <BODY> </p> <p class="sycode"> <H1> {{ name }} </H1> </p> <p class="sycode"> < ;P> {{ content }} </P> </p> <p class="sycode"> </BODY> </p> <p class="sycode"> </HTML> </p> <p class="sycode"> <br> </p> <p class="sycode"> Call: </p> <p class="sycode"> - ( void )viewDidLoad { </p> <p class="sycode"> [ super viewDidLoad ]; <br> NSString *path = [[ NSBundle mainBundle ] bundlePath ]; <br> NSURL *baseUrl = [ NSURL fileURLWithPath :path]; <br> NSString *htmlString = [ self demoFormatWithName : @"wuxueying" value : @"hello" ]; <br> [ self . webView loadHTMLString :htmlString baseURL :baseUrl]; <br> } <br> <br> - ( NSString *)demoFormatWithName:( NSString *)name value:( NSString *)value { <br> NSString *fileName = @"template.html" ; <br> NSString *path = [[[ NSBundle mainBundle ] bundlePath ] stringByAppendingPathComponent :fileName]; <br> NSString *template = [ NSString stringWithContentsOfFile :path encoding : NSUTF8StringEncoding error : nil ]; <br> NSDictionary *renderObject = @{ @"name" :name, @"content" :value } ; <br> NSString *content = [ GRMustacheTemplate renderObject :renderObject fromString :template error : nil ]; <br> return content; </p> <p class="sycode"> } </p> <p class="sycode"> <br> </p> <p class="sycode"> Effect: </p> <p class="sycode"> <br> </p> <br>