About
In this documentation we will discuss the rendering process in the {l:comp} component system.
Rendering
After constructed, components can be rendered: the process of evaluating the component in a context, producing raw HTML. Technically, the render is implemented as a typed function (in the sense of {typed}) render: Component x Context -> HTML.
1from comp import render
2
3html = render(my_comp, <context>)
Context
The Context type is a representation of Dict(Any), meaning that it is a dictionary with key and values. Typically, one pass this dictionary to the render function as keyword arguments:
1from comp import render
2
3html = render(
4 my_comp,
5 some_var=some_value,
6 other_var=other_value,
7 ...
8)
In the rendering process, the context should contains at least the jinja vars in the returning jinja string of the component to be rendered. Some data, however, is automatically included in the context:
local variables
depending components in the
__depends_on__variable.
In practical terms, rendering a component is similarly to using eval operation complied to all variables of a component, followed by a jinja renderization. This means that if some_var is a variable of some component my_comp, then to render my_comp we need to pass some_var with some value:
1from typed import SomeType
2from comp import component, Jinja, render
3
4@component
5def my_comp(some_var: SomeType, ...) -> Jinja:
6 ...
7 return """jinja
8 ....
9"""
10
11html = render(my_comp, some_var=some_value, ...)
We note that, if in the definition of
my_compthe variablesome_varhas a default value, it is used inrenderif the variable is omitted.
Minify
The render function has certain special variables, as we pass to discuss. The first of those variables in __minify__. It is a boolean variable with default value to False. If set to True, the obtained HTML is returned compacted.
So, if you want to produce compacted HTML, just use:
1from comp import render
2
3html = render(my_comp, <context_vars>, __minify__=True)
Scripts
In the sequence, we have the variable __scripts__ of type List(Script). It should be used to compend a component with certain auxiliary scripts. The Script model has a mandatory attribute script_src which defines its origin.
The attribute script_src is of type Union(Extension('js'), Url('http', 'https'), which means that it accepts both:
a
httporhttpsurla path to some
.jsfile.
If in __script__ it is passed a script object with script_src given by a url, then, during the rendering, the component is compended with the builtin script component complied the given script object. On the other hand, if the script object contains a script_src which is the path to a .js file, then the content of the file is added to the component inside a <script> block.
So, for example, suppose you need to incorporate a script located in the url https://my_script_url.com to a component my_comp. All you need to do is:
1from comp import render
2from comp.models import Script
3
4my_script = Script(
5 script_src="https://my_script_url.com",
6 script_defer=True,
7 ...
8)
9
10html = render(my_comp, <context_vars>, __scripts__=[my_script])