I have the following code, which displays the content for my custom page template "landingpage.php" and for single posts. The site uses Bootstrap 4.
<?php if(is_page_template( 'landingpage.php' )): ?>
<div id="content" class="site-content">
<div class="container-fluid">
<div class="row">
<?php elseif (is_single()): ?>
<div id="content" class="site-content pt-4">
<div class="container-fluid">
<div class="row">
<?php else: ?>
<div id="content" class="site-content">
<div class="container">
<div class="row">
<?php endif; ?>
Is there a more elegant and/or shorter way to achieve this?
1 Answer 1
The third <div>
appears to be the same in all three cases, so that can be abstracted out of the cases.
The outermost <div>
appears to have class pt-4
applied only if is_page_template( 'landingpage.php' )
returns a value that evaluates to FALSE
and is_single()
returns a value that evaluates to TRUE
.
And the first nested (i.e. second) <div>
has class container-fluid
if either of those functions return a value that evaluates to TRUE
.
This logic could be rewritten as below. I know it strays away from the familiar wordpress style of using the Alternative syntax for control structures and uses the shortcut syntax (i.e. <?=
) of echo
but it does add some separation of the logic from the markup
<?php
// default class names
$contentClass = 'site-content';
$containerClass = 'container-fluid';
if (!is_page_template( 'landingpage.php' )) {
if (is_single()) {
$contentClass .= ' pt-4';
}
else {
$containerClass = 'container';
}
}
?>
<div id="content" class="<?= $contentClass; ?>">
<div class="<?= $containerClass; ?>">
<div class="row">
It could be simplified using ternary operators but some may argue that would detract from readability.
Is this shorter?
yes, only by two lines
is this more elegant?
Well, that is subjective. The code above should be simpler to read. Consider the scenario where the code is updated by multiple people, including programmers and designers. The programmers can have designers update the markup without affecting the business logic. For more on this topic, refer to Mixing PHP and HTML.
-
1\$\begingroup\$ In php,
if
,elseif
, andelse
are language constructs. I assume languages like javascript do not haveelseif
which would explain why they require a space to formelse if
. When writing php, please suggest the use of the single language constructelseif
, versus using two language constructselse
(an else branch with no curly bracing) followed immediately by a newif
branch. \$\endgroup\$mickmackusa– mickmackusa2020年07月25日 08:11:36 +00:00Commented Jul 25, 2020 at 8:11 -
\$\begingroup\$ Updated- you are correct about JS not having
elseif
- and there are interesting comments on this SO answer \$\endgroup\$2020年07月25日 12:14:27 +00:00Commented Jul 25, 2020 at 12:14 -
\$\begingroup\$ Maybe I should have added these links as well to help explain to researchers... php.net/manual/en/control-structures.elseif.php and amitmerchant.com/difference-between-elseif-and-else-if-php \$\endgroup\$mickmackusa– mickmackusa2020年07月25日 22:29:22 +00:00Commented Jul 25, 2020 at 22:29
-
\$\begingroup\$ Sorry for the late response. Thank you so much for the improvement, that's exactly what i was looking for. \$\endgroup\$Sniffles– Sniffles2020年08月03日 15:24:14 +00:00Commented Aug 3, 2020 at 15:24