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:

<div class="banner-wrapper  <?php if ($current_page=='index') { ?>full<?php } ?>">


and in one of my templates, at top of page:

<?php
    //Set values for page
    $current_page = "index";
?>
<?php 
defined('C5_EXECUTE') or die("Access Denied.");
$this->inc('elements/header.php'); ?>
//HTML


Thanks for any help.

stephmars
View Replies: View Best Answer
PineCreativeLabs replied on at Permalink Reply
PineCreativeLabs
I'm not 100% sure if there is (perhaps someone else can answer this better than me). However, I know there is a way to have a script disabled if you're in edit mode (which is conditional). Below is a code example for disabling a script if in edit mode:

<?php if(!$c->isEditMode()) { ?>
<script type="text/javascript" src="<?php  echo $this->getThemePath()?>/js/jquery.masonry.min.js"></script>
<?php } ?>


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:
<?php $logo = new Area('Logo');
         if ($logo->getTotalBlocksInArea($c) == 0 && !$c->isEditMode()) {   
         ?>
<h1 class="logo">
              <a href="<?php  echo DIR_REL?>/" title="<?php  echo SITE?>"><?php  
            $block = Block::getByName('My_Site_Name');  
            if( $block && $block->bID ) $block->display();   
            else echo SITE;
          ?></a>
           </h1>
      <?php 
         } else {
            $logo->display($c);
         }
         ?>


So... from what I can tell, a basic conditional structure might look like this (but, again, I'm not 100% sure this is corrrect):
<?php if(!$c->isEditMode()) { ?><?php } ?>


I hope this is a good starting point.
stephmars replied on at Permalink Reply
stephmars
Thanks for this. I don't think I know enough about PHP for this to help me, though.
12345j replied on at Permalink Reply
12345j
you shouldn't have to do anything special, but try defining $current_page in your header.php file
stephmars replied on at Permalink Reply
stephmars
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. :/
12345j replied on at Permalink Reply
12345j
sortof a roundabout way- but in the header.php file do this
$typeHandle = $c->getCollectionTypeHandle();
if($typeHandle=='full'){
//do some stuff!
}
stephmars replied on at Permalink Reply
stephmars
Thanks. I don't know how to use this. Is there some documentation you could point me toward?
12345j replied on at Permalink Reply
12345j
its in developer-ese, buthttp://www.concrete5.org/documentation/developers/pages/page-types.... What don't you get about the code I gave you?
jordanlev replied on at Permalink Reply
jordanlev
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.
stephmars replied on at Permalink Reply
stephmars
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?
jordanlev replied on at Permalink Best Answer Reply
jordanlev
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.
stephmars replied on at Permalink Reply
stephmars
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.
jordanlev replied on at Permalink Reply
jordanlev
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:
<?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.
stephmars replied on at Permalink Reply
stephmars
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.
jordanlev replied on at Permalink Reply
jordanlev

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
stephmars replied on at Permalink Reply
stephmars
I found this thread (http://www.concrete5.org/community/forums/customizing_c5/check-whic... ) that had this code snippet:

if($c->getCollectionID() == HOME_CID) { //homepage
//code if its the homepage
} else { //other page
//code
}


Is "HOME_CID" supposed to be... something else?
jordanlev replied on at Permalink Reply
jordanlev
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.