Applying class name from select field

Hey all!

I put a selct field in my blueprint, which is supposed to control a class in my template. This is my YAML for that fild

position:
    label: Bildposition
    type: select
options:
    left: Bild links - Text rechts
    right: Bild rechts - Text links

in my template i tried this

<?php foreach(page()->children()->visible() as $item): ?>
				...
      <?php if($item->position() ='left' ): ?>
		<div class="left_50" >
      <?php else:  ?>
		<div class="right_50" >
	<?php endif ?>
				...
<?php endforeach ?>

But somehow this won’t work. Somebody can help me to point out the mistake?

Your if-statement syntax is not correct:

<?php if($item->position() == 'left' ): ?>

You could also shorten all this to:

<div class="<?php e($item->position() == 'left', "left_50", "right_50") ?>">

Are there any docs of the e function? Seems quite handy.

1 Like

+1
I’ve only found this in the helpers.php:

/**
 * Smart version of echo with an if condition as first argument
 *
 * @param boolean $condition
 * @param string $value The string to be echoed if the condition is true
 * @param string $alternative An alternative string which should be echoed when the condition is false
 */
function e($condition, $value, $alternative = null) {
  echo r($condition, $value, $alternative);
1 Like

http://getkirby.com/docs/cheatsheet/helpers/e

2 Likes

works <3. so close…

the e stuff looks very nice & compact. I have to do some reading on that. looks cool