PHx 1.4.1
PHx (Placeholders Xtended) will add the capability of output modifiers when using placeholders, template variables and settings tags. It also supports nested tags.
Submitted: Oct 19th 2006 | License: GPL - GNU Public | Downloads: 460
This resource has been deprecated.
Description
PHX (Placeholders Xtended) extends the use of placeholders, template variables and settings tags so you can easily format how the final output should look like. Will eliminate the need for snippets where you'd use them to format a value for display. Great for templating!
Transforming a value is is as easy as adding :modifier.
Examples:
a normale placeholder like [+placeholder+]
can be extended to a PHx placeholder: [+placeholder:esc+]. The same applies for template variables like:
[*createdby*]
Some modifiers take options like:
[*createdby:date=`%a %B %d, %Y at %H:%M`*]
Multiple modifiers can be placed into one placeholder/template variables and will be processed from left to right.
[*somevar:esc:nl2br:strip*]
Available modifiers:
String:
Returns current value with all alphabetic characters converted to lowercase.
Returns current value with all alphabetic characters converted to uppercase.
Make current value's first character uppercase
Returns length of the current value.
Strips html tags from the current value.
Escapes html and the [ ] characters.
converts new lines to
<br />
strip newlines and multiple spaces.
Reverses the current value.
Breaks words in the current value longer than the given length of characters by putting a space in between.
Default: 70 characters.
Returns the first X characters from the current value.
Default: 100 characters.
Special:
Converts unix timestamps to the format specified. Like PHP strftime (details)
Creates a MD5 hash from the current value
Formats a userid value (webuser have negative ids) to a field specified. Similar to the fields used in the database.
Examples fields: username, useremail
use simple calculations like - * + /.
The "?" character is replaced by the current value of the extension but you can also use nested tags.
Example calculation: ?+1+(2+3)+4/5*6
use "other value" when the output of the placeholder/templatevar is empty.
Translates the value of placeholder/templatevar to a specified output.
Example:
[+placeholder+] = 1
[+placeholder:select=`0=OFF&1=ON&2=UNKNOWN`+] = ON
[+placeholder+] = 2
[+placeholder:select=`0=OFF&1=ON&2=UNKNOWN`+] = UNKNOWN
Conditional operators:
is equal to
is not equal to
is equal or greater than
is equal or lower than
is greater than
is lower than
shown if conditions are true
shown if conditions are false
it's possible to combine multiple statements with:
Examples:
More examples will follow soon.
Example #1
myplaceholder is set to myvalue
[+myplaceholder:is=`myvalue`:then=`Correct`:else=`Incorrect`+]
will return: Correct
[+myplaceholder:isnot=`myvalue`:then=`Correct`:else=`Incorrect`+]
[+myplaceholder:is=`othervalue`:then=`Correct`:else=`Incorrect`+]
will both return: Incorrect
Example #2
myplaceholder is set to 2
someplaceholder is set to 3
otherplaceholder is set to 1
[+myplaceholder:is=`2`:then=`{{ChunkGood}}`:else=`{{ChunkBad}}`+]
will parse/display the contents of chunk called ChunkGood.
[+myplaceholder:gt=`1`:then=`Yes`:else=`No`+]
[+myplaceholder:lt=`3`:and:gt=`1`:then=`Yes`:else=`No`+]
[+myplaceholder:lt=`[+someplaceholder+]`:then=`Yes`:else=`No`+]
[+myplaceholder:islt=`2`:then=`Yes`:else=`No`+]
[+myplaceholder:isnot=`2`:or:lt=`3`:then=`Yes`:else=`No`+]
will all return Yes.
[+myplaceholder:isnot=`2`:then=`Yes`:else=`No`+]
[+myplaceholder:gt=`[+someplaceholder+]`:then=`Yes`:else=`No`+]
[+myplaceholder:lt=`2`:then=`Yes`:else=`No`+]
[+myplaceholder:gt=`2`:then=`Yes`:else=`No`+]
[+myplaceholder:lt=`1`:then=`Yes`:else=`No`+]
will all return No.
Custom Modifiers:
a modifier is in fact a replacement for a simple snippet to process a given value to anything you like. It's possible to create your own custom modifiers/mini-snippets by adding a new snippet into the MODx resource manager.
Because a modifier doesn't hold complicated code you don't need parameters other than what a modifier in PHx gets from the parser. There are two main variables:
1. $output
the current value of the variable that is getting "modified"
2. $options (optional)
the optionial string that a placeholder could have
to bring this into perspective here are some short examples:
myplaceholder is set to "test"
[+myplaceholder:mymodifier+]
in this case $output contains the value "test" and $options contains nothing cause they aren't specified.
[+myplaceholder:mymodifier=`my options`+]
in this case $output still contains the value "test" and $options contains the value "my options".
Creating a custom modifier (example 1):
With this knowledge we are going to create a new custom modifier. In this example I want to create a modifier (without options) that adds the string " because I love MODx" to a variable.
1. Login to the MODx manager
2. Go to Resources -> Manage Resources -> Snippets
3. Click "new snippet"
4. For the snippet name we enter "phx:love"
for PHx to know about the custom modifier all snippets created for PHx should be prefixed with "phx:" the string (containing NO spaces) after the prefix will be the actual modifier name. In this case our modifier will be triggered by adding :love to the placeholder like [+myplaceholder:love+].
5. Now we are going to enter the modifier code into the snippet code field. For this example we create it like this:
$newvalue = $output. " because I love MODx";
return $newvalue;
6. Click "save" and your custom modifier is ready for use!
Creating a custom modifier (example 2):
Similar to example 1 we are now going to create a modifier that will do the same BUT if there are options specified it will take the options value as the string to append.
1. Login to the MODx manager
2. Go to Resources -> Manage Resources -> Snippets
3. Click "new snippet"
4. For the snippet name we enter "phx:love2"
for PHx to know about the custom modifier all snippets created for PHx should be prefixed with "phx:" the string (containing NO spaces) after the prefix will be the actual modifier name. In this case our modifier will be triggered by adding :love to the placeholder like [+myplaceholder:love2+].
5. Now we are going to enter the modifier code into the snippet code field. For this example we create it like this:
$defaultValue = " because I love MODx";
if (strlen($options)>0) {
$newvalue = $output . $options;
} else {
$newvalue = $output . $defaultValue;
}
return $newvalue;
6. Click "save" and your custom modifier is ready for use!
Tips and Tricks
Using PHx with other snippets
If a snippet handles it's own templating, by replacing in a loop for example (like most of the snippets do) it's not possible to use the modifiers directly. In this case you still CAN use PHx by using a special modifier called phx:input.
So in the case described above you can't use the default method like:
[+myplaceholder:modifier1:modifier2+]
but you can do it like:
[+phx:input=`[+myplaceholder+]`:modifier1:modifier2+]
This works because the snippet can still replace it's placeholder value and PHx just takes whatever value is defined bij the input modifier.
Instructions
Installation
1) Download and extract the archive.
2) Create a directory called "phx" in your [MODx Directory]/assets/plugins directory
3) FTP or copy the files into [MODx Directory]/assets/plugins/phx
4) Create a new plugin in the manager called "PHx" and copy/paste the contents of phx.plugin.txt into the code field.
5) Check "OnParseDocument" at the System Events tab
Updates
fixed: userinfo to also look for manager id when using [+phx:userinfo=`field`+]