Sorry for a probably very basic question but I’m not a native PHP programmer.
I have this code in my template:
<ul class="overview">
<?php
foreach($posts as $post):
if($post->slug() !== $featured->slug()):
$hasPostImage = $post->post_image()->toFile();
$color_class = null;
if(!$hasPostImage):
$post_color = $post->post_color();
$post_color = $post_color->value();
switch($post_color) {
case "#8F3D97":
$color_class = 'purple';
break;
case "#3E51A3":
$color_class = 'blue';
break;
…
default:
$color_class = 'red';
break;
}
endif;
?>
<li class="<?php e(!$hasPostImage,' noimg '.$color_class); ?>">
<a href="<?= $post->url(); ?>">
<?php e($hasPostImage,$hasPostImage); ?>
<span><?= $post->title()->html(); ?></span>
</a>
</li>
<?php
endif;
endforeach;
?>
</ul>
I have a post_color field in the blueprint where various HEX values can be selected, to convert them to class names in the generated HTML, so that the elements can be styled accordingly in the stylesheet.
How would I outsource this switch statement and the whole post color logic to a controller? The controller example in the documentation is pretty basic and doesn’t help me very much.
Any help is greatly appreciated