MGTemplateEngine 模版发动机简单使用
生活随笔
收集整理的這篇文章主要介紹了
MGTemplateEngine 模版发动机简单使用
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
https://github.com/nxtbgthng/MGTemplateEngine
MGTemplateEngine 模版引擎
??
?MGTemplateEngine比較象 PHP 中的 Smarty 模版引擎,是一個輕量級的引擎,簡單好用。只要設置很多不同的HMTL模版,就能輕松的實現(xiàn)一個View多種內容格式的顯示,對于不熟悉HTML或者減輕工作量而言,把這些工作讓設計分擔一下還是很好的,也比較容易實現(xiàn)設計想要的效果。
?
?
?首先,看看模版的代碼
?
?<!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代碼 - 下面的創(chuàng)建代碼MGTemplateEngine都是從官方的例子中參考下來的,已經有很詳細的說明
?
?// Set up template engine with your chosen matcher.
?MGTemplateEngine *engine = [MGTemplateEngine templateEngine];
?//[engine setDelegate:self];
?[engine setMatcher:[ICUTemplateMatcher matcherWithTemplateEngine:engine]];
?
?// 這里就是設置,或者里邊塞變量的地方。其實也可以設置一個數(shù)組,這樣模板的靈活也會更強。這里我就不演示了官方有例子
?[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的高度,這很好理解,很簡單的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來的更方便直接,通過切換模版和簡單的參數(shù)設置,多個不同類型的欄目也可以使用同一個詳細頁,很大程度上減輕工作理和易于維護。
總結
以上是生活随笔為你收集整理的MGTemplateEngine 模版发动机简单使用的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 现代汽车集团:预计历年累计销量今年上半年
- 下一篇: iOS应用内购买(In App Purc