MGTemplateEngine 模版引擎
一、 MGTemplateEngine比较象 PHP 中的 Smarty 模版引擎,是一个轻量级的引擎,简单好用。只要设置很多不同的HMTL模版,就能轻松的实现一个View多种内容格式的显示,对于不熟悉HTML或者减轻工作量而言,把这些工作让设计分担一下还是很好的,也比较容易实现设计想要的效果。
像网易、粉笔网都是用的该库。
首先,看看模版的代码
-
<!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都是从官方的例子中参考下来的,已经有很详细的说明
-
-
MGTemplateEngine *engine = [MGTemplateEngine templateEngine];
-
-
[engine setMatcher:[ICUTemplateMatcher matcherWithTemplateEngine:engine]];
-
-
-
[engine setObject:self.detailData[@"title"] forKey:@"title"];
-
[engine setObject:self.detailData[@"content"] forKey:@"content"];
-
-
-
-
NSString *templatePath = [[NSBundle mainBundle] pathForResource:@"detail" ofType:@"html"];
-
-
-
NSString *html = [engine processTemplateInFileAtPath:templatePath withVariables:nil];
-
-
-
-
self.htmlWebView = [[UIWebView alloc] initWithFrame:CGRectMake(8, 5, 304, 320)];
-
self.htmlWebView.delegate = self;
-
self.htmlWebView.userInteractionEnabled = NO;
-
-
-
NSString *baseURL = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"Detail"];
-
[self.htmlWebView loadHTMLString:html baseURL:[NSURL fileURLWithPath:baseURL]];
-
[self.detailView addSubview:self.htmlWebView];
以上的都是 MGTemplateEngine 很基本的使用,将来也会大派用场的。对于内容页的显示,没有比HTML来的更方便直接,通过切换模版和简单的参数设置,多个不同类型的栏目也可以使用同一个详细页,很大程度上减轻工作理和易于维护。
二、在苹果公司不断的更新Xcode很多以前的库都被省略掉,所以以前的第三方导入到Xcode6.1 难免不会出现问题。同理今天我试了下导MGTemplateEngine,结果报了如下错误:

一番苦解后,原来是类中缺少<Foundation/Foundation.h>架包,import后问题解决。
MGTemplateEngine 模版引擎简单使用以及MGTemplateEngine导入xcode6.1报错修改
原文:http://blog.csdn.net/baron_blogs/article/details/42147529