code: def template(page=None, layout=None, **kwargs):
      pkey = "_template_extends__"
      def decorator(f):
          if inspect.isclass(f):
              layout_ = layout or page
              extends = kwargs.pop("extends", None)
              if extends and hasattr(extends, pkey):
                  items = getattr(extends, pkey).items()
                  if "layout" in items:
                      layout_ = items.pop("layout")
                  for k, v in items:
                      kwargs.setdefault(k, v)
              if not layout_:
                  layout_ = "layout.html"
              kwargs.setdefault("brand_name", "")
              kwargs["layout"] = layout_
              setattr(f, pkey, kwargs)
              setattr(f, "base_layout", kwargs.get("layout"))
              f.g(TEMPLATE_CONTEXT=kwargs)
              return f
          else:
              @functools.wraps(f)
              def wrap(*args2, **kwargs2):
                  response = f(*args2, **kwargs2)
                  if isinstance(response, dict) or response is None:
                      response = response or {}
                      if page:
                          response.setdefault("template_", page)
                      if layout:
                          response.setdefault("layout_", layout)
                      for k, v in kwargs.items():
                          response.setdefault(k, v)
                  return response
              return wrap
      return decorator