Peter Knight Posted November 22, 2017 Posted November 22, 2017 I have the following code which creates a 2 column div. In the first I check to see if there are any images present and only echo an image if there is. If I don't do this, I get an error. But I was wondering if there was a more efficient way to do this? I don't know the proper terminology but I suspect breaking the code into three parts as I have done ... First - the first <div class='uk-width-1-2@s'> Second - the image check Third - the final div ...is somewhat more verbose than it needs to be with more modern PHP <?php $imgoptions = array('quality' => 100,'upscaling' => false); $products = $page->siblings("id!=$page"); foreach ($products as $prod){ echo" <div class='uk-width-1-2@s'> "; if(count($prod->images)){ echo" <a href='$prod->url'> <img class='lazyload prod-thumb-ov' src='{$prod->images->first()->height(300, $imgoptions)->url}' data-src='{$prod->images->first()->height(300, $imgoptions)->url}' data-srcset=' {$prod->images->first()->width(414)->url} 414w, {$prod->images->first()->width(320)->url} 320w' data-sizes='auto' alt='{$prod->images->first()->description}'> </a> ";} echo " </div> <div class='uk-width-1-2@s'> {$prod->title} </div> ";} ?> Thanks
incognito.ms Posted November 22, 2017 Posted November 22, 2017 I think it's a matter of personal taste... Alternative syntax would be something like this: <?php $imgoptions = [ 'quality' => 100, 'upscaling' => false, ]; $products = $page->siblings("id!=$page"); ?> <?php foreach ($products as $prod): ?> <div class='uk-width-1-2@s'> <?php if (count($prod->images)): ?> <a href="<?= $prod->url; ?>"> [... and so on ...] </a> <?php endif;?> </div> <div class='uk-width-1-2@s'> <?= $prod->title; ?> </div> <?php endforeach;?> Furthermore, I normally separate the controller logic (upper part between <?php ... ?> from the view part (lower part) in different two files. In this case, the controller part is very simple, though. 2
maxf5 Posted November 23, 2017 Posted November 23, 2017 I normally would do the following. So you don't have to echo everything in strings. <?php $imgoptions = [ 'quality' => 100, 'upscaling' => false, ]; $products = $page->siblings("id!=$page"); ?> <?php foreach ($products as $prod) : ?> <div class='uk-width-1-2@s'> <?php if (count($prod->images)) : ?> <?php $fatboy = $prod->url; $thumb = $prod->images->first()->height(300, $imgoptions)->url; $w414 = $prod->images->first()->width(414)->url; $w320 = $prod->images->first()->width(320)->url; $imgdesc = $prod->images->first()->description; ?> <a href='<?= $fatboy; ?>'> <img class='lazyload prod-thumb-ov' src='<?= $thumb ?>' data-src='<?= $thumb ?>' data-srcset='<?= $w414 ?> 414w,<?= $w320 ?> 320w' data-sizes='auto' alt='<?= $imgdesc ?>'> </a> <?php endif; ?> </div> <div class='uk-width-1-2@s'> <?= $prod->title ?> </div> <?php endforeach; ?> Quote Furthermore, I normally separate the controller logic (upper part between <?php ... ?> from the view part (lower part) in different two files. In this case, the controller part is very simple, though. Seperating the logic from the view is always good. but this doesn't count as spaghetti code so far
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now