WhatsApp chat

Qweb en 20 pasos

Marlon Falcon Hernandez, qweb20pasospython
Back

Qweb es un motor de plantillas de código abierto para Python.

1. Instalación

pip install qweb

2. Programa

from qweb import QWeb

qweb = QWeb()
template = qweb.render('Hello {{ name }}', {'name': 'World'})

print(template)

3. Plantilla

<!DOCTYPE html>
<html>
    <head>
        <title>Qweb</title>
    </head>
    <body>
        <h1>Qweb</h1>
        <p>{{ name }}</p>
    </body>
</html>

4. Render

from qweb import QWeb

qweb = QWeb()
template = qweb.render('Hello {{ name }}', {'name': 'World'})

print(template)

5. Render Template

from qweb import QWeb

qweb = QWeb()
template = qweb.render_template('template.html', {'name': 'World'})

print(template)

5. Herencia

<!DOCTYPE html>
<html>
    <head>
        <title>Qweb</title>
    </head>
    <body>
        <h1>Qweb</h1>
        <p>{{ name }}</p>
    </body>

    <t t-name="template.html">
        <t t-call="base.html">
            <t t-set="name" t-value="'World'"/>
        </t>
    </t>


    <t t-name="base.html">
        <html>
            <head>
                <title>Qweb</title>
            </head>
            <body>
                <h1>Qweb</h1>
                <p>{{ name }}</p>
            </body>
        </html>

    </t>
</html>

5. Tabla

<table class="table table-striped">
    <thead>
        <tr>
            <th>Nombre</th>
            <th>Apellido</th>
            <th>Edad</th>
        </tr>
    </thead>
    <tbody>
        <tr t-foreach="users" t-as="user">
            <td>{{ user.name }}</td>
            <td>{{ user.last_name }}</td>
            <td>{{ user.age }}</td>
        </tr>
    </tbody>
</table>

6. Condicionales IF

<t t-if="user.age > 18">
    <td>{{ user.name }}</td>
    <td>{{ user.last_name }}</td>
    <td>{{ user.age }}</td>
</t>

6. Condicionales IF ELSE

<t t-if="user.age > 18">
    <td>{{ user.name }}</td>
    <td>{{ user.last_name }}</td>
    <td>{{ user.age }}</td>
</t>

<t t-else="">
    <td>{{ user.name }}</td>
    <td>{{ user.last_name }}</td>
    <td>{{ user.age }}</td>

</t>

7. For

<t t-foreach="users" t-as="user">
    <td>{{ user.name }}</td>
    <td>{{ user.last_name }}</td>
    <td>{{ user.age }}</td>
</t>

8. For Range

<t t-foreach="range(10)" t-as="i">
    <td>{{ i }}</td>
</t>

9. Variables

<t t-set="name" t-value="'World'"/>

10. Arreglos

<t t-set="users" t-value="['Marlon', 'Falcon', 'Hernandez']"/>

Diccionarios

<t t-set="users" t-value="{'name': 'Marlon', 'last_name': 'Falcon', 'age': 18}"/>

11. Funciones

<t t-set="users" t-value="{'name': 'Marlon', 'last_name': 'Falcon', 'age': 18}"/>

<t t-set="name" t-value="upper(users.name)"/>

12. Hello World

<t t-name="template.html">
    <t t-call="base.html">
        <t t-set="name" t-value="'World'"/>
    </t>
</t>

<t t-name="base.html">
    <html>
        <head>
            <title>Qweb</title>
        </head>
        <body>
            <h1>Qweb</h1>
            <p>{{ name }}</p>
        </body>
    </html>
</t>

12. JSON

<t t-set="users" t-value="{'name': 'Marlon', 'last_name': 'Falcon', 'age': 18}"/>

<t t-set="json" t-value="json(users)"/>

13. XML

<t t-set="users" t-value="{'name': 'Marlon', 'last_name': 'Falcon', 'age': 18}"/>

<t t-set="xml" t-value="xml(users)"/>

14. URL

<t t-set="url" t-value="url('https://www.google.com')"/>


mfalconsoft@gmail.com / +34 (662) 47 0645RSS