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    The StandardLibrary exposes built-in goodies.
0026    */
0027    public struct StandardLibrary
Logger.swift:23
extension StandardLibrary {
{ 0028 0029 /** 0030 As a filter, `HTMLEscape` returns its argument, HTML-escaped. 0031 0032 <pre> 0033 {{ HTMLEscape(content) }} 0034 </pre> 0035 0036 When used in a section, `HTMLEscape` escapes all inner variable tags in a section: 0037 0038 {{# HTMLEscape }} 0039 {{ firstName }} 0040 {{ lastName }} 0041 {{/ HTMLEscape }} 0042 0043 Variable tags buried inside inner sections are escaped as well, so that you 0044 can render loop and conditional sections: 0045 0046 {{# HTMLEscape }} 0047 {{# items }} 0048 {{ name }} 0049 {{/ items }} 0050 {{/ HTMLEscape }} 0051 0052 ### Usage 0053 0054 let template = ... 0055 template.registerInBaseContext("HTMLEscape", Box(StandardLibrary.HTMLEscape)) 0056 */ 0057 public static let HTMLEscape: MustacheBoxable = HTMLEscapeHelper() 0058 0059 /** 0060 As a filter, `URLEscape` returns its argument, percent-escaped. 0061 0062 <a href="http://google.com?q={{ URLEscape(query) }}">...</a> 0063 0064 When used in a section, `URLEscape` escapes all inner variable tags in a 0065 section: 0066 0067 {{# URLEscape }} 0068 <a href="http://google.com?q={{query}}&amp;hl={{language}}">...</a> 0069 {{/ URLEscape }} 0070 0071 Variable tags buried inside inner sections are escaped as well, so that you 0072 can render loop and conditional sections: 0073 0074 {{# URLEscape }} 0075 <a href="http://google.com?q={{query}}{{#language}}&amp;hl={{language}}{{/language}}">...</a> 0076 {{/ URLEscape }} 0077 0078 ### Usage 0079 0080 let template = ... 0081 template.registerInBaseContext("URLEscape", Box(StandardLibrary.URLEscape)) 0082 */ 0083 // public static let URLEscape: MustacheBoxable = URLEscapeHelper() 0084 0085 /** 0086 As a filter, `javascriptEscape` outputs a Javascript and JSON-savvy string: 0087 0088 <script type="text/javascript"> 0089 var name = "{{ javascriptEscape(name) }}"; 0090 </script> 0091 0092 When used in a section, `javascriptEscape` escapes all inner variable tags 0093 in a section: 0094 0095 <script type="text/javascript"> 0096 {{# javascriptEscape }} 0097 var firstName = "{{ firstName }}"; 0098 var lastName = "{{ lastName }}"; 0099 {{/ javascriptEscape }} 0100 </script> 0101 0102 Variable tags buried inside inner sections are escaped as well, so that you 0103 can render loop and conditional sections: 0104 0105 <script type="text/javascript"> 0106 {{# javascriptEscape }} 0107 var firstName = {{# firstName }}"{{ firstName }}"{{/}}{{^ firstName }}null{{/}}; 0108 var lastName = {{# lastName }}"{{ lastName }}"{{/}}{{^ lastName }}null{{/}}; 0109 {{/ javascriptEscape }} 0110 </script> 0111 0112 ### Usage 0113 0114 let template = ... 0115 template.registerInBaseContext("javascriptEscape", Box(StandardLibrary.javascriptEscape)) 0116 */ 0117 public static let javascriptEscape: MustacheBoxable = JavascriptEscapeHelper() 0118 0119 /** 0120 Iteration is natural to Mustache templates: 0121 `{{# users }}{{ name }}, {{/ users }}` renders "Alice, Bob, etc." when the 0122 `users` key is given a list of users. 0123 0124 The `each` filter gives you some extra keys: 0125 0126 - `@index` contains the 0-based index of the item (0, 1, 2, etc.) 0127 - `@indexPlusOne` contains the 1-based index of the item (1, 2, 3, etc.) 0128 - `@indexIsEven` is true if the 0-based index is even. 0129 - `@first` is true for the first item only. 0130 - `@last` is true for the last item only. 0131 0132 Given the following template: 0133 0134 One line per user: 0135 {{# each(users) }} 0136 - {{ @index }}: {{ name }} 0137 {{/}} 0138 0139 Comma-separated user names: 0140 {{# each(users) }}{{ name }}{{^ @last }}, {{/}}{{/}}. 0141 0142 The rendering reads: 0143 0144 One line per user: 0145 - 0: Alice 0146 - 1: Bob 0147 - 2: Craig 0148 0149 Comma-separated user names: Alice, Bob, Craig. 0150 0151 When provided with a dictionary, `each` iterates each key/value pair of the 0152 dictionary, stores the key in `@key`, and sets the value as the current 0153 context: 0154 0155 {{# each(dictionary) }} 0156 - {{ @key }}: {{.}} 0157 {{/}} 0158 0159 Renders: 0160 0161 - name: Alice 0162 - score: 200 0163 - level: 5 0164 0165 The other positional keys `@index`, `@first`, etc. are still available when 0166 iterating dictionaries. 0167 0168 ### Usage 0169 0170 let template = ... 0171 template.registerInBaseContext("each", Box(StandardLibrary.each)) 0172 */ 0173 public static let each = EachFilter 0174 0175 /** 0176 The zip filter iterates several lists all at once. On each step, one object 0177 from each input list enters the rendering context, and makes its own keys 0178 available for rendering. 0179 0180 Given the Mustache template: 0181 0182 {{# zip(users, teams, scores) }} 0183 - {{ name }} ({{ team }}): {{ score }} points 0184 {{/}} 0185 0186 The following JSON input: 0187 0188 { 0189 "users": [ 0190 { "name": "Alice" }, 0191 { "name": "Bob" }, 0192 ], 0193 "teams": [ 0194 { "team": "iOS" }, 0195 { "team": "Android" }, 0196 ], 0197 "scores": [ 0198 { "score": 100 }, 0199 { "score": 200 }, 0200 ] 0201 } 0202 0203 The rendering is: 0204 0205 - Alice (iOS): 100 points 0206 - Bob (Android): 200 points 0207 0208 In the example above, the first step has consumed (Alice, iOS and 100), and 0209 the second one (Bob, Android and 200). 0210 0211 The zip filter renders a section as many times as there are elements in the 0212 longest of its argument: exhausted lists simply do not add anything to the 0213 rendering context. 0214 0215 ### Usage 0216 0217 let template = ... 0218 template.registerInBaseContext("zip", Box(StandardLibrary.zip)) 0219 */ 0220 public static let zip = ZipFilter 0221 } 0222