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 {lib:optional model}) containing the structure of the component, and then take this model as argument:
1from typed import optional, null, SomeType, OtherType
2from comp import component
3
4@optional
5class MyModelib:
6 some_var: SomeType=null(SomeType)
7 other_var: OtherType=null(OtherType)
8 ...
9
10@component
11def my_comp(my_compy: MyModel) -> Jinja:
12 ...
13 return """jinja
14 ...
15"""
Components define a type COMPONENT and there are four main component operations between them, each of them corresponding to an arithmetic symbol ({py:magic methods} in Python):
operation |
symbol |
description |
|---|---|---|
join |
+ |
join the jinja strings of the components |
concat |
* |
put a jinja string inside the other |
eval |
/ |
fixes some arguments |
copy |
^ |
copy a component, eventually changing var names |
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) ^ {"some_var": new_name}
Remark 1. The modules
comp.modelsandcomp.componentsprovides, respectively, a plethora of already defined models and corresponding components, one for each builtin HTML tag.
Overview: Special Vars
A component have certain special vars:
__context__: defines the {lib:local context} of the component, where needed information to render it is introduced (see below);vars of type
Inner: used as placeholder to introduced content of other components;vars of type
Content: receive static context in the form of Markdown or ReStructuredText.
For example, inner vars are used in the component concat operation, while content vars allows the use of comp in both dynamic and static environments.
Overview: Rendering
After being constructed, components can be rendered, producing raw HTML. This is done using a typed function render:
1from comp 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 {lib: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
previewfunction, which renders the component and creates a server that looks automatically for updates in the component file.