Skip to content

Instantly share code, notes, and snippets.

@ProgramAlgo
Last active January 28, 2016 07:40
Show Gist options
  • Save ProgramAlgo/c93a9fc88c24e9e6a70f to your computer and use it in GitHub Desktop.
Save ProgramAlgo/c93a9fc88c24e9e6a70f to your computer and use it in GitHub Desktop.
Use Mustache.swift for UIWebView with swift
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