// -------------------- // Snippet: HomeAlone // -------------------- // Version: 0.6a // Date: 2004.09.02 // [email protected] // // This snippet was designed to show other snippets or chunks // that should appear on the home page only, or on all BUT // the home page. // // Modifed 2004.07.13 // [email protected] // for multilanguage sites // use base snippet/chunk name in template // you must have a separate snippet/chunk for each language used // snippet/chunk name iteself uses basename + ID of main language homepage // homepage check checks to see if page ID = $lang // snippet/chunk names passed in call parameter // have $lang appended while being loaded into array // eg HomeBanner is used in call parameter // if the $lang of the page being loaded is 1, the snippet/chunk name // will be modified to HomeBanner1 // Configuration Settings // $HA_sHome and $HA_sNotHome [ set in snippet call parameter ] // These variables are pipe separated strings of snippet names to run // on the home page (in the case of $HA_sHome) or to run on all BUT the // home page (in the case of $HA_sNotHome). // Call the snippet like this: // [[HomeAlone?HA_sHome=FirstSn|SecondSn&HA_sNotHome=third|fourth]] // $HA_cHome and $HA_cNotHome // These work exactly the same way but for chunks. You can use 1, 2, 3 or all 4 // parameters in a single call if you wish, but this will tend to get confusing: // [[HomeAlone?HA_cHome=snippet1&HA_cNotHome=snippet2&HA_cHome=chunk1&HA_cNotHome=chunk2]] // Snippets will be processed first then chunks. // for multilanguage sites, get the $lang value // or use site_start $lang = isset($_COOKIE['Language']) ? $_COOKIE['Language'] : $modx->config['site_start']; // something has to be set for this to run if (isset($HA_sHome) || isset($HA_sNotHome) || isset($HA_cHome) || isset($HA_cNotHome)){ // decide if this is the home page - modified for multilang $atHome = ($modx->documentIdentifier == $lang) ? true : false ; // get the snippet/chunk for that language // find snippets $homeSnippets = isset($HA_sHome)? explode("|",$HA_sHome): ''; $notHomeSnippets = isset($HA_sNotHome)? explode("|",$HA_sNotHome): ''; // initialize output variable $output = ''; // if ($atHome){ if ($homeSnippets){ foreach($homeSnippets as $snippetName){ $output .= '[['.$snippetName.$lang.']]'; // add $lang to name } } } else { // other than home page if ($notHomeSnippets){ foreach($notHomeSnippets as $snippetName){ $output .= '[['.$snippetName.$lang.']]'; // add $lang to name } } } // find chunks $homeChunks = isset($HA_cHome)? explode("|",$HA_cHome): ''; $notHomeChunks = isset($HA_cNotHome)? explode("|",$HA_cNotHome): ''; // decide if this is the home page if ($atHome){ if ($homeChunks){ foreach($homeChunks as $chunkName){ $output .= '{{'.$chunkName.$lang.'}}'; // add $lang to name } } } else { // other than home page if ($notHomeChunks){ foreach($notHomeChunks as $chunkName){ $output .= '{{'.$chunkName.$lang.'}}'; // add $lang to name } } } return $output; }
$lang = isset($_COOKIE['Language']) ? $_COOKIE['Language'] : $modx->config['site_start'];

<ul><li><a href="index.php?id=[*lang*]">Home</a></li></ul>
<div id="topmenu" class="wide">
{{HomeMenuItem}}
[[ListMenuX?id=[*lang*]&depth=1&nodiv=true]]
</div> <!-- end top menu -->// SetLangCookie snippet for ModX // set cookie with language ID // 08-28-2005 // [email protected] $lang=$modx->documentIdentifier; setcookie("Language",$lang,time()+604800, "/", "", 0); if(isset($_COOKIE['Language']) && $_COOKIE['Language'] != $lang) { $url = $modx->makeURL($lang); $modx->sendRedirect($url); }
// SetLangCookie snippet for ModX // set cookie with language ID // 08-28-2005 // [email protected] $lang=$modx->documentIdentifier; setcookie("Language",$lang,time()+604800, "/", "", 0);
@EVAL
if($modx->documentObject['parent'] == 0) {
$lang=$modx->documentIdentifier;
}
else
if(isset($_COOKIE['Language'])) {$lang=$_COOKIE['Language'];}
else {$lang=$modx->config['site_start'];}
return $lang;

I decided that I don’t like that kludge of redirecting to sync the cookie with the new language choice, so I reworked the TV and the snippet.
Drop this snippet into each of your main language home page documents:
// SetLangCookie snippet for ModX // set cookie with language ID // 08-28-2005 // [email protected] $lang=$modx->documentIdentifier; setcookie("Language",$lang,time()+604800, "/", "", 0);
That sets the cookie.
Then use this TV wherever you need the $lang variable to be set, such as in setting the ID for a menu snippet:
Default value:
@EVAL if($modx->documentObject['parent'] == 0) { $lang=$modx->documentIdentifier; } else if(isset($_COOKIE['Language'])) {$lang=$_COOKIE['Language'];} else {$lang=$modx->config['site_start'];} return $lang;
This first checks to see if the document is one of the main root documents (parent=0; where your language home pages/folders are); if so, it uses that document’s id for the $lang variable. If not, it gets the $lang variable from the cookie; if that’s not available, then it uses the default site_start, which should be your default language’s home page. So even if the user has cookies disabled, he’ll still get the whole site in the default language, he just won’t be able to change languages.
The first check is necessary because the $lang variable is grabbed from the (old) cookie before the new cookie is set, causing the menus to be one step behind the user’s new language selection. This way, if the page is a new language selection, its ID is used, instead of the old cookie value. The cookie is now up-to-date, so further menu selections will be from the correct language folder.
@EVAL
if($modx->documentObject['parent'] == 0) {
$lang=$modx->documentIdentifier;
}
else
if(isset($_COOKIE['Language'])) {$lang=$_COOKIE['Language'];}
else {$lang=$modx->config['site_start'];}
return $lang;