##// END OF EJS Templates
templater: add get() function to access dict element (e.g. extra)
Benoit Boissinot -
r18582:ef78450c default
parent child Browse files
Show More
@@ -44,19 +44,21 b' Note that a filter is nothing more than '
44
44
45 In addition to filters, there are some basic built-in functions:
45 In addition to filters, there are some basic built-in functions:
46
46
47 - date(date[, fmt])
48
49 - fill(text[, width])
50
51 - get(dict, key)
52
47 - if(expr, then[, else])
53 - if(expr, then[, else])
48
54
49 - ifeq(expr, expr, then[, else])
55 - ifeq(expr, expr, then[, else])
50
56
51 - sub(pat, repl, expr)
52
53 - join(list, sep)
57 - join(list, sep)
54
58
55 - label(label, expr)
59 - label(label, expr)
56
60
57 - date(date[, fmt])
61 - sub(pat, repl, expr)
58
59 - fill(text[, width])
60
62
61 Also, for any expression that returns a list, there is a list operator:
63 Also, for any expression that returns a list, there is a list operator:
62
64
@@ -207,6 +207,19 b' def buildfunc(exp, context):'
207 f = context._filters[n]
207 f = context._filters[n]
208 return (runfilter, (args[0][0], args[0][1], f))
208 return (runfilter, (args[0][0], args[0][1], f))
209
209
210 def get(context, mapping, args):
211 if len(args) != 2:
212 # i18n: "get" is a keyword
213 raise error.ParseError(_("get() expects two arguments"))
214
215 dictarg = args[0][0](context, mapping, args[0][1])
216 if not util.safehasattr(dictarg, 'get'):
217 # i18n: "get" is a keyword
218 raise error.ParseError(_("get() expects a dict as first argument"))
219
220 key = args[1][0](context, mapping, args[1][1])
221 yield dictarg.get(key)
222
210 def join(context, mapping, args):
223 def join(context, mapping, args):
211 if not (1 <= len(args) <= 2):
224 if not (1 <= len(args) <= 2):
212 # i18n: "join" is a keyword
225 # i18n: "join" is a keyword
@@ -285,11 +298,12 b' methods = {'
285 }
298 }
286
299
287 funcs = {
300 funcs = {
301 "get": get,
288 "if": if_,
302 "if": if_,
289 "ifeq": ifeq,
303 "ifeq": ifeq,
290 "join": join,
304 "join": join,
305 "label": label,
291 "sub": sub,
306 "sub": sub,
292 "label": label,
293 }
307 }
294
308
295 # template engine
309 # template engine
General Comments 0
You need to be logged in to leave comments. Login now