Useful Keyword Modifiers

Last Updated: 05 Jul 2019

Below is a collection of examples of different ways keyword modifiers can be used. If you have a handy keyword modifier you would like to add to the list, be sure to let us know.

Word play

Print ellipsis symbol if a block of text exceeds maximum number of words or characters

//max characters
%asset_metadata_description^maxchars:20^trim%%asset_metadata_description^charcount^gt:20:…:%
 
//max words
%asset_metadata_description^maxwords:5^trim%%asset_metadata_description^wordcount^gt:5:…:%

Max charcount result: The quick brown fox jumped over the lazy dog => The quick brown fox…

Max wordcount result: The quick brown fox jumped over the lazy dog => The quick brown fox jumped…


Print a semicolon separated list from a matrix value (such as checkbox, select field, multiple text metadata fields) into a ordered or un-ordered list, but only if it is not empty

%asset_metadata_keywords^replace:;:</li><li>^tagif:li^tagif:ul%

Original: Value 1; Value 2; Value 3

Result: <ul><li>Value 1</li><li>Value 2</li><li>Value 3</li></ul>


Number play

Calculate 10% GST of a product price and price without GST

//GST
%item_price^number_format:2^divide:11^number_format:2%
//price excluding GST
%item_price^number_format:2^divide:11^multiply:10^number_format:2%

GST result: 110.00 => 10.00

Price excluding GST result: 110.00 => 100.00

Simple padding

Pad to two digits: Adds an extra '0' in front if the number is only one digit.

%asset_metadata_number^charcount^eq:1:0:%%asset_metadata_number%

Regular Expression Replacements

Print the file extension of any file asset

%asset_name^replace:.*\.: %

Result: document.pdf => pdf

Extract any parameter value from a URL

%asset_metadata_youtube^replace:.*v=:^replace:&.*:%

Result: http://www.youtube.com/watch?v=5B-Es5-CjFg&feature=youtube_gdata => 5B-Es5-CjFg

Print the page number outside of an asset listing, conditionally

%globals_server_QUERY_STRING^contains:_result_page:Page : %%globals_server_QUERY_STRING^replace:.*_result_page=:^replace:&.*:%

Result: http://www.squiz.net/asset-listing?result_15168_result_page=2 => Page 2

Escape percentages from keyword value

%asset_name^replace:25:TW3NTYF1V3^urlencode^replace:.25:&#37;^urldecode^replace:TW3NTYF1V3:25%

Result: %some_matrix_keyword% => &#37;some_matrix_keyword&#37;

Replace all white space occurrences (including new lines) with a single space

%asset_metadata_description^replace:\s: %

Result: Some description
with line break => Some description with line break .

Or, to condense all white space:

%asset_metadata_description^replace:\s+: %

Strip HTML but retain spaces between paragraphs and line breaks

% asset_metadata_description^replace:(<\/p>|<br\/?>):\1 ^striphtml%

Result: Line 1.
Line 2. => Line 1. Line 2. . If necessary, cater for other elements like h[1-6]

Print your site path without the domain

<MySource_PRINT id_name="__global__" var="current_asset" keyword="asset_url^replace:((.)*\.\D{2,3}):/" />

This will take all characters up until it reaches a period character, followed by characters of length 2 to 3 ( .com.au , .net , etc) and replace them with the / character. Please note that if any of your webpages contain a period in the page directory (such as /example.page/ ), this will not work.

Result: http://www.example.com.au/newpage/test-page => /newpage/test-page

Print a schema relative path for an asset URL

%globals_asset_url^replace:.*\/\/://%

Result https://www.site.com/page => //www.site.com/page

Remove certain parts of a URL (including colon)

This trick uses the hexadecimal representation of a character, in this case colon: "\x3A".

First group matches "http://" or "https://". Second group matches everything after the domain (first slash).

%asset_url^replace:(https?\x3A\/\/)|(\/.*):%

Using colon in a time stamp

Instead of having a separate keyword replacement for each part of the date format, use a different divider (eg =), then replace the url-encoded value.

%globals_asset_created^date_format:H=i=s^urlencode^replace:3D:3A^urldecode%

Using colon in ^eq:: return value

Uses the base64 value for colon (Og==).

%globals_asset_assetid^neq:1:Og==^base64decode% 

Return the first or last word (even if hyphenated) from a string

%asset_name%                   ==>  John-Paul Syriatowicz
%asset_name^replace:(\s).*$:%  ==>  John-Paul
%asset_name^replace:.*(\s):%   ==>  Syriatowicz

Remove the last or first character of a string

%asset_name%                               ==>  ABC
%asset_name^replace:.$:%                   ==>  AB
%asset_name^reverse^replace:.$:^reverse%   ==>  BC

Miscellaneous

Print different asset value depending on asset type (parse file example)

<MySource_PRINT var="asset_type^replace_keywords:contains:Redirect:{redirect_url}:{asset_url}" />

Redirect pages will print redirect_url keyword value.

All other pages will print asset_url keyword value.

List out current asset contents with it’s own paint layout

When using %asset_contents% keyword replacement in an asset listing, be sure to use the following to ensure it is painted with it’s own paint layout:

% asset_assetid^as_asset:asset_contents%

Output 'checked="checked"' if asset ID is in a comma-separated list of IDs

This one his handy if you need to store multiple asset IDs in a single metadata field, and then spit them out in a form

% globals_user_metadata_feed_subscriptions^replace_keywords:replace:.*\b{asset_assetid}\b.*:\1^replace_keywords:eq:{asset_assetid}:checked="checked"%

Use metadata of homepage if current page metadata is empty

Use case: Custom background images on children pages which are different to the home page. If not custom image set, use the homepage image.

%globals_asset_metadata_setting.background.image^as_asset:asset_url^replace_keywords:empty:{globals_site_index_id^as_asset:asset_metadata_setting.background.image^as_asset:asset_url}%

Get current URL

If %globals_asset_url% isn't what you want, try one of:

%globals_site_url%
%globals_server_REQUEST_URI%
%globals_server_HTTPS^eq:on:https:http%://%globals_server_SERVER_NAME%%globals_server_REQUEST_URI%

Print dynamic value depending on if you are on the homepage or not

//Up to version 5.4.2.2:
%globals_asset_assetid^replace:{globals_site_assetid}|{globals_site_index_id}:H^eq:H:home:inside%

//From version 5.4.3.0 and up:
%frontend_asset_is_homepage^eq:1:home:inside%

Change the name of the commit button on Asset Builders

Change "Commit" to whatever the commit button's value is in your system.

%commit_button_clean^replace:Commit:Submit Comment%

The Latest

Let Us Know What You Think

Let us know if you spot any errors or if you have any ideas on how we can improve the Matrix Community Website.

Contact Squiz for Demo

Let us show you the true power of Squiz Matrix by giving you a personalised demonstration.