Implementing run-time versioning for Elements

Implementing run-time versioning for Elements

One of the benefits of MODX Revolution, is the simplistic methods required to establish run-time configurations.

Posted on March 24, 2012 at 13:11:00
Tags: Caching
Words: 575
Level: Advanced
Dependencies: None

Using System Settings

[[++jquery_load:eq=`0`:then=``:else=`
<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/[[++jquery_version:default=`1.7.1`]]/jquery.min.js"></script>
`]]
[[++jquerytools_load:eq=`0`:then=``:else=`
<script type="text/javascript" src="//cdn.jquerytools.org/[[++jquerytools_version:default=`1.2.6`]]/all/jquery.tools.min.js"></script>
`]]

Explanation

[[++jquery_load:eq=`0`:then=``:else=`DO SOMETHING`]]
[[++jquerytools_load:eq=`0`:then=``:else=`DO SOMETHING`]]
<script type="text/javascript"
 src="//ajax.googleapis.com/ajax/libs/jquery/[[++jquery_version:default=`1.7.1`]]/jquery.min.js"
></script>

Output

<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js">
<script type="text/javascript" src="//cdn.jquerytools.org/1.2.6/all/jquery.tools.min.js">

Comments

As you may have noticed, the jQuery version which was placed in the page was 1.7.2 and not the default 1.7.1 which was the "current" version when the site went live. This is due to existence of a System Setting which is used to override the default value.

Techniques such as this, allows a single Chunk to be used across multiple Templates and/or Resources in order to update various sections of the site, while leaving others set to the default value.

Most applications would simply place this in the header of a Template and allow the inclusion of the respective library to be managed site-wide - wherever the Template is in use.

Other applications may have very little need of the jQuery library, and therefore the inclusion of a Chunk on those pages may prove more beneficial.

Using a simple Chunk

<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/[[+jquery_version:default=`1.7.1`]]/jquery.min.js"></script>
<script type="text/javascript" src="//cdn.jquerytools.org/[[+jquerytools_version:default=`1.2.6`]]/all/jquery.tools.min.js"></script>

Explanation

As this method may be considered more premeditated, the check for it being "desired" in the current page has been removed.

Chunk Call - jQuery Version

[[$jQueryLoaderRuntime?jquery_version=`1.4.2`]]

Chunk Output

<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js">
<script type="text/javascript" src="//cdn.jquerytools.org/1.2.6/all/jquery.tools.min.js">

Chunk Call- both changed

[[$jQueryLoaderRuntime?jquery_version=`1.4.4`&jquerytools_version=`1.2.4`]]

Chunk Output

<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js">
<script type="text/javascript" src="//cdn.jquerytools.org/1.2.4/all/jquery.tools.min.js">

Final Comments

These techniques can be used in a wide array of application, such as layout, targeted css, SEO, images, etc. In many situations similar implementations are the reason the page source changes upon every load. Experiment and see what you can come up with.

MODX:
Website development platform combining security, power, and innovation.