您的位置:首页 > 其它

MGTemplateEngine 模版发动机简单使用

2017-07-22 09:55 281 查看

https://github.com/nxtbgthng/MGTemplateEngine




MGTemplateEngine 模版引擎

 
 
 MGTemplateEngine比較象 PHP 中的 Smarty 模版引擎。是一个轻量级的引擎,简单好用。仅仅要设置非常多不同的HMTL模版。就能轻松的实现一个View多种内容格式的显示,对于不熟悉HTML或者减轻工作量而言,把这些工作让设计分担一下还是非常好的,也比較easy实现设计想要的效果。


 
 
 首先,看看模版的代码
 
 <!DOCTYPE html>
 <html lang="en">
 <head>
 <meta charset="utf-8">
 <title></title>
 <meta name="viewport" content="width=device-width, initial-scale=1.0">
 <link href="./detail.css" rel="stylesheet">
 </head>
 <body>
 <div id='container' name="container">
 <div class="title">{{ title }}</div>
 <div class="date">{{ date }}</div>
 <div class="content">{{ content }}</div>
 </div>
 </body>
 </html>
 
 
 Objective-C代码 - 以下的创建代码MGTemplateEngine都是从官方的样例中參考下来的,已经有非常具体的说明
 
 // Set up template engine with your chosen matcher.
 MGTemplateEngine *engine = [MGTemplateEngine templateEngine];
 //[engine setDelegate:self];
 [engine setMatcher:[ICUTemplateMatcher matcherWithTemplateEngine:engine]];
 
 // 这里就是设置。或者里边塞变量的地方。

事实上也能够设置一个数组,这样模板的灵活也会更强。这里我就不演示了官方有样例
 [engine setObject:self.detailData[@"title"] forKey:@"title"];
 [engine setObject:self.detailData[@"content"] forKey:@"content"];
 
 // MGTemplateEngine/Detail/detail.html
 // MGTemplateEngine/Detail/detail.css
 NSString *templatePath = [[NSBundle mainBundle] pathForResource:@"detail" ofType:@"html"];
 
 // Process the template and display the results.
 NSString *html = [engine processTemplateInFileAtPath:templatePath withVariables:nil];
 
 
 // 获得HTML
 self.htmlWebView = [[UIWebView alloc] initWithFrame:CGRectMake(8, 5, 304, 320)];
 self.htmlWebView.delegate = self;
 self.htmlWebView.userInteractionEnabled = NO;
 
 // 你就能载入到HTML里面的.css文件
 NSString *baseURL = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"Detail"];
 [self.htmlWebView loadHTMLString:html baseURL:[NSURL fileURLWithPath:baseURL]];
 [self.detailView addSubview:self.htmlWebView];
 
 
 由于我的UIWebView是在插入到UITableView,所以在UIWebView载入完后,就得又一次计算高度。由于我想让用户感觉不到这事实上是一个HTML。
 
 // 我将UIWebView加入到了self.detailView
 self.listTableView.tableHeaderView = self.detailView;
 
 
 #pragma mark -
 #pragma mark -# UIWebViewDelegate
 
 - (void)webViewDidFinishLoad:(UIWebView *)webView {
 
 // 获取整个HMTL的高度,这非常好理解。非常easy的JS
 NSString *heightString = [self.htmlWebView stringByEvaluatingJavaScriptFromString:@"document.getElementById(\"container\").offsetHeight;"];
 
 // 重设view内容大小
 CGRect nFrame = self.detailView.frame;
 nFrame.size.height = [heightString doubleValue] + 35.0;
 self.detailView.frame = nFrame;
 
 // 重设webview内容大小
 CGRect nWebViewFrame = self.htmlWebView.frame;
 nWebViewFrame.size.height = [heightString doubleValue] + 15;
 self.htmlWebView.frame = nWebViewFrame;
 
 // 让UIWebView载入完后,才设置UITableView,最后才载入评论
 [self tableViewSetting];
 [self getCommentList];
 }
 
 
 以上的都是 MGTemplateEngine 非常主要的使用。将来也会大派用场的。对于内容页的显示,没有比HTML来的更方便直接,通过切换模版和简单的參数设置,多个不同类型的栏目也能够使用同一个具体页,非常大程度上减轻工作理和易于维护。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: