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:
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:
Any help would be much appreciated.
Cheers
Ben
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:
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
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>
Same theory, different approach.
In controller:
In view:
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:
Thanks guys - much appreciated!
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.
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
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
close:
or
will work.
<?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.
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?
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">
Viewing 15 lines of 23 lines. View entire code block.
ok i see what you are doing, I would do something like this:
but yeah your version would work too :)
<?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 :)
then in view:
should do the trick.