TemplateRepository

final public class TemplateRepository

A template repository represents a set of sibling templates and partials.

You don’t have to instanciate template repositories, because GRMustache provides implicit ones whenever you load templates with methods like Template(named:error:), for example.

However, you may like to use one for your profit. Template repositories provide:

  • custom template data source
  • custom Configuration
  • a cache of template parsings
  • absolute paths to ease loading of partial templates in a hierarchy of directories and template files
  • Creates a TemplateRepository which loads template through the provided dataSource.

    The dataSource is optional, but repositories without dataSource can not load templates by name, and can only parse template strings that do not contain any {{> partial }} tag.

    let repository = TemplateRepository()
    let template = try! repository.template(string: "Hello {{name}}")
    

    Declaration

    Swift

    public init(dataSource: TemplateRepositoryDataSource? = nil)
  • Creates a TemplateRepository that loads templates from a dictionary.

    let templates = ["template": "Hulk Hogan has a Mustache."]
    let repository = TemplateRepository(templates: templates)
    
    // Renders "Hulk Hogan has a Mustache." twice
    try! repository.template(named: "template").render()
    try! repository.template(string: "{{>template}}").render()
    

    Declaration

    Swift

    convenience public init(templates: [String: String])

    Parameters

    templates

    A dictionary whose keys are template names and values template strings.

  • Creates a TemplateRepository that loads templates from a directory.

    let repository = TemplateRepository(directoryPath: "/path/to/templates")
    
    // Loads /path/to/templates/template.mustache
    let template = try! repository.template(named: "template")
    

    Eventual partial tags in template files refer to sibling template files.

    If the target directory contains a hierarchy of template files and sub-directories, you can navigate through this hierarchy with both relative and absolute paths to partials. For example, given the following hierarchy:

    • /path/to/templates
      • a.mustache
      • partials
        • b.mustache

    The a.mustache template can embed b.mustache with both {{> partials/b }} and {{> /partials/b }} partial tags.

    The b.mustache template can embed a.mustache with both {{> ../a }} and {{> /a }} partial tags.

    Declaration

    Swift

    convenience public init(directoryPath: String, templateExtension: String? = "mustache", encoding: String.Encoding = String.Encoding.utf8)

    Parameters

    directoryPath

    The path to the directory containing template files.

    templateExtension

    The extension of template files. Default extension is “mustache”.

    encoding

    The encoding of template files. Default encoding is UTF-8.

  • Creates a TemplateRepository that loads templates from a URL.

    let templatesURL = NSURL.fileURLWithPath("/path/to/templates")!
    let repository = TemplateRepository(baseURL: templatesURL)
    
    // Loads /path/to/templates/template.mustache
    let template = try! repository.template(named: "template")
    

    Eventual partial tags in template files refer to sibling template files.

    If the target directory contains a hierarchy of template files and sub-directories, you can navigate through this hierarchy with both relative and absolute paths to partials. For example, given the following hierarchy:

    • /path/to/templates
      • a.mustache
      • partials
        • b.mustache

    The a.mustache template can embed b.mustache with both {{> partials/b }} and {{> /partials/b }} partial tags.

    The b.mustache template can embed a.mustache with both {{> ../a }} and {{> /a }} partial tags.

    Declaration

    Swift

    convenience public init(baseURL: URL, templateExtension: String? = "mustache", encoding: String.Encoding = String.Encoding.utf8)

    Parameters

    baseURL

    The base URL where to look for templates.

    templateExtension

    The extension of template resources. Default extension is “mustache”.

    encoding

    The encoding of template resources. Default encoding is UTF-8.

  • Creates a TemplateRepository that loads templates stored as resources in a bundle.

    let repository = TemplateRepository(bundle: nil)
    
    // Loads the template.mustache resource of the main bundle:
    let template = try! repository.template(named: "template")
    

    Declaration

    Swift

    convenience public init(bundle: Bundle?, templateExtension: String? = "mustache", encoding: String.Encoding = String.Encoding.utf8)

    Parameters

    bundle

    The bundle that stores templates resources. Nil stands for the main bundle.

    templateExtension

    The extension of template resources. Default extension is “mustache”.

    encoding

    The encoding of template resources. Default encoding is UTF-8.

  • The configuration for all templates and partials built by the repository.

    It is initialized with Mustache.DefaultConfiguration.

    You can alter the repository’s configuration, or set it to another value, before you load templates:

    // Reset the configuration to a factory configuration and change tag delimiters:
    let repository = TemplateRepository()
    repository.configuration = Configuration()
    repository.configuration.tagDelimiterPair = ("<%", "%>")
    
    // Renders "Hello Luigi"
    let template = try! repository.template(string: "Hello <%name%>")
    try! template.render(["name": "Luigi"])
    

    Changing the configuration has no effect after the repository has loaded one template.

    Declaration

    Swift

    public var configuration: Configuration
  • The template repository data source, responsible for loading template strings.

    Declaration

    Swift

    public let dataSource: TemplateRepositoryDataSource?
  • Creates a template from a template string.

    Depending on the way the repository has been created, partial tags such as {{>partial}} load partial templates from URLs, file paths, keys in a dictionary, or whatever is relevant to the repository’s data source.

    Throws

    MustacheError

    Declaration

    Swift

    public func template(string: String) throws -> Template

    Parameters

    templateString

    A Mustache template string.

    Return Value

    A Mustache Template.

  • Creates a template, identified by its name.

    Depending on the repository’s data source, the name identifies a bundle resource, a URL, a file path, a key in a dictionary, etc.

    Template repositories cache the parsing of their templates. However this method always return new Template instances, which you can further configure independently.

    Throws

    MustacheError

    Seealso

    reloadTemplates()

    Declaration

    Swift

    public func template(named name: String) throws -> Template

    Parameters

    name

    The template name.

    Return Value

    A Mustache Template.

  • Clears the cache of parsed template strings.

    // May reuse a cached parsing:
    let template = try! repository.template(named:"profile")
    
    // Forces the reloading of the template:
    repository.reloadTemplates();
    let template = try! repository.template(named:"profile")
    

    Note that previously created Template instances are not reloaded.

    Declaration

    Swift

    public func reloadTemplates()