
I haven’t tried this particular code, but it looks fine to me (and I use similar code to set dynamic CSS all the time). Can you post all of your snippet code? If for example you left off a closing comment tag above the part that checks the user agent string then it might be getting closed after that block at the next comment.
I dont have this working YET. Snippet runs and return my default css. User agent is showing when echo. filenames and typos checked.
Cached cleared, different browsers used.


I’ve been the error, too. I got this working. Now I just need to bone up on my CSS layout skills for mobile.
@BobRay
Can this snippet be repurposed to display a chunk instead? While switching to custom CSS is very useful, I would rather have it switch to a template instead.
<?php if (strpos($_SERVER['HTTP_USER_AGENT'],$key)) { ... }
This check for a string in a string is not always working.
Quote from: BobRay at Dec 28, 2008, 11:09 PM
<?php if (strpos($_SERVER['HTTP_USER_AGENT'],$key)) { ... }
If the string you are searching for is at position 0. strpos will return a 0, and 0 == false in PHP. Changing the check to strpos(...)!==false should do the trick, since !== and === checks for type and value.
For more info, check the manual: http://dk.php.net/function.strpos

if (strstr($_SERVER['HTTP_USER_AGENT'],$key))
select a chunk name from the array rather than a file name and use $modx->getChunk(chunk_name)I’m afraid you’re going to have to hold my hand on this. It’s an easy switch for you, but not so for me.
<?php
/* Snippet Title: CustomChunk */
$output = '';
$cssBase = $modx->config['site_url'] . 'assets/templates/catalytic2/css/'; /* or wherever you keep your css files */
/* array of browser agent names and their respective chunk templates */
[color=red]/* select a chunk name from the array rather than a file name and use $modx->getChunk(chunk_name) WHERE TO USE getChunk?? */
/* And how does one name a chunk in this code without the chunk's curlybrackets invoking the chunk?*/[/color]
$browserArray = array(
'iPhone' => 'mobile.css',
'SymbianOS/9.1' => 'symbian.css',
'anotherbrowser' => 'otherbrowser.css',
'yetanother browser' => 'stillotherbrowser.css'
);
/* see if we recognize one */
foreach ($browserArray as $key => $value) {
if (strstr($_SERVER['HTTP_USER_AGENT'],$key)) {
$output .= $cssBase . $value;
break;
}
}
/* if we didn't find one, use the default css file */
[color=red]/* better: get a default chunk (non-mobile template) */[/color]
if ($output == '') {
$output = $cssBase . 'template_css.css';
[color=red]/* we don't need to output a file location. How do we output a chunk instead?*/[/color]
}
return $output;
?>