How to use the py4web.core.Template function in py4web

To help you get started, we’ve selected a few py4web examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github web2py / py4web / tests / test_template.py View on Github external
def test_template(self):
        t = Template("index.html", path=PATH)
        output = t.transform(dict(n=3))
        self.assertEqual(output, "0,1,2.\n")
github web2py / py4web / py4web / core.py View on Github external
def transform(self, output):
        if not isinstance(output, dict):
            return output
        context = dict(request=request)
        context.update(yatl.helpers.__dict__)
        context.update(URL=URL)
        context.update(output)
        context["__vars__"] = output
        app_folder = os.path.join(os.environ["PY4WEB_APPS_FOLDER"], request.app_name)
        path = self.path or os.path.join(app_folder, "templates")
        filename = os.path.join(path, self.filename)
        output = yatl.render(
            Template.reader(filename),
            path=path,
            context=context,
            delimiters=self.delimiters,
            reader=Template.reader,
        )
        return output
github web2py / py4web / py4web / utils / auth.py View on Github external
data.update(status="error", message="validation errors", code=401)
            elif "errors" in data and not data["errors"]:
                del data["errors"]
            data["status"] = data.get("status", "success")
            data["code"] = data.get("code", 200)
            return data
        elif path == "logout":
            self.session["user"] = None
            # Somehow call revoke for active plugin
        elif path == "verify_email" and self.db:
            if self.verify_email(get_vars.get("token")):
                redirect(URL("auth", "email_verified"))
            else:
                redirect(URL("auth", "token_expired"))
        env["path"] = path
        return Template("auth.html").transform(env)
github web2py / py4web / py4web / core.py View on Github external
def uses(*fixtures_in):
        """Find all fixtures, including dependencies, topologically sorted"""
        fixtures = []
        reversed_fixtures = []
        stack = list(fixtures_in)
        while stack:
            fixture = stack.pop()
            reversed_fixtures.append(fixture)
            for other in getattr(fixture, "__prerequisites__", []):
                stack.append(other)
        for fixture in reversed(reversed_fixtures):
            if isinstance(fixture, str):
                fixture = Template(fixture)
            if not fixture in fixtures:
                fixtures.append(fixture)

        def decorator(func):
            @functools.wraps(func)
            def wrapper(*args, **kwargs):
                try:
                    [obj.on_request() for obj in fixtures]
                    ret = func(*args, **kwargs)
                    for obj in fixtures:
                        ret = obj.transform(ret)
                    [obj.on_success() for obj in fixtures]
                    return ret
                except HTTP:
                    [obj.on_success() for obj in fixtures]
                    raise
github web2py / py4web / py4web / core.py View on Github external
def reader(filename):
        """Cached file reader, only reads template if it has changed"""

        def raw_read():
            with open(filename, encoding="utf8") as stream:
                return stream.read()

        return Template.cache.get(
            filename, raw_read, expiration=1, monitor=lambda: os.path.getmtime(filename)
        )