Skip to content

Commit

Permalink
Add view decorator
Browse files Browse the repository at this point in the history
  • Loading branch information
valq7711 committed Mar 30, 2023
1 parent 3db84bc commit f2e65f5
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions upytl/core.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import io
import re
import functools
from enum import Enum
from types import SimpleNamespace, CodeType

Expand Down Expand Up @@ -708,6 +709,27 @@ def render(self, template: Dict[Tag, dict], ctx, *, indent=2, debug=False, docty
out.print(it)
return out.buf.getvalue()

def view(self, template, **defaults):

from collections.abc import MutableMapping

def decorator(func):

@functools.wraps(func)
def wrapper(*args, **kwargs):
result = func(*args, **kwargs)
if isinstance(result, (dict, MutableMapping)):
tplvars = defaults.copy()
tplvars.update(result)
return self.render(template, tplvars)
elif result is None:
return self.render(template, defaults)
return result

return wrapper

return decorator


class HTMLPrinter:

Expand Down

0 comments on commit f2e65f5

Please sign in to comment.