Module can work with Mako Templates by default.

More information about Mako Templates: https://www.makotemplates.org/

Let's create simple module, like in New module registration in Trassir and add a template for the main page.
Template must be saved in the script folder /templates/index.html

Template code:

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Trassir App</title>
</head>
<body>
  Hello <b>${user}</b>, today is <i>${today}</i>
</body>
</html>

Now we can create handler, where we will set the name for our template into the  __tempalte__ attribute
Template will be available in the module list only if method get will return self.render().

By the way, self.render() can accept any keyword arguments, which you can use in your template.

For example, for the display in the template, let's send current user name and date/time into the module.

# ExampleApp
"""
<parameters>
    <company>DSSL</company>
    <author>AATrubilin</author>
    <title>ExampleApp</title>
    <version>1.0.0</version>
</parameters>
"""
import datetime

from exthttp import create_app, BaseHandler

app = create_app("ExampleApp")
app.create_module(
    name="My First App", icon_path="manual/en/gtk-dialog-important.png",
)


@app.route("index")
class IndexHandler(BaseHandler):
    __template__ = "index.html"

    def get(self, request, *args, **kwargs):
        return self.render(
            user=request.user.name,
            today=datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"),
        )



  • Нет меток