MGTemplateEngine 模版引擎简单使用以及MGTemplateEngine导入xcode6.1报错修改

MGTemplateEngine 模版引擎


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

像网易、粉笔网都是用的该库。


    首先,看看模版的代码

[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都是从官方的例子中参考下来的,已经有很详细的说明

[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(85304320)];  
  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];  

   

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


二、在苹果公司不断的更新Xcode很多以前的库都被省略掉,所以以前的第三方导入到Xcode6.1 难免不会出现问题。同理今天我试了下导MGTemplateEngine,结果报了如下错误:

                                                                            

一番苦解后,原来是类中缺少<Foundation/Foundation.h>架包,import后问题解决。


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值