Proposal: cacheif template tag

112 vistas
Ir al primer mensaje no leído

Igor Margitich

no leída,
24 sept 2022, 5:27:05 a.m.24/9/2022
para Django developers (Contributions to Django itself)
Hi django-developers,

I would like to propose new template tag `cacheif`. Could be useful when you need to cache part of html and it depends on some condition. Template tag is similar to built-in `cache` tag but accepts extra boolean parameter. See example:

{% cacheif user.is_anonymous 10 homepage %}
  <div class="content">
     .....
  </div>
{% endcacheif %}


Carlton Gibson

no leída,
30 sept 2022, 3:09:53 a.m.30/9/2022
para django-d...@googlegroups.com
Hey Igor, 

I wonder if you can achieve the same varying the timeout parameter based in user.is_authenticated?

Kind Regards,

Carlton

--
You received this message because you are subscribed to the Google Groups "Django developers (Contributions to Django itself)" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-develop...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-developers/bcf8ffee-6497-4a55-ba40-913446d15b06n%40googlegroups.com.

Igor Margitich

no leída,
12 oct 2022, 11:17:57 a.m.12/10/2022
para Django developers (Contributions to Django itself)
Hey Carlton,
I am not sure I got your idea.
The point of proposal is not in user.is_authenticated. user.is_authenticated is just a boolean. Conditional caching for non logged in user is just very simple and common use case when `cacheif` could be used.

пʼятниця, 30 вересня 2022 р. о 09:09:53 UTC+2 carlton...@gmail.com пише:

Carlton Gibson

no leída,
2 nov 2022, 3:39:19 a.m.2/11/2022
para Django developers (Contributions to Django itself)
Hi Igor, 

The sort of thing I had in mind was: 

    {% with cache_timeout=request.user.is_authenticated|yesno:"0,500" %}
      {% cache cache_timeout sidebar %}

        ...

      {% endcache %}
    {% endwith %}

Kind Regards,

Carlton

Joe Tennies

no leída,
7 nov 2022, 11:12:40 a.m.7/11/2022
para django-d...@googlegroups.com
Looking at the code Carlton provided sounds like it meets what was being requested, but it fails "There should be one-- and preferably only one --obvious way to do it." in that it wasn't immediately obvious, but adding the cacheif tag would lead to more than one way. Therefore I'm suggesting that either the new template tag gets added or this gets documented in https://docs.djangoproject.com/en/4.1/topics/cache/#template-fragment-caching . I think the suggestion from Carlton is readable enough that it just should be documented as an example, so it becomes more obvious how to put it together when glancing at this section.

This section also mentions that "The fragment is cached forever if timeout is None." But it's missing the "timeout of 0 won’t cache the value." Note that appears in other references for the timeout value.

- Joe

PS: If I remember, I'll make a PR for those when I get home from work, but I'm more than happy for someone to beat me to it.

Responder a todos
Responder al autor
Reenviar
0 mensajes nuevos