Mgtemplateengine template engine simple to use

Source: Internet
Author: User
Tags smarty template

Original: http://blog.csdn.net/crazy_srufboy/article/details/21748995

The effect to be achieved

First, the middle title to the content is displayed using UIWebView, the comment is UITableView can be dragged down to load more comments, can also add comments and delete comments.

Motivation

Commenting on the use of UIWebView in the page will make the display more flexible and simple, of course, you may be able to find the uitextview extension of the Internet is also OK, but I feel that HTML is more in line with my needs. In the years of PHP development we know, in fact, the details of the page is very similar, mainly the content of the display of different styles. But on the iOS platform, do these splicing work is very tired and difficult to maintain, but also to constantly calculate the location of various types of page objects, it is thankless. Using UIWebView will be a good choice.

Mgtemplateengine Template engine

Mgtemplateengine is a lightweight engine that is simple and easy to use, compared to the Smarty template engine in PHP. As long as you set up a lot of different HMTL templates, you can easily implement a view of a variety of content format display, for unfamiliar with HTML or reduce workload, it is very good to share these work to design, but also easier to achieve the desired effect of design.

First, look at the code of the template

[HTML]View PlainCopy 
  1. <! DOCTYPE HTML>
  2. <HTML lang="en">
  3. <head>
  4. <meta charset="Utf-8">
  5. <title></title>
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <link href="./detail.css" rel="stylesheet">
  8. </head>
  9. <body>
  10. <div id=' container ' name="container">
  11. <div class="title">{{title}}</div>
  12. <div class="date">{{date}</div>
  13. <div class="content">{{content}}</div>
  14. </div>
  15. </Body>
  16. </html>

Objective-c code-The following create code Mgtemplateengine is referenced in the official example, has been described in detail

[OBJC]View PlainCopy 
  1. Set up template engine with your chosen matcher.
  2. Mgtemplateengine *engine = [Mgtemplateengine templateengine];
  3. [Engine setdelegate:self];
  4. [Engine setmatcher:[icutemplatematcher Matcherwithtemplateengine:engine];
  5. Here is the setting, or the place where the frontier fortress variable is. In fact, you can also set an array, so that the template will be more flexible. Here, I'm not going to show you an official case.
  6. [Engine setobject:selfdetaildata[@ "title"] Forkey:@ "title"];
  7. [Engine setobject:selfdetaildata[@ "content"] Forkey:@ "Content"];
  8. Mgtemplateengine/detail/detail.html
  9. Mgtemplateengine/detail/detail.css
  10. NSString *templatepath = [[NSBundle Mainbundle] pathforresource:@ "Detail" OfType:@ "html"];
  11. Process the template and display the results.
  12. NSString *html = [engine processtemplateinfileatpath:templatepath withvariables: nil];
  13. Get HTML
  14. Self. Htmlwebview = [[UIWebView alloc] initwithframe:cgrectmake (8, 5, 304, 32  0)];
  15. Self. Htmlwebview. Delegate = self ;
  16. Self. Htmlwebview. userinteractionenabled = NO;
  17. You will be able to load the. css file inside the HTML
  18. NSString *baseurl = [[[NSBundle Mainbundle] ResourcePath] stringbyappendingpathcomponent:@ "Detail"];
  19. [self. Htmlwebview loadhtmlstring:html baseurl:[nsurl Fileurlwithpath:baseurl];
  20. [self. DetailView addsubview:self. Htmlwebview];

Because my uiwebview is plugged into the uitableview, I have to recalculate the height after the UIWebView is loaded. Because I want to make the user feel that this is actually an HTML.

[OBJC]View PlainCopy 
    1. I added UIWebView to the Self.detailview
    2. Self. Listtableview. Tableheaderview = self. DetailView;
[OBJC]View PlainCopy 
  1. #pragma mark-
  2. #pragma Mark-# uiwebviewdelegate
  3. -(void) Webviewdidfinishload: (UIWebView *) WebView {
  4. //Get the entire HMTL height, which is well understood, very simple JS
  5. nsstring *heightstring = [self. Htmlwebview stringbyevaluatingjavascriptfromstring:@ " document.getElementById (\ "Container\"). offsetheight; "];
  6. //Reset View content size
  7. CGRect nframe = self. DetailView. Frame;
  8. Nframe. Size. Height = [heightstring doublevalue] + 35. 0;
  9. self. DetailView. frame = Nframe;
  10. //Reset WebView content size
  11. CGRect nwebviewframe = self. Htmlwebview. Frame;
  12. Nwebviewframe. Size. Height = [heightstring doublevalue] + 15;
  13. self. Htmlwebview. frame = Nwebviewframe;
  14. //Let UIWebView load, only set UITableView, last load comment
  15. [self tableviewsetting];
  16. [self getcommentlist];
  17. }

The above is mgtemplateengine very basic use, in the future will come in handy. For the display of content pages, there is no more convenient than HTML directly, through the switch template and simple parameter settings, a number of different types of columns can also use the same detail page, to a large extent reduce work and easy maintenance.

For more in-depth understanding, you can go to Mgtemplateengine official website

Mgtemplateengine template engine simple to use (GO)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.