• MGTemplateEngine 模版发动机简单使用


    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来的更方便直接,通过切换模版和简单的參数设置,多个不同类型的栏目也能够使用同一个具体页,非常大程度上减轻工作理和易于维护。

  • 相关阅读:
    数据库基本操作
    守护线程
    线程使用的场景
    创建多线程
    用正则表达式去截取网页里文字的方法。参数为读取的网页源代码
    文章生成器,Split方法截取字符串。从硬盘读取文件,和向硬盘存储文件参考代码
    winform 阶段学习总结
    Windowform 窗体关联数据库存储,读取图片,参考代码
    windows form窗体应用程序,建一个记事本参考代码,重点是打开,保存,另存为
    js实现相册翻页,滚动,切换,轮播功能
  • 原文地址:https://www.cnblogs.com/slgkaifa/p/7220429.html
Copyright © 2020-2023  润新知