MGTemplateEngineモデルエンジンの簡単な使用

3511 ワード

MGTemplateEngineはPHPのSmartyモデルエンジンに似ていて、軽量級のエンジンで、簡単で使いやすいです.多くの異なるHMTLテンプレートを設定すれば、1つの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は公式の例から参考にしており、すでに詳細な説明があります
// 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   ,     ,    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];
}

以上はMG TemplateEngineが基本的に使われていますが、将来的には大いに役に立ちます.コンテンツページの表示については、HTMLほど直接的に便利ではありません.テンプレートと簡単なパラメータ設定を切り替えることで、複数の異なるタイプの欄も同じ詳細ページを使用することができ、作業の合理性とメンテナンスが大幅に軽減されます.もっと詳しく知るにはMGTemplateEngine公式サイトに行くことができます
参照先:https://github.com/mattgemmell/MGTemplateEngine