contextual caching of templates with components

本小妞迷上赌 提交于 2019-12-08 06:45:44

问题


I have a page with a menu component. The menu marks the active element with a different CSS class.

Now I want to cache each menu item page. The different pages come all from the same module/action, the difference is just the ID (foo/bar?item=1).

The problem is that the menu is only cached one time, but I need a cache version for every menu item.

I just tried the cache option "contextual: true", but I think this does not work because the main template (barSuccess) is always the same.

Do you guys have any idea, how to solve this problem?


回答1:


You can force cache key by passing the sf_cache_key to the component:

include_component('menu', 'main', array('sf_cache_key' => $sf_params->get('item')));

This way component will be cached for every value of 'item'.

Another way is to use different sets of parameters:

include_component('menu', 'main', array('item' => $sf_params->get('item')));

This way component will be cached for every value of item as well.

In the first solution you force the cache key. It's useful when you need custom logic to decide if the cache should be generated or not.

The second solution relies on fact that component is cached for every combination of parameter values passed to it (there can be more than one of course) .



来源:https://stackoverflow.com/questions/4692203/contextual-caching-of-templates-with-components

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!