Conditional statements in templates and element files
Permalink
Is there a structure I need to follow in order to use conditional statements in files?
For example, header.php:
and in one of my templates, at top of page:
Thanks for any help.
For 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.
Thanks for this. I don't think I know enough about PHP for this to help me, though.
you shouldn't have to do anything special, but try defining $current_page in your header.php file
Thanks, that worked. However, I need to have different settings on different templates, and if the settings are all at the top of header.php, kind of defeats the purpose. :/
sortof a roundabout way- but in the header.php file do this
$typeHandle = $c->getCollectionTypeHandle(); if($typeHandle=='full'){ //do some stuff! }
Thanks. I don't know how to use this. Is there some documentation you could point me toward?
its in developer-ese, buthttp://www.concrete5.org/documentation/developers/pages/page-types.... What don't you get about the code I gave you?
Can you explain exactly what it is you're trying to achieve?
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.
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.
It's a complicated design with many different versions of headers/footers and components within them, and I wanted to know if there is actually a way to use conditional statements in C5.
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?
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?
Based on this thread and the other question you had about the autonav, I'm thinking that you might be going about the site in the wrong way. The point of using a CMS is to have consistent re-usable templates, which means that other than the actual content that differs from page to page, most of the design should be the same.
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.
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.
Hey, thanks for this info. This is what I'll be doing.
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.
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.
You could put the navigation menu code in a separate file, for example called "navigation_menu.php" in side your theme's "elements" directory. Then in each of your templates, you can put:
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:
Note that if this is in a separate file, you'll want to put this line at the top:
And also note that the path always needs to start with a slash but not end in one.
<?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.
Including the navigation_menu.php file worked (thank you!), but I can't seem to get the correct path structure down for the menu select snippet.
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.
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" <discussions@concretecms.com>
Sent: Wednesday, November 2, 2011 1:21pm
To: concrete@jordanlev.com
Subject: Conditional statements in templates and element files: Conditional statements in templates and element files
I found this thread (http://www.concrete5.org/community/forums/customizing_c5/check-whic... ) that had this code snippet:
Is "HOME_CID" supposed to be... something else?
if($c->getCollectionID() == HOME_CID) { //homepage //code if its the homepage } else { //other page //code }
Is "HOME_CID" supposed to be... something else?
HOME_CID is the number 1. HOME_CID is defined as the number 1 deep down in the bowels of the Concrete5 system. CID is short for "Collection ID", which is the database id for the page (due to historical reasons, pages are often called collections in the concrete5 core code -- but they're basically the same thing).
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.
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.