About
Here you will find an overview for the comp library.
Overview: Components
The lib comp deals with components. These are typed functions (in the sense of typed) which return jinja strings, i.e, Python strings of type Jinja
, which contain jinja2 syntax. Components should also be defined using the @component
decorator.
The typical way to define a component is to first define a model (in the sense of typed, typically a optional model) containing the structure of the component, and then take this model as argument:
1from typed import optional, null, SomeType, OtherType
2from app import component
3
4@optional
5class MyModel:
6 some_var: SomeType=null(SomeType)
7 other_var: OtherType=null(OtherType)
8 ...
9
10@component
11def my_comp(my_comp: MyModel) -> Jinja:
12 ...
13 return """jinja
14 ...
15"""
Components define a type COMPONENT
and there are four three main operations between them (the ), each of them corresponding to an arithmetic symbol:
operation |
symbol |
description |
---|---|---|
join |
+ |
join the jinja strings of the components |
concat |
* |
put a jinja string inside the other |
eval |
/ |
fixes some arguments |
This means that we can construct complex components through component equations:
1my_comp = (
2 (comp_1 * comp_2) +
3 (comp_3 / {'some_var': some_value})
4) / {"other_var": other_value}
Remark 1. The modules
app.models
andapp.components
provides, respectively, a plethora of already defined models and corresponding components, one for each builtin HTML tag.
Overview: Rendering
After being constructed, components can be rendered, producing raw HTML. This is done using a typed function render
:
1from app import render
2
3html = render(my_comp, **context)
To render a component one needs to pass a context, which is a dictionary. It attaches values to each argument of the component, as well as to other “free variables”.
The rendering process can be customized to produce optimized raw HTML by passing certain special variables to render
:
argument |
type |
description |
---|---|---|
__minified__ |
Bool |
return minified HTML |
__styled__ |
Bool |
auto-generate <style> for inline classes |
__scripts__ |
List(Script) |
include given script data in the HTML |
__assets__ |
List(Asset) |
include given asset data in the HTML |
Remark 2. During the construction of a component you can preview it using the
preview
function, which renders the component and creates a server that looks automatically for updates in the component file.
Overview: Statics
… TBA …