Re: Couple of beginner questions

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Ashley Sheridan wrote:
On Sun, 2009-01-11 at 10:16 -0500, tedd wrote:
At 3:02 PM +0000 1/11/09, Ashley Sheridan wrote:
On Sun, 2009-01-11 at 09:46 -0500, tedd wrote:
 At 1:49 PM +0000 1/11/09, Ashley Sheridan wrote:
 > >
 >Unless it's something like this:
 >
 ><?php
 >echo "<h1 class=\"$headerClass\">$whatever</h1>";
 > >?>
 >
 Here's the alterative I would use:

 > <h1 class="<php echo($headerClass);?>"><php echo($whatever);?></h1>

I'm not wanting ti nitpick, but what if there were several attributes
you needed to populate from the PHP? I just find it easier on my eyes to
include HTML in the PHP string that's being output, but I can see the
definite merits for echoing out single parts that would only get in the
way of the HTML.
Ash:

Nitpick as much as you want -- we all have our level of accommodation.

To me, there usually is a clear demarcation between the different languages, their scope, and best application. If I had to deal with several attributes, such as shown here:

<h1 class="red header">Hello World</h1>

I would write it up like so:

$header_class = "red header";
$whatever = "Hello World";

<h1 class="<php echo($header_class);?>"><php echo($whatever);?></h1>

In my css, I would have

.red
    {
    color: #ff0000;
    }

.header
    {
    font-size: 1.2em;
    }

That would solve the problem of several attributes and keep php, html, and css separate.

So handling numerous attributes would not be a problem for me.

Cheers,

tedd

--
-------
http://sperling.com  http://ancientstones.com  http://earthstones.com

I was thinking more along the lines of this:

echo "<img src=\"$url\" alt=\"$alt\" title=\"$alt\" class=\"$imgclass
\"/>";


which looks like this otherwise:

<img src="<?php echo($url);?>" alt="<?php echo($alt);?>" title="<?php
echo($alt);?>" class="<?php echo($imgclass);?>"/>




Ash
www.ashleysheridan.co.uk


i love these discussions on pedantics and semantics!

personally (when I need to) I always go for a bit of concatenation so in the example above:

// somewhere in the business logic / functional layer
$imgHTML = '<img src="' . $url . '" alt="' . $alt . '" title="' . $alt . '" class="' . $imgclass . '" />';

.....
// somewhere in the display layer
echo $imgHTML;

however; I only really do this when developing or in a quick bit of script; whenever possible I'll always use a templating engine, or xml/xsl which is the perfect abstraction between data and presentation IMHO.

another little note is that I'd never echo out a class or presentation data; infact I'd never use a class on a tag such as img either, preference going to a bit of css using selectors

div#article_content p img {
/* whatever */
}

I'm not sure what's brought me to these conclusions, I've certainly been through all the other methods of doing it - however for a couple of years now I've limitted all presentation to css only, css contained in a stylesheet - I try to use minimal css classes, and stick to using an id wherever I can't simply redefine the html tag.

TBH i think even if I'm doing a complete site myself, I still like to wear different caps (developer, designer, etc) and as such keep the layers as seperate as possible, as if it was somebody completely different working on the design.

the above means that moving back to the original <h1> example(s) I'd simply

<h1>whatever</h1>

css:
h1 {
  color: rgb(255,0,0);
  font-size: 1.2em;
}

seeing as you can only have one h1 tag on a single document.

if you can take any points from this ramble of mine, it'd be that IMHO the output from a script should at most comprise of something like:
<?php
// business logic
$display_engine->output( $template , $variables );
?>
infact ideally an html tag should never be seen in a php script

... and to take it further a echo'd string probably shouldn't either! so technically this is bad:

<?php
if( whatever() ) {
// whatever
} else {
echo 'turns out you entered some invalid data client';
}
?>

while this is good
<?php
if( whatever() ) {
// whatever
} else {
throw new UnexpectedValueException( WHATEVER_ERROR_CODE );
}
?>

which is caught higher up, the localized error message for WHATEVER_ERROR_CODE is then loaded, fired through to the display engine which formats and displays it to the client.

can seem like overkill, but if you want to have multiple front ends to you're app (say a soap interface, a flash ui and an html ui) there's no other way.

joy

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux