Last active
January 28, 2016 07:40
-
-
Save ProgramAlgo/c93a9fc88c24e9e6a70f to your computer and use it in GitHub Desktop.
Use Mustache.swift for UIWebView with swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
do { | |
// Load the `product_list.mustache` resource of the main bundle | |
let template = try Template(named: "product/product_list") | |
let bundlePath = NSBundle.mainBundle().bundlePath | |
let data = NSData(contentsOfFile: bundlePath + "/product/product_list.json") | |
let json: AnyObject = try NSJSONSerialization.JSONObjectWithData(data!, options:.AllowFragments) | |
// Convert to Dictionary: | |
let jsonData = json as! [String: AnyObject] | |
//print(jsonData) | |
// The rendering | |
let rendering = try template.render(Box(jsonData)) | |
let mainBundleURL: NSURL = NSURL.fileURLWithPath(bundlePath) | |
//print(mainBundleURL) | |
webView.loadHTMLString(rendering, baseURL: mainBundleURL) | |
} catch { | |
print(error) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment