MenuCaching snippet for MODX Revolution

the Idea of a snippet is very simple. You often need to do on the website menu, which remains unchanged on all pages. Only change the CSS class of the active menu item. Why do we need on each page to do queries to the database and generate menu if you only want to change CSS class? But if it's a big menu with drop-down sub-levels, generation may spend too many resources. Snippet menuCaching isolates the server from this extra work.



Description of the snippet

The snippet allows you to generate menus for each page separately only in order to put a CSS class on active menu item.
The menu is generated only once for the entire site, but the active points are marked.

Example usage


the
[[menuCaching?
&snippetToCache=`Wayfinder`
&cacheSuffix=`top_menu`
&contexts=`web`
&startId=`0`
&level=`1`
&hereClass=`active`
&rowTpl=`@CODE: < li class="[[+wf.classnames]] {{active[[+id]]}}"><a href="[[+wf.link]]" > [[+wf.linktext]]</a></li>`
]]


It is recommended to create a set of parameters (for example, "top_menu") to, again, to save the parser from MODX extra work:

the
[[menuCaching@top_menu]]


What's going on here, I think you already guessed. In the cache menu is stored like this:

the
<ul>
<li class="first {{active1}}"><a href="">Item 1</a></li>
<li class=" {{active2}}"><a href="">Item 2</a></li>
<li class="last {{active3}}"><a href="">menu Item 3</a></li>
</ul>


It only remains to replace the tag type {{active1}} on the class of the active element, and the other to remove, which makes the snippet. Set to the snippet comes with a plugin that clears the cache when you click the "Update site" in the admin.

An important parameter of the snippet cacheSuffix. The example creates a cache for the whole site, but you can create a cache for separate parent categories, and within categories, everything will be taken from the cache:

the
&cacheSuffix=`[[*parent]]`


This snippet can be used not only for menu, and for example for different sliders, etc. blocks that do not change on different pages.

Snippet menuCaching in MODX repository: http://modx.com/extras/package/menucaching

Added

The results of testing with a large menu. Calls both snippets uncached, i.e. [[!snippet]] (for testing, in reality, it is necessary to cache).

Wayfinder
The first call to any of the pages where there is a menu: 2.5815 sec.
The transition to any other page with the same menu: 2.4566 sec. (i.e. the vaunted cacheResults doesn't help)
Updated the page: 1.0021 seconds. (only in this case, cacheResults saves)

The same Wayfinder, using menuCaching
The first call to any of the pages where there is a menu: 2.4842 seconds. (in error, of course, there is no difference)
The transition to any other page with the same menu: 0.7008 seconds. (the result is the same, but the time is different)
Updated the page: 0.4187 sec.
Article based on information from habrahabr.ru

Комментарии

Популярные сообщения из этого блога

Integration of PostgreSQL with MS SQL Server for those who want faster and deeper

Custom database queries in MODx Revolution

Parse URL in Zend Framework 2