Page_list custom template - how to add if/else statement to test for page attributes

Permalink
Hi There,

We're using a custom Page-list template and would like to add in a conditional to test for some custom page attributes.

If the page contains the following attributes:
- cooking_time
- preparation_time

Then we'd like to output the following:
<!-- entry prep and cooking time -->
   <ul class="v-list bullets time">
      <li>Preparation: <?php echo $prep ?>mins</li>
      <li>Cooking: <?php echo $cook ?>mins</li>
   </ul>


And if those attributes are not present, we'd like to output nothing - is this possible?

We can see the basis for the if/else statements but don't know the syntax for testing if an attribute has been entered. At the start of the template we're loading the custom attributes and they get output when we echo them on pages that have the attribute:
// load page attribute 'preparation_time'
      $prep = $cobj->getCollectionAttributeValue("preparation_time");
      // load page attribute 'preparation_time'
      $cook = $cobj->getCollectionAttributeValue("cooking_time");


Any help would be much appreciated.

Cheers

Ben

 
jelthure replied on at Permalink Reply
jelthure
in controller:
$c = Page::getCurrentPage();
if($c instanceof Page) {
$this->set('cook', $c->getAttribute('cooking_time'));
$this->set('prep', $c->getAttribute('preparation_time'));
}


then in view:
<ul class="v-list bullets time">
<?php if($prep){ ?><li>Preparation: <?php echo $prep ?>mins</li><?php }?>
<?php if($cook){ ?><li>Cooking: <?php echo $cook ?>mins</li><?php }?>
   </ul>


should do the trick.
ijessup replied on at Permalink Reply
ijessup
I'd probably do something like this:
<?php
// List out all the attributes you want to use.
$attributes['Preparation']   = $cobj->getAttribute('prep_time');
$attributes['Cooking']       = $cobj->getAttribute('cook_time');
?>
<ul>
<!-- Encapsulate all previously defined attributes in an <li> element -->
<?php foreach($attributes as $name => $attribute) {
   if(!empty($attribute)) { 
      print '<li>'.$name.': '.$attribute.'</li>'; 
   }
}?>
</ul>
jelthure replied on at Permalink Reply
jelthure
Same theory, different approach.
In controller:
$aks = array('cooking_time','preparation_time');
$c = Page::getCurrentPage();
if($c instanceof Page) {
  foreach($aks as $a){
      $this->set( $a, $c->getAttribute($a) );
  }
}

In view:
<ul class="v-list bullets time">
<?php if($preparation_time){ ?><li>Preparation: <?php echo $preparation_time ?>mins</li><?php }?>
<?php if($cooking_time){ ?><li>Cooking: <?php echo $cooking_time ?>mins</li><?php }?>
</ul>
cmscss replied on at Permalink Reply
Thanks guys - much appreciated!
cmscss replied on at Permalink Reply
What about if you're trying to do the same thing on a page rather than a page list template?

Obviously I don't know any php but decided to give things a go (didn't work!)

My theory was to:
- Defined a variable called "image"
- Test to see if the variable was not empty
- If it wasn't empty, echo the <img> tag with the variable in it.

<?php
   $image = (
      $c->getAttribute('main_image')->getVersion()->getRelativePath()
   );
   if ($image != "") { 
      echo "<img src=" .$image " width="490" height="300">";
   }
?>


But I get a "Parse error: syntax error, unexpected T_CONSTANT_ENCAPSED_STRING, expecting ',' or ';' in /path/to/themes/theme-name/right_sidebar.php on line 38 "

Line 38 is the echo statement.

Anyway, a bit over my head at this stage!

Any pointers in the right direction would be much appreciated.

Cheers

Ben
jelthure replied on at Permalink Best Answer Reply
jelthure
close:
<?php
   $image = $c->getAttribute('main_image')->getVersion()->getRelativePath();
   if ($image) { 
      echo "<img src=\"".$image."\" width=\"490\" height=\"300\">";
   }
?>

or
<?php if ($c->getAttribute('main_image')) { ?>
<img src="<?php echo $c->getAttribute('main_image')->getVersion()->getRelativePath();?>" width="490" height="300">
<?php } ?>

will work.
cmscss replied on at Permalink Reply
Mate, thanks heaps.

So that second example means that the html elements don't have to be escaped?

I also added an else statement to remove the markup which seems to work - but would it be considered acceptable PHP?

<?php       
   if ($c->getAttribute('main_image')) { ?>
      <div class="unit size1of2">
         <h2><?php  echo $c->getCollectionName(); ?></h2>
         <?php 
            $a = new Area('Intro');
            $a->display($c);
         ?>
      </div>
      <div class="unit size1of2 last">
         <img src="<?php echo $c->getAttribute('main_image')->getVersion()->getRelativePath();?>" width="490" height="300">
      </div>
   <?php } 
   else { ?>
      <div class="unit size1of1">
jelthure replied on at Permalink Reply
jelthure
ok i see what you are doing, I would do something like this:
<?php $css = 1;
      if ($c->getAttribute('main_image')) { 
        $css = 2;
      } 
?> 
<div class="unit size1of<?php echo $css;?>">
<h2><?php  echo $c->getCollectionName(); ?></h2> 
  <?php $a = new Area('Intro'); $a->display($c); ?>
</div>
<?php if($css == 2){?>
      <div class="unit size1of<?php echo $css;?> last">
         <img src="<?php echo $c->getAttribute('main_image')->getVersion()->getRelativePath(); ?>" width="490" height="300">
      </div>
<?php } ?>


but yeah your version would work too :)