# HG changeset patch # User Benoit Boissinot # Date 1360363754 -3600 # Node ID ef78450c8df6ae3792f7410a52e962bfcbc2c6bf # Parent 3490c91a1fcb13859000e2d50b1bf5ba8997728e templater: add get() function to access dict element (e.g. extra) diff -r 3490c91a1fcb -r ef78450c8df6 mercurial/help/templates.txt --- a/mercurial/help/templates.txt Fri Feb 08 21:55:46 2013 +0100 +++ b/mercurial/help/templates.txt Fri Feb 08 23:49:14 2013 +0100 @@ -44,19 +44,21 @@ In addition to filters, there are some basic built-in functions: +- date(date[, fmt]) + +- fill(text[, width]) + +- get(dict, key) + - if(expr, then[, else]) - ifeq(expr, expr, then[, else]) -- sub(pat, repl, expr) - - join(list, sep) - label(label, expr) -- date(date[, fmt]) - -- fill(text[, width]) +- sub(pat, repl, expr) Also, for any expression that returns a list, there is a list operator: diff -r 3490c91a1fcb -r ef78450c8df6 mercurial/templater.py --- a/mercurial/templater.py Fri Feb 08 21:55:46 2013 +0100 +++ b/mercurial/templater.py Fri Feb 08 23:49:14 2013 +0100 @@ -207,6 +207,19 @@ f = context._filters[n] return (runfilter, (args[0][0], args[0][1], f)) +def get(context, mapping, args): + if len(args) != 2: + # i18n: "get" is a keyword + raise error.ParseError(_("get() expects two arguments")) + + dictarg = args[0][0](context, mapping, args[0][1]) + if not util.safehasattr(dictarg, 'get'): + # i18n: "get" is a keyword + raise error.ParseError(_("get() expects a dict as first argument")) + + key = args[1][0](context, mapping, args[1][1]) + yield dictarg.get(key) + def join(context, mapping, args): if not (1 <= len(args) <= 2): # i18n: "join" is a keyword @@ -285,11 +298,12 @@ } funcs = { + "get": get, "if": if_, "ifeq": ifeq, "join": join, + "label": label, "sub": sub, - "label": label, } # template engine