MGTemplateEngine 模版引擎简单使用(转)

原文:http://blog.csdn.net/crazy_srufboy/article/details/21748995css

要实现的效果html

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

  首先上图中间的 标题至内容 都是使用UIWebView显示,评论是UITableView能够往下拖加载更多评论,也能够增长评论同删除评论。web

动机数组

       评细页中使用 UIWebView 会使显示内容更加的灵活和简单,固然你也许能够网上找UITextView的扩展一样也行,但我感受HTML更符合个人需求。在多年的PHP开发中咱们知道,其实详细页都是大同小异,主要是显示的内容风格不一样。但在iOS平台上,作这些拼拼接接的活很累也不易于维护,还要不断计算各类类型页面对象的位置,实在吃力不讨好。使用UIWebView将是一个不错的选择。xcode

MGTemplateEngine 模版引擎
网站

     MGTemplateEngine比较象 PHP 中的 Smarty 模版引擎,是一个轻量级的引擎,简单好用。只要设置不少不一样的HMTL模版,就能轻松的实现一个View多种内容格式的显示,对于不熟悉HTML或者减轻工做量而言,把这些工做让设计分担一下仍是很好的,也比较容易实现设计想要的效果。lua

    首先,看看模版的代码spa

[html]  view plain  copy
 
 在CODE上查看代码片派生到个人代码片
  1. <!DOCTYPE html>  
  2. <html lang="en">  
  3.   <head>  
  4.     <meta charset="utf-8">  
  5.     <title></title>  
  6.     <meta name="viewport" content="width=device-width, initial-scale=1.0">  
  7.     <link href="./detail.css" rel="stylesheet">  
  8.   </head>  
  9.   <body>  
  10.   <div id='container' name="container">  
  11.     <div class="title">{{ title }}</div>  
  12.     <div class="date">{{ date }}</div>  
  13.     <div class="content">{{ content }}</div>  
  14.   </div>  
  15.   </body>  
  16. </html>  

 Objective-C代码 - 下面的建立代码MGTemplateEngine都是从官方的例子中参考下来的,已经有很详细的说明.net

[objc]  view plain  copy
 
 在CODE上查看代码片派生到个人代码片
  1. // Set up template engine with your chosen matcher.  
  2. MGTemplateEngine *engine = [MGTemplateEngine templateEngine];  
  3. //[engine setDelegate:self];  
  4. [engine setMatcher:[ICUTemplateMatcher matcherWithTemplateEngine:engine]];  
  5.   
  6. // 这里就是设置,或者里边塞变量的地方。其实也能够设置一个数组,这样模板的灵活也会更强。这里我就不演示了官方有例子  
  7. [engine setObject:self.detailData[@"title"] forKey:@"title"];  
  8. [engine setObject:self.detailData[@"content"] forKey:@"content"];  
  9.   
  10. // MGTemplateEngine/Detail/detail.html  
  11. // MGTemplateEngine/Detail/detail.css  
  12. NSString *templatePath = [[NSBundle mainBundle] pathForResource:@"detail" ofType:@"html"];  
  13.   
  14. // Process the template and display the results.  
  15. NSString *html = [engine processTemplateInFileAtPath:templatePath withVariables:nil];  
  16.   
  17.   
  18. // 得到HTML  
  19. self.htmlWebView = [[UIWebView alloc] initWithFrame:CGRectMake(8, 5, 304, 320)];  
  20. self.htmlWebView.delegate = self;  
  21. self.htmlWebView.userInteractionEnabled = NO;  
  22.   
  23. // 你就能加载到HTML里面的.css文件  
  24. NSString *baseURL = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"Detail"];  
  25. [self.htmlWebView loadHTMLString:html baseURL:[NSURL fileURLWithPath:baseURL]];  
  26. [self.detailView addSubview:self.htmlWebView];  

    由于个人UIWebView是在插入到UITableView,因此在UIWebView加载完后,就得从新计算高度。由于我想让用户感受不到这实际上是一个HTML。 设计

[objc]  view plain  copy
 
 在CODE上查看代码片派生到个人代码片
  1. // 我将UIWebView添加到了self.detailView  
  2. self.listTableView.tableHeaderView = self.detailView;  
[objc]  view plain  copy
 
 在CODE上查看代码片派生到个人代码片
  1. #pragma mark -  
  2. #pragma mark -# UIWebViewDelegate  
  3.   
  4. - (void)webViewDidFinishLoad:(UIWebView *)webView {  
  5.   
  6.     // 获取整个HMTL的高度,这很好理解,很简单的JS  
  7.     NSString *heightString = [self.htmlWebView stringByEvaluatingJavaScriptFromString:@"document.getElementById(\"container\").offsetHeight;"];  
  8.       
  9.     // 重设view内容大小  
  10.     CGRect nFrame = self.detailView.frame;  
  11.     nFrame.size.height = [heightString doubleValue] + 35.0;  
  12.     self.detailView.frame = nFrame;  
  13.   
  14.     // 重设webview内容大小  
  15.     CGRect nWebViewFrame = self.htmlWebView.frame;  
  16.     nWebViewFrame.size.height = [heightString doubleValue] + 15;  
  17.     self.htmlWebView.frame = nWebViewFrame;  
  18.   
  19.     // 让UIWebView加载完后,才设置UITableView,最后才加载评论  
  20.     [self tableViewSetting];  
  21.     [self getCommentList];  
  22. }  

 以上的都是 MGTemplateEngine 很基本的使用,未来也会大派用场的。对于内容页的显示,没有比HTML来的更方便直接,经过切换模版和简单的参数设置,多个不一样类型的栏目也可使用同一个详细页,很大程度上减轻工做理和易于维护。

    要更深刻的了解,能够去 MGTemplateEngine 官方网站

相关文章
相关标签/搜索