##// END OF EJS Templates
templatefilters: allow declaration of input data type...
Yuya Nishihara -
r37239:54355c24 default
parent child Browse files
Show More
@@ -324,7 +324,7 b' class templatefilter(_templateregistrarb'
324 324
325 325 templatefilter = registrar.templatefilter()
326 326
327 @templatefilter('myfilter')
327 @templatefilter('myfilter', intype=bytes)
328 328 def myfilterfunc(text):
329 329 '''Explanation of this template filter ....
330 330 '''
@@ -332,6 +332,9 b' class templatefilter(_templateregistrarb'
332 332
333 333 The first string argument is used also in online help.
334 334
335 Optional argument 'intype' defines the type of the input argument,
336 which should be (bytes, int, or None for any.)
337
335 338 'templatefilter' instance in example above can be used to
336 339 decorate multiple functions.
337 340
@@ -342,6 +345,9 b' class templatefilter(_templateregistrarb'
342 345 Otherwise, explicit 'templatefilters.loadkeyword()' is needed.
343 346 """
344 347
348 def _extrasetup(self, name, func, intype=None):
349 func._intype = intype
350
345 351 class templatefunc(_templateregistrarbase):
346 352 """Decorator to register template function
347 353
@@ -354,12 +354,12 b' def splitlines(text):'
354 354 def stringescape(text):
355 355 return stringutil.escapestr(text)
356 356
357 @templatefilter('stringify')
357 @templatefilter('stringify', intype=bytes)
358 358 def stringify(thing):
359 359 """Any type. Turns the value into text by converting values into
360 360 text and concatenating them.
361 361 """
362 return templateutil.stringify(thing)
362 return thing # coerced by the intype
363 363
364 364 @templatefilter('stripdir')
365 365 def stripdir(text):
@@ -342,6 +342,7 b' def evalstringliteral(context, mapping, '
342 342 return stringify(thing)
343 343
344 344 _unwrapfuncbytype = {
345 None: _unwrapvalue,
345 346 bytes: stringify,
346 347 int: unwrapinteger,
347 348 }
@@ -400,8 +401,9 b' def runtemplate(context, mapping, templa'
400 401
401 402 def runfilter(context, mapping, data):
402 403 arg, filt = data
403 thing = evalfuncarg(context, mapping, arg)
404 thing = evalrawexp(context, mapping, arg)
404 405 try:
406 thing = unwrapastype(thing, getattr(filt, '_intype', None))
405 407 return filt(thing)
406 408 except (ValueError, AttributeError, TypeError):
407 409 sym = findsymbolicname(arg)
General Comments 0
You need to be logged in to leave comments. Login now