PHx/CustomModifiers
From MODx Wiki
[edit]
Installation
- Login to the MODx manager
- Go to Resources -> Manage Resources -> Snippets
- Click "new snippet"
- Prefix the snippet name with "phx:"
[edit]
Contributed
[edit]
phx:get
- usage: [+phx:get=`name`+]
<?php return $_GET[$options]; ?>
[edit]
phx:post
- usage: [+phx:post=`value`+]
<?php return $_POST[$options]; ?>
[edit]
phx:bbcode
- description: parse bb code (also escapes all html and MODx tags characters)
- usage: [+variable:bbcode+]
<?php $string = preg_replace('~\[link\]www.(.+?)\[/link\]~is', '<a href="http://www.\1">www.\1</a>', $string); $string = preg_replace('~\[url\]www.(.+?)\[/url\]~is', '<a href="http://www.\1">www.\1</a>', $string); $string = preg_replace('~\[img\](.+?)\[/img\]~is', '<img src="\1" alt="[image]" style="margin: 5px 0px 5px 0px" />', $string); $string = preg_replace('~\[img-l\](.+?)\[/img\]~is', '<img src="\1" alt="[image]" style="border: thin solid #DFE5F2; FLOAT: left; MARGIN-RIGHT: 20px" />', $string); $string = preg_replace('~\[img-r\](.+?)\[/img\]~is', '<img src="\1" alt="[image]" style="border: thin solid #DFE5F2; FLOAT: right; MARGIN-LEFT: 20px;" />', $string); return $string; ?>
[edit]
phx:parent
- description: get specified document field from parent document (id)
- usage: [+variable:parent=`field`+]
- defaults to pagetitle
<?php $parent = $modx->getParent($output,1,$field); return $parent[$field]; ?>
[edit]
phx:rating
- description: user rating system based on Jot post count
- usage: [+comment.userpostcount:rating+]
<?php $defaultValue = " <img src=assets/images/icons/star.png />"; $newvalue = '<img src=assets/images/icons/bronze.png />'; } $newvalue = '<img src=assets/images/icons/silver.png />'; } $newvalue = '<img src=assets/images/icons/gold.png />'; } else { $newvalue = $defaultValue; } return $newvalue; ?>
[edit]
phx:url
- description: takes a document id and creates the corresponding modx link (similar to [~ ~])
- usage: [*id:url*]
<?php return $modx->makeUrl($output); ?>
[edit]
phx:uri
- description: takes a string and prepares it to be a part of a valid URI.
- usage: [+string:uri+]
<?php ?>
[edit]
phx:7bit
- description: returns the 7bit representation of a string
- usage: [+string:7bit+]
<?php $text = preg_replace(array('/ß/','/&(..)lig;/','/&([aouAOU])uml;/','/&(.)[^;]*;/'),array('ss',"$1","$1".'e',"$1"),$text); return $text; ?>
[edit]
phx:nohttp
- description: Removes the http:// from a URL, to create a display-friendly web address
- usage: [+string:nohttp+]
<?php return $url; ?>
[edit]
phx:phpthumb
- [+myimage+] contains the full path to the image
- [+myimage:phpthumb=`w=450`+]
- [+myimage:phpthumb=`w=450&ar=x&fltr{}=gray&fltr{}=rcd|8|1`+]
- [+myimage+] contains only the filename
- [+myimage:phpthumb=`w=450#[(base_path)]assets/images/`+]
- [+myimage+] contains no data referring to the image
- [+myimage:phpthumb=`w=450#[(base_path)]assets/images/myimage.jpg#1`+]
- added by: maFF
[edit]
phx:stripsnippet
- description: Removes cached and uncached snippet in output (usefull in Ditto summarize)
- usage: [+string:stripsnippet+]
<?php $string=$output; return $string; ?>
[edit]
phx:genitive
- description: Creates the genitive of a name - appends either ′s or ′
- usage: [+myname:genitive+] or [+myname:genitive=`endchar|normalchar|otherchar`+]
- added by: maFF
<?php /** * Creates the genitive of a name. * * Examples: * Joe => Joe's * Mary => Mary's * Lucas => Lucas' * * Usage: * * [+myname:genitive+] * [+myname:genitive=`endchar|normalchar|otherchar`+] * */ $name = $output; $endchar = (!empty($options[0])) ? $options[0] : 's'; // char a name has to end with to get treated specially $otherchar = (!empty($options[2])) ? $options[2] : '′'; // char to append to names which end with $endchar else $genitive = $name.$normalchar; return $genitive; ?>
[edit]
phx:ifnotempty
- description: The opposite of the native PHX "isempty" function. Returns the option value ONLY if the input value is empty (excluding whitespace)
- usage: [+string:ifnotempty=`String to return if not empty`+]
<?php return $options; } ?>
[edit]
phx:timesince
- description: Converts a unix timestamp into the number of years, months, days, hours, minutes, and optionally seconds from the current time.
- usage: [+date:timesince+]
- added by: apoxx
<?php { function timeSince($timestamp) { { $timeago = '0 seconds ago.'; return $timeago; } 'year' => 31556926, 'month' => 2629743, 'day' => 86400, 'hour' => 3600, 'minute' => 60, // 'second' => 1 ); $timeago = ''; foreach ($units as $key => $val) { if ($seconds >= $val) { $seconds = ($seconds-($results*$val)); $timeago .= ($results >= 2) ? $results . ' ' . $key . 's, ' : $results . ' ' . $key . ', '; } } } } return timeSince($output); ?>
[edit]
phx:smileys
- description: Converts text patterns into smiley images.
- usage: [*content:smileys*]
- notes: it works with the silk iconset by famfamfam out of the box, if you put the icons into assets/images/smileys. If you want to use other smileys/replacement tags just edit the smileys array.
- added by: maFF
<?php $smiley_basepath = $modx->config['base_url'].'assets/images/smileys/'; $smiley_imagetag = '<img src="'.$smiley_basepath.'##smiley##" alt="##text##" title="##text##" />'; 'emoticon_evilgrin.png', // file name ':D'), // text version for alt and title tags 'emoticon_happy.png', ':)'), 'emoticon_unhappy.png', ':('), 'emoticon_wink.png', ';)'), 'emoticon_tongue.png', ':P'), 'emoticon_surprised.png', ':O') ); foreach($smileys as $search => $smiley) { $thistag = $smiley_imagetag; } return $output; ?>
[edit]
phx:ordinal
- description: Ordinal numbers.
- usage: [*number:ordinal*]
- notes: Adds 'st' to 21 to make it the 21st or 'nd' to 132 for 132nd.
- added by: bwente
<?php { function ordinal($cardinal){ : (($test_c < 4) ? ($test_c < 3) ? ($test_c < 2) ? ($test_c < 1) ? 'th' : 'st' : 'nd' : 'rd' : 'th')); return $cardinal.$ext; } } return ordinal($output); ?>
[edit]
phx:textile
- description: Format content using the textile parser class.
- usage: [*content:textile*]
- notes: You need to download Textile and save the class file into assets/snippets/textile (or change the path in the snippet).
- added by: maFF
<?php $classTextile = $modx->config["base_path"].'assets/snippets/textile/classTextile.php'; require_once($classTextile); } } $textile = new Textile(); $output = $textile->TextileThis($output); return $output; ?>
[edit]
phx:word_limit
- description: limits content to specified number of words (good for news summary)
- usage: [+content:word_limit=`10`+] - limits content to 10 words
- added by: yentsun
<?php $retval = $output; { $retval = $output; } else { } return $retval; ?>
[edit]
phx:character_limit
- description: limits content to specified number of characters (good for news summary)
- usage: [+content:character_limit=`35`+] - limits characters to 35
- added by: themancan
<?php return $output_truncated; ?>
[edit]
phx:alternateClass
- description: Allows rows or other repeating elements to alternate provided the $output is an integer generated by a loop.
- usage: [+phx:alternateClass=`class name`+]
- example: <li class="[+maxigallery.picture.pos:alternateClass=`alt`+]"> outputs <li class="alt"> for every other <li> in the loop.
- notes: Originally designed for use with MaxiGallery to apply an alternating class to the <li> galleryPictureTpl template chunk.
- added by: smashingred
[edit]
phx:docfield
- description: get specified field from document (id)
- usage: [+docid:docfield=`field`+]
- defaults to pagetitle
- added by: bwente
<?php return $docfield[$field]; ?>
[edit]
phx:tv
- description: get a template variable from any page id
- usage: [+phx:tv=`<docid>?<templatevar>`+] , where <docid> is the document id (e.g. 25) and <templatevar> is a template variable associated to the given docid.
- default: if the document id or the template variable don't exist no output is produced
- added by: natalino
<?php $result = $modx->getTemplateVar($tv, 'name', $id, 1); return $result['value']; } ?>
example: placing the place of birth from page id 14, as part of the content in page id 22. Place this content while editing page id 22
[+phx:tv=`14?place_of_birth`+]
[edit]
phx:evenodd
- description: Returns even if even, otherwise odd. Nearly identical to the 'alternateClass' above.
- usage: [+ditto_iteration:evenodd]. Useful for creating alternate templates in Ditto without needing an additional chunk. For example, class="news_item_[+ditto_iteration:evenodd+]" will return news_item_even or news_item_odd
- default: Defaults to "odd", as there is no test to ensure the input is an integer.
- added by: themancan
<?php if ($output % 2) { return 'even'; } else { return 'odd'; } ?>
[edit]
phx:price_format
- description: Formats a number (TV) to a specific format with a specific number of decimals. Useful for allowing users to enter a "price" TV, and making sure you have a consistent XX.YY format, even if they just enter XX.
- usage: [+product_price:price_format+].
- default: Defaults to 2 decimal places.
- added by: themancan
<? function format_number($str,$decimal_places='2',$decimal_padding="0"){ /* taken from http://us2.php.net/manual/en/function.number-format.php#54799 */ /* firstly format number and shorten any extra decimal places */ /* Note this will round off the number pre-format $str if you dont want this fucntionality */ $number[1] = (isset($number[1]))?$number[1]:''; // to fix the PHP Notice error if str does not contain a decimal placing. return (float) $number[0].'.'.$decimal; } } return format_number($output); ?>
[edit]
phx:make_url
- description: Takes a string and makes it a URL if it's not.
- example: If a user enters 'www.example.com', but you want to link to that, if you did 'href="www.example.com"' it wouldn't work. Yeah, you should be validating that on the server side anyway, but this modifier is useful in case you can't. It will make "www.example.com" into "http://www.example.com", but won't touch things that start with an http:// or https://
- usage: [+member_url:make_url+]
- default: Adds "http://" to the beginning of the string.
- added by: themancan
<? return $output; } else { return 'http://' . $output; } ?>
[edit]
phx:zeropad
- description: Zero-padding a string. Takes the number of total digits as the input.
- example: This can be useful when you want to output some kind of ID numbers with fixed digits, or US zip codes: "00001", "00124", "90210"
- usage: [+ditto_iteration:zeropad=`3`] [*zipcode:zeropad=`5`*]
- default: No zero-padding.
- added by: ryanlwh
<? ?>
