Update!! (16-02-2007)
This thing is getting bigger! Here is the snippet code.
Change Log and
To Do list at the end of this post.
Comments and testers are welcome!
<?php
/* AltOutput - by Blaise Bernier
* * * * * * * * * * * * * * * * * * * * * * * *
* (c) 02-02-2007 Medial Design
* * * * * * * * * * * * * * * * * * * * * * * *
*
* Recent updates :
* * * * * * * * * * * * * * * * * * * * * * * *
* Blaise Bernier (16-02-2007) :
* - Completed placeholder support
* - Added Javascript and CSS conditions (not tested)
* Blaise Bernier (12-02-2007) :
* - Added very basic support for placeholders in output chunk;
* - Added a property to the snippet to select the phpSniff path
* (default="manager/includes/sniff/")
*/
$snippetPath = isset($snippetPath) ? $snippetPath : "manager/includes/sniff/";
/*** Includes ***/
require_once($snippetPath . "phpSniff.class.php");
/* Snippet usage and params description :
* * * * * * * * * * * * * * * * * * * * * *
*
* &conditionData : data to check in the condition. Values : browser, javascript, css, java
* (default="browser")
*
* * * * General parameters : * * * * * * *
*
* &defaultChunk : Chunk used by default. If null, no output will be made if no conditions are met.
* (default = "")
*
* * * * Browser check : * * * * * * * * * *
*
* &IEChunk : Chunk used if browser is IE. If null, default chunk is used.
* (default = "")
* &FFChunk : Chunk used if browser is FireFox or Mozilla. If null, default chunk is used.
* (default = "")
* &OperaChunk : Chunk used if browser is Opera. Iff null, default chunk is used.
* (default = "")
*
* * * * Javascript check : * * * * * * * *
*
* &JSChunk : Chunk used if browser has javascript enabled. If there is no javascript support, default chunk is used.
* If no chunk is specified, there will be an output only if there is no JS support. If there is no default
* chunk, there will be an output only if there is JS support.
* (default="")
*
* * * * CSS Check : * * * * * * * * * * * *
*
* &CSSChunk : Chunk used if browser support CSS, no matter wich version. If null, there will be a CSS version check.
* If there is no CSS support, default chunk is used.
* (default="")
* &CSS1Chunk : Chunk used if browser supports CSS1. If there is no CSS support, default chunk is used.
* (default="")
* &CSS2Chunk : Chunk used if browser supports CSS2. If there is no CSS support, default chunk is used.
* (default="")
*
* If none of these chunk is specified, there will be an output ouly if there is no CSS support. If no default chunk is
* set, there will be an output only if there is CSS support.
*
* * * * Additional data : * * * * * * * * *
*
* You can fill placeholders in your chunks with some extra data. To do so,
* simply call your snippets with additional parameters like these :
*
* &extraDataTest=`Some info` &extraDataSomeName=`[!SomeSnippetOutput!]` &extraDataN=`[*AnyDataSource*]`
*
* In your chunks, add placeholders this way:
*
* [+AltOut.DataTest+] [+AltOut.DataSomeName+] [+AltOut.DataN+]
*
*/
/* Initialization */
$clientdata =& new phpSniff();
$conditionData = (isset($conditionData)) ? $conditionData : "browser";
$defaultChunk = (isset($defaultChunk)) ? $defaultChunk : "";
$output = "";
/* Define chunks depending on the condition */
switch($conditionData) {
case "browser" :
$IEChunk = (isset($IEChunk)) ? $IEChunk : $defaultChunk;
$FFChunk = (isset($FFChunk)) ? $FFChunk : $defaultChunk;
$OperaChunk = (isset($OperaChunk)) ? $OperaChunk : $defaultChunk;
break;
}
/* Select output chunk */
switch($conditionData) {
case "browser" :
$clientBrowser = $clientdata->property('browser');
switch ($clientBrowser) {
case "ie" :
$output = $modx->getChunk($IEChunk);
break;
case "mz" :
case "fx" :
$output = $modx->getChunk($FFChunk);
break;
case "op" :
$output = $modx->getChunk($OperaChunk);
break;
default :
$output = $modx->getChunk($defaultChunk);
}
break;
case "javascript" :
$output = $clientdata->has_feature('java') ? $modx->getChunk($JSChunk) : $modx->getChunk($defaultChunk);
break;
case "css" :
if (isset($CSSChunk)) {
$output = ($clientdata->has_feature('css1') || $clientdata->has_feature('css2')) ? $modx->getChunk($CSSChunk) : $modx->getChunk($defaultChunk);
}
else {
$output = $clientdata->has_feature('css1') ? $modx->getChunk($CSS1Chunk) : ( $clientdata->has_feature('css2') ? $modx->getChunk($CSS2Chunk) : $modx->getChunk($defaultChunk) );
}
break;
}
/* Populate extra data and setup placeholders */
$extraDataCount = 0;
foreach ($modx->event->params as $pName => $pValue) {
if (substr($pName,0,9) == "extraData") {
$modx->setPlaceholder("AltOut.Data" . substr($pName,9), $pValue);
$extraDataCount++;
}
}
/* Fill the chunk placeholders if there is some extra data */
$output = ($extraDataCount > 0) ? $modx->mergePlaceholderContent($output) : $output;
/* Return the output */
return $output;
?>
Change Log:
- (16-02-2007) :
[list]
[li]Completed the placeholder support. It’s easy to use and flexible.
- Add support for CSS and Javascript detection.
[/li]
[li](12-02-2007) :
- Changed the ’require’ at the beginning for ’require_once’ so you can use the snippet more than once per page
- Added a simple way to change the path to phpSniff. Now the version included in MODx is used by default. Still not using the API function.
- Added some basic support for placeholders. It let you add some extra data to control the output of the snippet more easily.
[/li]
[/list]
To Do:
(updated on : 16-02-2007)
Items on top of the list have an higher priority.
- Add support for more browsers by using some ’dynamic’ way of naming chunks : <chunk name>-Default, <chunk name>-IE, etc. If no corresponding chunk is found, then default one will be used and if no default chunk exists, nothing will be returned.
- Add conditions for MODx user information: User group, Web user group, User logged in manager, and more!
Blaise Bernier
www.medialdesign.com - Solutions for small business, hosting, design and more!
I gave this a whirl but it hasn’t worked, could you give an example how to call it ?
[[AltOutput?defaultChunk=`ChunkName`&IEChunk=`Chunk2Name`]]
For the moment, conditionData is useless because I haven’t implemented anything else for the moment.
Works really well for me!
Blaise Bernier
www.medialdesign.com - Solutions for small business, hosting, design and more!
Looking really good, keep it up!
I confess I did not take the time to test it yet, but it’s on my todo list now it could prove really helpful in my development process

I’ll keep you posted about how things go for me...
.: COO - Commerce Guys - Community Driven Innovation :.
MODx est l'outil id
Thanks a lot! I plan to add lots of new condtion types soon. I’ll also improve my placeholder support to be more efficient and flexible. Maybe I’ll also add an "eval" condition type that will return a chunk depending on the result of a PHP expression.
Blaise Bernier
www.medialdesign.com - Solutions for small business, hosting, design and more!
I want to retrieve the params of the snippet beginning with "extraData". I tried to filter the $_POST array, but it dont work.
Blaise Bernier
www.medialdesign.com - Solutions for small business, hosting, design and more!
I updated the snippet with a few new features and complete placeholder support to send extra data to your chunks! See the first post of this thread for more information and source code.
Blaise Bernier
www.medialdesign.com - Solutions for small business, hosting, design and more!
I would like to see $modx->getUserData() be used instead of using your own phpSniff class as that’s just the same. Other than that, looks good to me! Thanks!
"He can have a lollipop any time he wants to. That's what it means to be a programmer."