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

  • 相关阅读:
    Spark Netty与Jetty (源码阅读十一)
    Netty服务端与客户端(源码一)
    NIO源码阅读
    Spark之SQL解析(源码阅读十)
    Spark BlockManager的通信及内存占用分析(源码阅读九)
    Spark Job的提交与task本地化分析(源码阅读八)
    Spark Shuffle数据处理过程与部分调优(源码阅读七)
    Spark常用函数(源码阅读六)
    Spark数据传输及ShuffleClient(源码阅读五)
    SparkConf加载与SparkContext创建(源码阅读四)
  • 原文地址:https://www.cnblogs.com/slgkaifa/p/7220429.html
Copyright © 2011-2022 走看看