简体   繁体   中英

Python: How can I implement this in a DRY way using Jinja2?

I have lots of code like this in my template:

  <h2>Owners of old cars</h2>
    <table id="hor-minimalist-b" summary="Old Cars">
    <thead>
        <tr>
            <th scope="col">Name</th>
            <th scope="col">Age</th>
            <th scope="col">Address</th>
        </tr>
    </thead>
    <tbody>
    {% for result in old_cars %}
        <tr>
            <td>{{result.name}}</td>
            <td>{{result.age}}</td>
            <td>{{result.address}}</td>
        </tr>
    {% endfor %}
    </tbody>
    </table>      

    <h2>Owners of Ford cars</h2>
    <table id="hor-minimalist-b" summary="Old Cars">
    <thead>
        <tr>
            <th scope="col">Name</th>
            <th scope="col">Age</th>
            <th scope="col">Address</th>
        </tr>
    </thead>
    <tbody>
    {% for result in ford_cars %}
        <tr>
            <td>{{result.name}}</td>
            <td>{{result.age}}</td>
            <td>{{result.address}}</td>
        </tr>
    {% endfor %}
    </tbody>
    </table>

There will be lots more tables created like those above. As you can see there is lots of duplicate code. Is there anyway to do this so I don't repeat as much code each time a create a table? Thanks.

UPDATE

I'm thinking about creating a new object, called table, and adding a result and the corresponding h2 title to it. I can then create a list of these table objects, called tables, which I can pass to the template. The template can then iterate over them.

Surely what you want, ideally, is:

{% for car in cars %}
<h2>Owners of {{ car.manufacturer }}</h2>
<table id="hor-minimalist-b" summary="Old Cars">
<thead>
    <tr>
        <th scope="col">Name</th>
        <th scope="col">Age</th>
        <th scope="col">Address</th>
    </tr>
</thead>
<tbody>
    {% for model in car.models %}
    <tr>
        <td>{{model.name}}</td>
        <td>{{model.age}}</td>
        <td>{{model.address}}</td>
    </tr>
    {% endfor %}
</tbody>
</table>
{% endfor %}

I'm not sure what ORM you're using, but in Django that wouldn't be too hard to construct; after all, all you pass in is a list of objects with subobjects and fields.

Again, I can only talk for a django perspective (construct from your own ORM), but you can either deliberately construct the dictionary with explicit requests for each type of manufacturer, or rather have a model for "sales" and a model for "manufacturer" and construct a many-to-many relationship between the two. Your query roughly looks like this, in django python:

result = []
manufacturers = Manufacturer.objects.all() # get all manuf
for m in manufacturer:
    mf = dict()
    mf["manufacturer"] = m.name
    mf["models"] = m.model_set.all() # get all linked models
    result.append(mf)
# pass result to template as cars

Does that make sense?

Use template for the header and footer of the table

<table id="hor-minimalist-b" summary="Old Cars">
<thead>
    <tr>
        <th scope="col">Name</th>
        <th scope="col">Age</th>
        <th scope="col">Address</th>
    </tr>
</thead>
<tbody>

Put this in a file named "table_header.html" and include it in your template. The same for the footer!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM