0001 // The MIT License 0002 // 0003 // Copyright (c) 2015 Gwendal Roué 0004 // 0005 // Permission is hereby granted, free of charge, to any person obtaining a copy 0006 // of this software and associated documentation files (the "Software"), to deal 0007 // in the Software without restriction, including without limitation the rights 0008 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 0009 // copies of the Software, and to permit persons to whom the Software is 0010 // furnished to do so, subject to the following conditions: 0011 // 0012 // The above copyright notice and this permission notice shall be included in 0013 // all copies or substantial portions of the Software. 0014 // 0015 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 0016 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 0017 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 0018 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 0019 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 0020 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 0021 // THE SOFTWARE. 0022 0023 0024 /** 0025 Configuration exposes properties that affect both the parsing and the rendering 0026 of Mustache templates. 0027 0028 0029 ### What can be configured 0030 0031 Configuration covers: 0032 0033 - **Content type**: HTML templates escape rendered strings, while Text templates 0034 do not. Text templates are HTML-escaped as a whole when included in HTML 0035 templates. 0036 0037 - **Context stack**: values stored in a Configuration's context are readily 0038 available to templates. 0039 0040 - **Tag delimiters**: default Mustache delimiters are `{{` and `}}`. These are 0041 configurable. 0042 0043 0044 ### Usage 0045 0046 You setup a configuration *before* loading templates: 0047 0048 // Template loaded later will not HTML-escape the rendered strings: 0049 Mustache.DefaultConfiguration.contentType = .Text 0050 0051 // A text template 0052 let template = try! Template(string: "...") 0053 0054 0055 ### Configuration levels 0056 0057 There are three levels of configuration: 0058 0059 `Mustache.DefaultConfiguration` is a global variable that applies by default: 0060 0061 Mustache.DefaultConfiguration.contentType = .Text 0062 0063 // A text template 0064 let template = try! Template(named: "Document") 0065 0066 `TemplateRepository.configuration` only applies to templates loaded from the 0067 template repository: 0068 0069 let repository = TemplateRepository(directoryPath: "/path/to/templates") 0070 repository.configuration.contentType = .Text 0071 0072 // A text template 0073 let template = try! repository.template(named: "Document") 0074 0075 Templates can also be configured individually. See the documentation of each 0076 Configuration method for more details. 0077 */ 0078 public struct Configuration{ 0079 0080 // ========================================================================= 0081 // MARK: - Factory Configuration 0082 0083 /** 0084 Returns a factory configuration. 0085 0086 Its contentType is HTML, baseContext empty, tag delimiters `{{` and `}}`. 0087 0088 For example: 0089 0090 // Make sure the template repository uses factory configuration, 0091 // regardless of changes made to `Mustache.DefaultConfiguration`: 0092 0093 let repository = TemplateRepository(directoryPath: "/path/to/templates") 0094 repository.configuration = Configuration() 0095 0096 0097 */ 0098 public init
Configuration.swift:334 public var DefaultConfiguration = Configuration()ExpressionGenerator.swift:33 let configuration: ConfigurationExpressionGenerator.swift:35 init(configuration: Configuration? = nil) {TemplateGenerator.swift:41 let configuration: ConfigurationTemplateGenerator.swift:43 init(configuration: Configuration? = nil) {TemplateRepository.swift:162 public var configuration: ConfigurationTemplateRepository.swift:294 private var _lockedConfiguration: Configuration?TemplateRepository.swift:295 private var lockedConfiguration: Configuration {() { 0099 contentType = .HTML 0100 baseContext = Context() 0101 tagDelimiterPair = ("{{", "}}") 0102 } 0103 0104 0105 // ========================================================================= 0106 // MARK: - Content Type 0107 0108 /** 0109 The content type of strings rendered by templates built with this 0110 configuration. 0111 0112 It affects the HTML-escaping of your data: 0113 0114 - The `.HTML` content type has templates render HTML. This is the default 0115 behavior. HTML template escape the input of variable tags such as 0116 `{{name}}`. Use triple mustache tags `{{{content}}}` in order to avoid the 0117 HTML-escaping. 0118 0119 - The `.Text` content type has templates render text. They do not 0120 HTML-escape their input: `{{name}}` and `{{{name}}}` have identical, 0121 non-escaped, renderings. 0122 0123 GRMustache safely keeps track of the content type of templates: should a 0124 HTML template embed a text template, the content of the text template would 0125 be HTML-escaped, as a whole. 0126 0127 Setting the contentType of a configuration affects the contentType of all 0128 templates loaded afterwards: 0129 0130 // Globally, with Mustache.DefaultConfiguration: 0131 0132 Mustache.DefaultConfiguration.contentType = .Text 0133 let textTemplate = try! Template(named: "Script") 0134 0135 // Locally, using a TemplateRepository: 0136 0137 let repository = TemplateRepository(bundle: NSBundle.mainBundle()) 0138 repository.configuration.contentType = .HTML 0139 let HTMLTemplate = try! repository.template(named: "HTMLDocument") 0140 0141 In order to set the content type of an individual templates, use pragma tags 0142 right in the content of your templates: 0143 0144 - `{{% CONTENT_TYPE:TEXT }}` turns a template into a text template. 0145 - `{{% CONTENT_TYPE:HTML }}` turns a template into a HTML template. 0146 0147 For example: 0148 0149 {{! This template renders a bash script. }} 0150 {{% CONTENT_TYPE:TEXT }} 0151 export LANG={{ENV.LANG}} 0152 ... 0153 0154 These pragmas must be found early in the template (before any value tag). 0155 Should several pragmas be found in a template content, the last one wins. 0156 */ 0157 public var contentType
Configuration.swift:334 public var DefaultConfiguration = Configuration(): ContentType 0158 0159 0160 // ========================================================================= 0161 // MARK: - Context Stack 0162 0163 /** 0164 The base context for templates rendering. All templates built with this 0165 configuration can access values stored in the base context. 0166 0167 The default base context is empty. 0168 0169 You can set it to some custom context, or extend it with the 0170 `extendBaseContext` and `registerInBaseContext` methods. 0171 0172 // Globally, with Mustache.DefaultConfiguration: 0173 0174 Mustache.DefaultConfiguration.baseContext = Context(Box(["foo": "bar"])) 0175 0176 // "bar" 0177 let template1 = try! Template(string: "{{foo}}") 0178 try! template1.render() 0179 0180 // Locally, using a TemplateRepository: 0181 0182 let repository = TemplateRepository(bundle: NSBundle.mainBundle()) 0183 repository.configuration.baseContext = Context(Box(["foo": "bar"])) 0184 0185 // "bar" 0186 let template2 = try! repository.template(string: "{{foo}}") 0187 try! template2.render() 0188 0189 The base context can also be set for individual templates: 0190 0191 let template3 = try! Template(string: "{{foo}}") 0192 template3.baseContext = Context(Box(["foo": "bar"])) 0193 0194 // "bar" 0195 try! template3.render() 0196 0197 See also: 0198 0199 - extendBaseContext 0200 - registerInBaseContext 0201 */ 0202 public var baseContext
Configuration.swift:99 contentType = .HTMLCoreFunctions.swift:628 templateRepository.configuration.contentType = .TextTemplateRepository.swift:277 contentType: lockedConfiguration.contentType,: Context 0203 0204 /** 0205 Extends the base context with the provided boxed value. All templates built 0206 with this configuration can access its keys. 0207 0208 // Globally, with Mustache.DefaultConfiguration: 0209 0210 Mustache.DefaultConfiguration.extendBaseContext(Box(["foo": "bar"])) 0211 0212 // "bar" 0213 let template1 = try! Template(string: "{{foo}}") 0214 try! template1.render() 0215 0216 // Locally, using a TemplateRepository: 0217 0218 let repository = TemplateRepository(bundle: NSBundle.mainBundle()) 0219 repository.configuration.extendBaseContext(Box(["foo": "bar"])) 0220 0221 // "bar" 0222 let template2 = try! repository.template(string: "{{foo}}") 0223 try! template2.render() 0224 0225 The base context can also be extended for individual templates: 0226 0227 let template3 = try! Template(string: "{{foo}}") 0228 template3.extendBaseContext(Box(["foo": "bar"])) 0229 0230 // "bar" 0231 try! template3.render() 0232 0233 - parameter box: The box pushed on the top of the context stack. 0234 0235 See also: 0236 0237 - baseContext 0238 - registerInBaseContext 0239 */ 0240 public mutating func extendBaseContext(box: MustacheBox) { 0241 baseContext = baseContext.extendedContext(box) 0242 } 0243 0244 /** 0245 Registers a key in the base context. All renderings will be able to access 0246 the provided box through this key. 0247 0248 Registered keys are looked up first when evaluating Mustache tags. 0249 0250 // Globally, with Mustache.DefaultConfiguration: 0251 0252 Mustache.DefaultConfiguration.registerInBaseContext("foo", Box("bar")) 0253 0254 // Renders "bar" 0255 let template1 = try! Template(string: "{{foo}}") 0256 try! template1.render() 0257 0258 // Renders "bar" again, because the registered key "foo" has priority. 0259 try! template1.render(Box(["foo": "qux"])) 0260 0261 // Locally, using a TemplateRepository: 0262 0263 let repository = TemplateRepository(bundle: NSBundle.mainBundle()) 0264 repository.configuration.registerInBaseContext("foo", Box("bar")) 0265 0266 // "bar" 0267 let template2 = try! repository.template(string: "{{foo}}") 0268 try! template2.render() 0269 0270 Keys can also be registered in the base context of individual templates: 0271 0272 let template3 = try! Template(string: "{{foo}}") 0273 template3.registerInBaseContext("foo", Box("bar")) 0274 0275 // "bar" 0276 try! template3.render() 0277 0278 0279 - parameter key: An identifier. 0280 - parameter box: The box registered for *key*. 0281 0282 See also: 0283 0284 - baseContext 0285 - extendBaseContext 0286 */ 0287 public mutating func registerInBaseContext(key: String, _ box: MustacheBox) { 0288 baseContext = baseContext.contextWithRegisteredKey(key, box: box) 0289 } 0290 0291 0292 // ========================================================================= 0293 // MARK: - Tag delimiters 0294 0295 /** 0296 The delimiters for Mustache tags. All templates built with this 0297 configuration are parsed using those delimiters. 0298 0299 The default value is `("{{", "}}")`. 0300 0301 Setting the tagDelimiterPair of a configuration affects all templates loaded 0302 afterwards: 0303 0304 // Globally, with Mustache.DefaultConfiguration: 0305 0306 Mustache.DefaultConfiguration.tagDelimiterPair = ("<%", "%>") 0307 let template1 = try! Template(string: "<% name %>) 0308 0309 // Locally, using a TemplateRepository: 0310 0311 let repository = TemplateRepository() 0312 repository.configuration.tagDelimiterPair = ("[[", "]]") 0313 let HTMLTemplate = try! repository.template(string: "[[ name ]]") 0314 0315 You can also change the delimiters right in your templates using a "Set 0316 Delimiter tag": `{{=[[ ]]=}}` changes delimiters to `[[` and `]]`. 0317 */ 0318 public var tagDelimiterPair
Configuration.swift:100 baseContext = Context()Configuration.swift:241 baseContext = baseContext.extendedContext(box)Configuration.swift:241 baseContext = baseContext.extendedContext(box)Configuration.swift:288 baseContext = baseContext.contextWithRegisteredKey(key, box: box)Configuration.swift:288 baseContext = baseContext.contextWithRegisteredKey(key, box: box)Template.swift:42 self.init(repository: repository, templateAST: templateAST, baseContext: repository.configuration.baseContext)TemplateRepository.swift:190 return Template(repository: self, templateAST: templateAST, baseContext: lockedConfiguration.baseContext)TemplateRepository.swift:214 return Template(repository: self, templateAST: templateAST, baseContext: lockedConfiguration.baseContext): TagDelimiterPair 0319 0320 } 0321 0322 0323 // ============================================================================= 0324 // MARK: - Default Configuration 0325 0326 /** 0327 The default configuration that is used unless specified otherwise by a 0328 `TemplateRepository`. 0329 0330 See also: 0331 0332 - TemplateRepository 0333 */ 0334 public var DefaultConfiguration
Configuration.swift:101 tagDelimiterPair = ("{{", "}}")TemplateGenerator.swift:62 let tagStartDelimiter = configuration.tagDelimiterPair.0TemplateGenerator.swift:63 let tagEndDelimiter = configuration.tagDelimiterPair.1TemplateGenerator.swift:70 let tagStartDelimiter = configuration.tagDelimiterPair.0TemplateGenerator.swift:71 let tagEndDelimiter = configuration.tagDelimiterPair.1TemplateGenerator.swift:78 let tagStartDelimiter = configuration.tagDelimiterPair.0TemplateGenerator.swift:79 let tagEndDelimiter = configuration.tagDelimiterPair.1TemplateGenerator.swift:86 let tagStartDelimiter = configuration.tagDelimiterPair.0TemplateGenerator.swift:87 let tagEndDelimiter = configuration.tagDelimiterPair.1TemplateGenerator.swift:103 let tagStartDelimiter = configuration.tagDelimiterPair.0TemplateGenerator.swift:104 let tagEndDelimiter = configuration.tagDelimiterPair.1CoreFunctions.swift:512 templateRepository.configuration.tagDelimiterPair = info.tag.tagDelimiterPairTemplateRepository.swift:284 tagDelimiterPair: lockedConfiguration.tagDelimiterPair)= Configuration() 0335
ExpressionGenerator.swift:36 self.configuration = configuration ?? DefaultConfigurationTemplateGenerator.swift:44 self.configuration = configuration ?? DefaultConfigurationTemplateRepository.swift:116 configuration = DefaultConfiguration