How can I add an if/else statement into my template so if its the homepage it shows a block of code, otherwise it shows another block of code?
-
☆ A M B ☆
- 24,524 Posts
There are a number of ways that you can do this; which version of MODx are you using?
The fastest way would be a snippet:
[[!ShowAtHome? &yesChunk=`SomeChunkName` &noChunk=`SomeOtherChunkName`]]
<?php
/* ShowAtHome snippet */
if ($modx->resource->get('id') == $modx->getOption('site_start')) {
$chunk = $yesChunk;
} else {
$chunk = $noChunk;
}
return $modx->getChunk($chunk);
Or, a slightly more efficient version of the same thing:
<?php
/* ShowAtHome snippet */
return $modx->getChunk( ($modx->resource->get('id') == $modx->getOption('site_start')) ? $yesChunk : $noChunk);
-
☆ A M B ☆
- 24,524 Posts
Evo got a "HomeAlone" snippet like this years ago, but I’ve been using a TV for this instead for almost as long. The default value for the TV is what it normally should be (i.e. the chunk used on all other pages), and it’s only edited on the home page resource (to specify the chunk used for the home page). Then I assign the TV to a group that only the Admin user belongs to, so nobody else even sees it when editing resources. If it’s not something that anybody but Admin will ever need to change, I just use the TV content itself rather than chunks.
Quote from: ripcurlksm at Nov 12, 2010, 11:57 PM
How can I add an if/else statement into my template so if its the homepage it shows a block of code, otherwise it shows another block of code?
[[*id:eq=`1`:then=`I am page 1`:else=`I am not page 1`]]
(assuming the home page has ID 1)
--
cal wilson
oksushi.com
I know this is way old... that said, I love the simplicity of the output filter method. To smarten it up just a tad you could modify the code to:
[[*id:eq=`[[++site_start]]`:then=`I'm the site_start`:else=`Nope, not site_start`]]
I LOVE MODX! | greyskymedia.com
-
☆ A M B ☆
- 24,524 Posts
I will have to say that over the years I've gradually shifted from manipulating a single template to using two or three, occasionally more, templates to handle the home page and one- or two- or more column pages. Using chunks to manage the common sections (masthead, main menu, footer) having multiple templates can have a number of advantages The templates can act as categories for resources, thus allowing for organization of TVs and other resource content manipulation.
Oh for sure! And especially if you're doing form customization. Personally I had some weird banner that the client wanted ONLY on the home page. My code above seems like the easiest way to accomplish this.
I LOVE MODX! | greyskymedia.com