@@ -445,228 +445,6 @@ Debugging
445445 The behaviour can be configured via :envvar: `PYTHONBREAKPOINT ` or
446446 :func: `sys.breakpointhook `.
447447
448- Rendering cache:
449- ----------------
450-
451- ``t-cache="key_cache" `` tags part of template to be cached at rendering time.
452- Every sub-directives will be call only during the first rendering. It means
453- that the sql queries excecuted during the rendering of those sub-directives
454- are also done only once.
455-
456- ``t-nocache="documentation" `` tags part of template to be render every time.
457- The content can only use the root values.
458-
459- Why and when to use ``t-cache ``?
460- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
461-
462- This directive is used to speed up the rendering, by caching parts of the final
463- document, which may save queries to the database. However, it should be used
464- sparingly, as ``t-cache `` will inevitably complicate templates (and their
465- understanding of ``t-set `` for example).
466-
467- However, in order to actually save database queries, it might be necessary to
468- render the template with values that are evaluated lazily. If those lazy
469- values are used in a cached part, they will not be evaluated if the part is
470- available in cache.
471-
472- The ``t-cache `` directive is useful for template parts using values that depend
473- on a limited amount of data. We recommend to analyze the rendering of the
474- template with the profiler (by activating the "**Add qweb directive context **"
475- option). Passing lazy values to the rendering in controllers allow you to
476- display the directives using these values and triggering the queries.
477-
478- A concern of using such a cache are making it available to different users
479- (different users should render the cached parts the same way). Another
480- potential issue is to invalidate its entries when necessary. For the latter,
481- the **key expression ** should be chosen wisely. Using the ``write_date `` of a
482- recordset can make a cache key out-of-date without having to discard it
483- explicitly from the cache, for instance.
484-
485- One should also pay attention to the fact that the values in ``t-cache `` parts
486- are scoped. This implies that if there are ``t-set `` directives in this part of
487- the template, the rendering of what comes after it could be different than if
488- there was no ``t-cache `` directive.
489-
490- What if there is a ``t-cache `` inside a ``t-cache ``?
491- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
492-
493- The parts are cached. Each containing only the string corresponding to its
494- rendering. Thus, the ``t-cache `` inside will probably be read less often, its
495- cache key will not necessarily be used. If this must be the case, then you may
496- need to add a ``t-nocache `` (on the same node or a parent).
497-
498- What is ``t-nocache `` used for?
499- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
500-
501- If you want to cache part of a template with ``t-cache `` but a small piece must
502- remain dynamic and be evaluated at cache times. However, the part in
503- ``t-nocache `` will not have access to the ``t-set `` value of the template. Only
504- the values provided by the controller are accessible there.
505- For example, the menu is cached because it's the same all the time and takes
506- time to render (using the performance devtools with the qweb context lets you
507- investigate). However, in the menu, we want the ecommerce cart to be always up
508- to date. So there is a ``t-nocache `` to keep this part dynamic.
509-
510- The base of ``t-cache ``
511- ~~~~~~~~~~~~~~~~~~~~~~~
512-
513- The ``t-cache `` directive allows you to store the rendered result of a template.
514- The **key expression ** (eg 42: ``t-cache="42" ``) will be evaluated as a python
515- expression. This will be used to generate the **cache key **. So there can be
516- different **cache values ** (cached render part) for the same template part. If
517- the **key expression ** is a tuple or a list, it will be searched when generating
518- the **cache key **. If one or more recordsets are returned by the **key
519- expression **, then the model, ids and their corresponding write_date will be
520- used to generate the **cache key **. Special case: If the **key expression **
521- returns a Falsy value, then the content will not be cached.
522-
523- Example::
524-
525- <div t-cache="record,bool(condition)">
526- <span t-if="condition" t-field="record.partner_id.name">
527- <span t-else="" t-field="record.partner_id" t-options-widget="contact">
528- </div>
529-
530- In this case, there may be values (string) in the cache corresponding to each
531- record already returned with a true condition, as well as for the false
532- condition. And if a module modifies the record, the write_date being modified,
533- the cached value is discarded.
534-
535- ``t-cache `` and scoped values (``t-set ``, ``t-foreach ``...)
536- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
537-
538- Values in ``t-cache `` are scoped, this involves a change in behavior between
539- having or not having ``t-cache `` on one of the parent nodes. Don't forget to
540- take into account that Odoo uses a lot of templates, ``t-call `` and view
541- inheritance. Adding a ``t-cache `` can therefore modify the rendering of a
542- template that you do not see when editing.
543- (``t-foreach `` it's like a ``t-set `` for each iteration)
544-
545- Example::
546-
547- <div>
548- <t t-set="a" t-value="1"/>
549- <inside>
550- <t t-set="a" t-value="2"/>
551- <t t-out="a"/>
552- </inside>
553- <outside t-out="a"/>
554-
555- <t t-set="b" t-value="1"/>
556- <inside t-cache="True">
557- <t t-set="b" t-value="2"/>
558- <t t-out="b"/>
559- </inside>
560- <outside t-out="b"/>
561- </div>
562-
563- Will render::
564-
565- <div>
566- <inside>2</inside>
567- <outside>2</inside>
568-
569- <inside>2</inside>
570- <outside>1</inside>
571- </div>
572-
573-
574- The base of ``t-nocache ``
575- ~~~~~~~~~~~~~~~~~~~~~~~~~
576-
577- The template part contained in a node with a ``t-nocache `` attribute is not
578- cached. This content is therefore **dynamic ** and is rendered systematically.
579- However the available values are those provided by the controller (when
580- calling the ``_render `` method).
581-
582- Example::
583-
584- <section>
585- <article t-cache="record">
586- <title><t t-out="record.name"/> <i t-nocache="">(views: <t t-out="counter"/>)</i></titlle>
587- <content t-out="record.description"/>
588- </article>
589- </section>
590-
591- Will render (counter = 1)::
592-
593- <section>
594- <article>
595- <title>The record name <i>(views: 1)</i></titlle>
596- <content>Record description</content>
597- </article>
598- </section>
599-
600- Here the ``<i> `` tag that contains the container will always be rendered. While
601- the rest is as a single string in the cache.
602-
603- ``t-nocache `` and scoped root values (``t-set ``, ``t-foreach ``...)
604- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
605-
606- The contents of the ``t-nocache `` tag can be used for documentation and to
607- explain why the directive is added.
608- The values are scoped into ``t-nocache ``, these values are root values only
609- (values provided by the controller and/or when calling the ``_render `` method
610- of ``ir.qweb ``). ``t-set `` can be done in the template part, but will not be
611- available elsewhere.
612-
613- Example::
614-
615- <section>
616- <t t-set="counter" t-value="counter * 10"/>
617- <header t-nocache="">
618- <t t-set="counter" t-value="counter + 5"/>
619- (views: <t t-out="counter"/>)
620- </header>
621- <article t-cache="record">
622- <title><t t-out="record.name"/> <i t-nocache="">(views: <t t-out="counter"/>)</i></titlle>
623- <content t-out="record.description"/>
624- </article>
625- <footer>(views: <t t-out="counter"/>)</footer>
626- </section>
627-
628- Will render (counter = 1)::
629-
630- <section>
631- <header>
632- (views: 6)
633- </header>
634- <article>
635- <title>The record name <i>(views: 1)</i></titlle>
636- <content>Record description</content>
637- </article>
638- <footer>(views: 10)</footer>
639- </section>
640-
641- Here the ``<i> `` tag that contains the container will always be rendered. While
642- the rest is as a single string in the cache. The counter is not updated by the
643- ``t-set `` out of the ``t-nocache ``
644-
645- ``t-nocache-* `` add some primitive values in the cache
646- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
647-
648- In order to be able to use values generated in the template, it is possible to
649- cache them. The directive is used as ``t-nocache-*="expr" `` where ``* `` is the
650- name of the chosen value and ``expr `` the python expression so the result will
651- be cached. The cached value must be primitive type.
652-
653- Example::
654-
655- <section t-cache="records">
656- <article t-foreach="records" t-as="record">
657- <header>
658- <title t-field="record.get_method_title()"/>
659- </header>
660- <footer t-nocache="This part has a dynamic counter and must be rendered all the time."
661- t-nocache-cached_value="record.get_base_counter()">
662- <span t-out="counter + cached_value"/>
663- </footer>
664- </article>
665- </section>
666-
667- The value ``cached_value `` is cached with the cached template part of
668- ``t-cache="records" `` and add to the scoped root values each time.
669-
670448Helpers
671449-------
672450
0 commit comments