Add a Template Loader
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
import Foundation
|
||||
import PathKit
|
||||
|
||||
/// A class representing a template
|
||||
public class Template {
|
||||
@@ -34,6 +35,18 @@ public class Template {
|
||||
}
|
||||
}
|
||||
|
||||
/// Create a template with a file found at the given path
|
||||
public convenience init?(path:Path) {
|
||||
var error:NSError?
|
||||
|
||||
if let string:String = path.read() {
|
||||
self.init(templateString:string)
|
||||
} else {
|
||||
self.init(templateString:"")
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
/// Create a template with a template string
|
||||
public init(templateString:String) {
|
||||
let lexer = Lexer(templateString: templateString)
|
||||
|
||||
45
Stencil/TemplateLoader.swift
Normal file
45
Stencil/TemplateLoader.swift
Normal file
@@ -0,0 +1,45 @@
|
||||
//
|
||||
// TemplateLoader.swift
|
||||
// Stencil
|
||||
//
|
||||
// Created by Kyle Fuller on 28/12/2014.
|
||||
// Copyright (c) 2014 Cocode. All rights reserved.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
import PathKit
|
||||
|
||||
// A class for loading a template from disk
|
||||
public class TemplateLoader {
|
||||
let paths:[Path]
|
||||
|
||||
public init(paths:[Path]) {
|
||||
self.paths = paths
|
||||
}
|
||||
|
||||
public init(bundle:[NSBundle]) {
|
||||
self.paths = bundle.map {
|
||||
return Path($0.bundlePath)
|
||||
}
|
||||
}
|
||||
|
||||
public func loadTemplate(templateName:String) -> Template? {
|
||||
return loadTemplate([templateName])
|
||||
}
|
||||
|
||||
public func loadTemplate(templateNames:[String]) -> Template? {
|
||||
for path in paths {
|
||||
for templateName in templateNames {
|
||||
let templatePath = path + Path(templateName)
|
||||
|
||||
if templatePath.exists() {
|
||||
if let template = Template(path: templatePath) {
|
||||
return template
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user