MODX Community

Different chunk according to context

I have an existing website (.co.uk) to which I wish to add an alternative TLD (.com). Stuff like phone number, email address, physical address are stored in chunks.
What I’d like to do is display alternative chunks (same pages) when the .com is visited and so I have 2 questions…
Firstly, I found this tutorial for creating contexts, it’s old but seems to explain things clearly. Is this still correct? http://www.belafontecode.com/modx-revolution-hosting-multiple-domains/
Secondly, I found this method for displaying a chunk according to context…
[[$[[*context_key]]_context_chunk]]
…but is there a way of only switching specified chunks, so defaulting to the original context, unless I specify, with code similar to above?

1 Like

It seems like the nested chunk tag should work for you, but it requires you to name the chunk to match the tag. So if your contexts are web and othercontext, the tag would be:

[[$[[*context_key]]_chunk]]

and the chunks would be named web_chunk and othercontext_chunk

This assumes that the resources have web and othercontext in their context_key field, which they would automatically if you created them within those contexts, but might not if you created them earlier. You could find out in the DB.

FWIW, I would probably do this with a snippet:

[[!GetContextChunk]]
/* GetContextChunk snippet */
if ($modx->context->get('key') === 'web') {
   $chunkName = 'web_chunk';
} else {
   $chunkName = 'othercontext_chunk';
}

return $modx->getChunk($chunkName);

This method doesn’t depend on the context of the resource. Instead it responds to the actual context the user is currently in. This assumes that you’ve set up the context routing correctly so that the user is actually in the correct context.

Thank you Bob. It looks like the snippet is the way to go but it also looks like I’d need a snippet for every chunk - is that right?
The website consists of around 2,000 pages - do all these pages have to exist in the other context as well, for this method to work?

You can send the name of the chunk without its prefix as an argument if you need it for multiple chunks.

In this example, the chunks are named web_chunkName and othercontext_chunkName

[[!GetContextChunk &chunkName=`chunkName`]]
/* GetContextChunk snippet */
$chunkName = $scriptProperties('chunkName');

if ($modx->context->get('key') === 'web') {
   $chunkName = 'web_' . $chunkName';
} else {
   $chunkName = 'othercontext_' . $chunkName';
}

return $modx->getChunk($chunkName);

If you just want to change a chunk in dependence of the domain-name, you could use a snippet with something like this:

$domain = parse_url($_SERVER['HTTP_HOST']);
if ($domain === 'domain.co.uk') {
   $chunkName = 'domain.co.uk';
} else {
   $chunkName = 'domain.com';
}
return $modx->getChunk($chunkName);

My problem was that I thought I needed to use contexts and I don’t. It’s much simpler than that. Thank you Bob and Raffenberg

Ah . . . I thought you needed the contexts for something else. Raffenberg’s solution is definitely the way to go.

Sorry Bob - I was thinking that to use two domains I needed two contexts. Don’t know why but I got it in my head that because the domain is set in config it the site would only work under that domain unless I used contexts. Good lesson learned.

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.