Conditional statements in templates and element files
PermalinkFor example, header.php:
<div class="banner-wrapper <?php if ($current_page=='index') { ?>full<?php } ?>">
and in one of my templates, at top of page:
Thanks for any help.
$typeHandle = $c->getCollectionTypeHandle(); if($typeHandle=='full'){ //do some stuff! }
Usually, if you want to do different things on different pages, you should just create different page type templates. But if it's something in your header which is only a small difference, then yeah it can be more efficient to use a conditional "if" statement. But it really depends on exactly what you're trying to do.
Then I was thinking, screw it, I'll just put the components in each of the templates. But then I remembered that I have a very complex navigation with various components inside of it, and it was recommended elsewhere in the forums that I hard code it.
To do that, though, I'd have to have a conditional statement to change the CSS for the selected menu items (e.g., "About" is selected when on the About page).
Looks like I'm stuck, yeah?
It sounds like you have specific images and styles for each page in the menu, and here it sounds like you have specific styles for each page in your site. This is not going to be easy to do with any CMS system.
But if this is the design you have and the job you need to get done, obviously you need to do something to make it work :)
So what I recommend is that you create different page type templates for every different design you have. Don't use the $this->inc('elements/header.php') and $this->inc('elements/footer.php') technique that is common in most C5 themes, instead, each page type template should start with the doctype and end with the </html> tag -- a complete page basically. Doing it this way will make it very easy for you to include different stylesheets or different inline styles, or even different hardcoded menus.
You can use conditionals instead, but it's not worth it to use them unless you're only talking about minor changes -- otherwise your html templates become littered with hard-to-decipher PHP code, and when you go back to edit these in a week or a month or a year, it will be incredibly difficult to make sense of it.
I suppose there isn't a way to build the site with individual templates, as you suggest, but keep the main navigation separate and hard coded *and* able to display "selected" tabs? I'm guessing no, but I thought I'd ask as I have only a basic understanding of C5.
<?php $this->inc('elements/navigation_menu.php'); ?>
This is the same technique that is usually used for the header and footer, but there's no reason it can't be used for other things instead like a navigation menu.
As for the "selected" tab, it looked like your nav menu code was fairly complex, so it will not be as straightforward as usual. But if for example you denote the selected tab with <li class="selected">, and you know the path of every page, you could do something like this:
<li class="<?php echo ($c->getCollectionPath() == '/some/page') ? 'selected' : ''; ?>">
Note that if this is in a separate file, you'll want to put this line at the top:
$c = Page::getCurrentPage();
And also note that the path always needs to start with a slash but not end in one.
If your default.php file sits right inside the theme folder, what path, then, would the home page be?
Edit: Ha. Ok. Turns out it's "". Not even "/". So, got it, nevermind.
And THANK YOU SO MUCH for your help. Really, the generosity of you guys never ceases to amaze. Thanks.
The home page is either '/' or '' (just a slash, or an empty string -- can't remember which one).
-----Original Message-----
From: "Concrete5 Community" <[email protected]>
Sent: Wednesday, November 2, 2011 1:21pm
To: [email protected]
Subject: Conditional statements in templates and element files: Conditional statements in templates and element files
if($c->getCollectionID() == HOME_CID) { //homepage //code if its the homepage } else { //other page //code }
Is "HOME_CID" supposed to be... something else?
Every site always has a home page, and that home page's id is always 1. HOME_CID is also 1. So that code is saying "if this is the home page, do something, otherwise if it's any other page do something else".
So that code will be useful to you if you have something that you want on the home page but you don't want on any other page.
But it sounds like your conditional situation is much more complicated than that -- sounds like you have lots of different pages all with lots of different requirements, not just the home page.
As mentioned in my response above, I think that using different page type templates for different styles is the best way to go.
ALSO... another example I have here is to hide the default logo while in edit mode, and replace it with a custom-added block. If no block is added, it shows the default logo:
So... from what I can tell, a basic conditional structure might look like this (but, again, I'm not 100% sure this is corrrect):
I hope this is a good starting point.