开发者

How to manage a lot of own template tags (which are made for widgets)?

开发者 https://www.devze.com 2023-02-10 13:06 出处:网络
Some parts of Django template are kinda widgets1, which are implemented using template tags and there could be a lot of some of them at the page (depending on session cookies for example)

Some parts of Django template are kinda widgets1, which are implemented using template tags and there could be a lot of some of them at the page (depending on session cookies for example)

Approach like this will do if their number is limited:

{% extends "some.html" %}
{% load mywidgets %}

{% block widgets %}
{% if widget1 %} {% widget1 %} {% endif %}
{% if widget2 %} {% widget2 %} {% endif %}
...

{% if widgetn %} {% widgetn %} {% endif %}
{% endblock widgets %}
开发者_如何学运维

But what if it's not? How to handle a huge amount of widgets?


1 You can see what I mean at iGoogle ;)


I think that these widgets should be some kind of objects, not template tags. Objects that can render them self. Then you can simply store these objects in list and render them in a cycle.

In view you would initialize those objects by passing request or other required arguments. Widget object would implement __unicode__ method which will render it. Then in template you would render it: {{ widget }}.

widgets.append( Widget(request) )

--

{% for widget in widgets %}
    {{ widget }}
{% endfor %}

Your widgets could even be django models. Then you could store settings and relate them to users.

You might want to check how fein-cms, django-cms implement content placeholders. Any kind of content objects can be associated to placeholder. Content object can render it self, and placeholder just renders collection of them.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号