Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.php > #17379

Re: Is this just a question of personal prefs - echo or not?

From Thomas 'PointedEars' Lahn <PointedEars@web.de>
Newsgroups comp.lang.php
Subject Re: Is this just a question of personal prefs - echo or not?
Date 2017-04-26 03:10 +0200
Organization PointedEars Software (PES)
Message-ID <1532509.nxgZTucpeQ@PointedEars.de> (permalink)
References <odnc5b$c2c$1@dont-email.me> <8737cwzkpi.fsf@bsb.me.uk>

Show all headers | View raw


Ben Bacarisse wrote:

> TimW <tim@mysurname.me.uk> writes:
>> I am making a theme for the cms I am using. There are two page templates
>> and I want to be able to style elements differently according to which
>> template is in use, so hopefully this line is fairly self explanatory:
>>
>> <body <?php if ($template_file != 'homepage.php')
>>    { echo "class =\"subpage\"";} ?> >
>>
>> And it works, fine, but I don't think it would normally be done that way.
>> Is this better?
>>
>> <body <?php if ($template_file != 'homepage.php')
>>    {?> class ="subpage"  <?php } ?> >
>>
>> or just different? Is one more acceptable or normal or easier to read?

Usually one would use PHP template syntax in templates:

  <body
    <?php if ($template_file !== 'homepage.php'): ?>
      class="subpage"
    <?php endif; ?>
  >

However, it produces more whitespace.  You can work around that:

  <body<?php
    if ($template_file !== 'homepage.php'):
      ?> class="subpage"<?php
    endif;
  ?>>

but whether you find that better readable is a matter of preference.
It is also a matter of available syntax highlighting.

You can minify markup output using output buffering and regular expressions, 
so that you can write templates easily understandable without output 
overhead.  A template engine should allow that.
 
> I find both rather fussy.  Here's yet another way:
> 
>   <body<?= $template_file != 'homepage.php' ? ' class=subpage' : '' ?>>

Do not rely on type conversion:

  <body<?= $template_file !== 'homepage.php' ? ' class=subpage' : '' ?>>

But really, if there is only one case where to output something, then a 
simple “if” is easier to read.

OTOH, it might be better readable and easier maintainable to make the case 
explicit:

  <?php if ($template_file === 'homepage.php'): ?>
    <body>
  <?php else: ?>
    <body class="subpage">
  <?php endif; ?>

This also indicates that where one has two cases it usually results in more 
readable code to use a true-condition instead of a false-condition.

The hardcoded “homepage.php” string does not belong in the template of 
course, but a boolean template variable should be queried instead.
 
-- 
PointedEars
Zend Certified PHP Engineer <http://www.zend.com/en/yellow-pages/ZEND024953>
<https://github.com/PointedEars> | <http://PointedEars.de/wsvn>
Twitter: @PointedEars2 | Please do not cc me./Bitte keine Kopien per E-Mail.

Back to comp.lang.php | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Is this just a question of personal prefs - echo or not? TimW <tim@mysurname.me.uk> - 2017-04-25 12:36 +0100
  Re: Is this just a question of personal prefs - echo or not? Tim Streater <timstreater@greenbee.net> - 2017-04-25 13:10 +0100
    Re: Is this just a question of personal prefs - echo or not? "Christoph M. Becker" <cmbecker69@arcor.de> - 2017-04-25 15:00 +0200
  Re: Is this just a question of personal prefs - echo or not? Jerry Stuckle <jstucklex@attglobal.net> - 2017-04-25 08:19 -0400
    Re: Is this just a question of personal prefs - echo or not? Richard Yates <richard@yatesguitar.com> - 2017-04-25 21:06 -0700
      Re: Is this just a question of personal prefs - echo or not? Jerry Stuckle <jstucklex@attglobal.net> - 2017-04-26 07:55 -0400
        Re: Is this just a question of personal prefs - echo or not? Richard Yates <richard@yatesguitar.com> - 2017-04-26 09:15 -0700
          Re: Is this just a question of personal prefs - echo or not? Jerry Stuckle <jstucklex@attglobal.net> - 2017-04-26 12:48 -0400
      Re: Is this just a question of personal prefs - echo or not? Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2017-04-26 20:40 +0200
  Re: Is this just a question of personal prefs - echo or not? "Christoph M. Becker" <cmbecker69@arcor.de> - 2017-04-25 15:03 +0200
    Re: Is this just a question of personal prefs - echo or not? Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2017-04-26 03:14 +0200
  Re: Is this just a question of personal prefs - echo or not? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-04-25 14:21 +0100
    Re: Is this just a question of personal prefs - echo or not? Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2017-04-26 03:10 +0200
    Re: Is this just a question of personal prefs - echo or not? TimW <tim@mysurname.me.uk> - 2017-04-26 19:31 +0100
      Re: Is this just a question of personal prefs - echo or not? Tim Streater <timstreater@greenbee.net> - 2017-04-26 20:21 +0100

csiph-web