opinions on inline server-side code?

J

John Salerno

After doing a little reading (I'm trying to figure out how to in-line
Python code in my HTML), it seems like a lot of purists think that
inline code is a bad thing (sort of like inline style). I was wondering
what you guys think of that, since you are obviously very strict about
things like style, HTML vs XHTML, etc. (although, I've never heard any
criticism in this group of inline PHP, for example, such as when I asked
about it a while back).

Thanks.
 
T

Toby Inkster

John said:
After doing a little reading (I'm trying to figure out how to in-line
Python code in my HTML), it seems like a lot of purists think that
inline code is a bad thing (sort of like inline style). I was wondering
what you guys think of that, since you are obviously very strict about
things like style, HTML vs XHTML, etc. (although, I've never heard any
criticism in this group of inline PHP, for example, such as when I asked
about it a while back).

PHP (and ASP and others too) is clearly designed to be scattered in little
droplets throughout an HTML file:

<? require_once "myfunctions.php"; ?>
<h1><? print get_title_from_db(); ?></h1>
<div id="content">
<? print get_content_from_db(); ?>
</div>
<!-- and so on -->

And when you just need to add little splashes of dynamicism here and
there, that works fairly well.

However, for larger projects, you'll probably find it easier to do things
the opposite way around:

<?
require_once "myfunctions.php";
$t = get_title_from_db();
$c = get_content_from_db();
printf("<h1>%s</h1>\n", htmlspecialchars($t));
printf("<div id=\"content\">%s</div>\n",
htmlspecialchars($c));
print "<!-- and so on -->\n";
?>

only "dropping out of PHP mode" occasionally, when you have large chunks
of HTML to output (that hasn't come from a database, or been dynamically
generated in any other way).

For my current blog project, I've got about 6000 lines of code so far, and
all of them are PHP, as per the second example. (No plain HTML yet, and
none planned!)
 
J

John Salerno

Toby said:
For my current blog project, I've got about 6000 lines of code so far, and
all of them are PHP, as per the second example. (No plain HTML yet, and
none planned!)

You frighten me! ;)
 
C

cwdjrxyz

John said:
After doing a little reading (I'm trying to figure out how to in-line
Python code in my HTML), it seems like a lot of purists think that
inline code is a bad thing (sort of like inline style). I was wondering
what you guys think of that, since you are obviously very strict about
things like style, HTML vs XHTML, etc. (although, I've never heard any
criticism in this group of inline PHP, for example, such as when I asked
about it a while back).

First, inline style is not a bad thing per se, and is completely valid
according to W3C standards up to and including xhtml 1.1. One can use
an external style sheet, a style sheet in the head of the page, or a
style many places in the body as most convenient. If you use most of
the same style elements on many pages, then an external style sheet is
the way to go. If you are using most style elements only on a single
page, then a style sheet in the head is the way to go. Or the head
stylesheet can be used to override a few elements in an external style
sheet that are different for the page. This is cascading. Likewise, the
same considerations apply to a style in the body of the page at perhaps
a division or paragraph level. If, for example, you are going to use a
style only in a single paragraph or division for a single page, I find
it pointless to write the style for it in a head style sheet and then
also have to reference this at the paragraph or division where it
applies. If you for example use javascript to detect something such as
screen width after the page is downloaded, and then use this detected
width to write a style based on it using javascript, then working at
the division or paragraph method often is by far the most simple method
to go.

About the same consideration apply for deciding if you want to use an
external script, a big script on the page, or several small scripts
scattered throughout the page. You do what is most convenient.

Again about the same applies for a serverside script such as php. I
have some pages that are mostly php, some with just a bit of php, and
some with many small bits of php. In some cases you will use both php
and javascript, because some things, such as changing a color after the
page has downloaded, need javascript to do it.

In most cases the "grammar" of how you handle your css, javascript, or
php will make little difference in how fast the page loads. If you are
concerned about download time, reducing image and other media bit size
often is far more effective than fussing with code. Practical
considerations are to use a formalism that you can easily modify after
not seeing the page for a long time, and try to use external code or
head code when you can for things that get repeated often so that you
do not have to reinvent the wheel every time you write a new page much
like an old page.
 
R

Richard Sexton

First, inline style is not a bad thing per se, and is completely valid
according to W3C standards up to and including xhtml 1.1. One can use
an external style sheet,

Because nothing beats subjecting dialup users (there's still LOTS) to
a page, semi readable only to have it jump around a second later
to something completely different.

I consider it a good thing if you can read a page that's only
partially loaded. If you can give somebody something in one MTU
that'll take them a few seconds to read what they experience is
for all intents and purposes as goood as instant page loading.
 
A

Andy Dingley

it seems like a lot of purists think that
inline code is a bad thing (sort of like inline style).

No, nothing like it. Inline styles are fine in their place and (in some
cases, such as lots of absolute positioning) certainly better than huge
dynamically generated CSS documents.

Compare ASP (lots of in-line code) and good Java server-side code (no
in-line code, and the tools to make this possible) though. If someone
learns ASP first, they're ruined for Java. Conversely the only way to
get good ASP coders is to teach them JSP and Servlets first.
 
T

Toby Inkster

John said:
You frighten me! ;)

It's only a tiny fraction complete so far. Will probably be about 20K-30K
lines of code when finished. (My line counts include comments, but not
blank lines.)

One of my reasons for using as little HTML as possible in this project is
that it's designed to validate as HTML 4.01, XHTML 1.0 and XHTML 1.1, with
some support for ISO HTML and the current XHTML 2.0 draft. Now *that's*
frightening! (FWIW, it will also play nicely with both PostgreSQL and
MySQL.)
 
J

John Salerno

Toby said:
One of my reasons for using as little HTML as possible in this project is
that it's designed to validate as HTML 4.01, XHTML 1.0 and XHTML 1.1, with
some support for ISO HTML and the current XHTML 2.0 draft. Now *that's*
frightening! (FWIW, it will also play nicely with both PostgreSQL and
MySQL.)

It's comments like this that make me realize how little I know about any
of these subjects! :)

Any chance I could see a snippet of this code, just for kicks? I'm still
not sure how HTML is created dynamically through scripts.
 
T

Toby Inkster

J

John Salerno

J

Jim Higson

John said:
After doing a little reading (I'm trying to figure out how to in-line
Python code in my HTML), it seems like a lot of purists think that
inline code is a bad thing (sort of like inline style). I was wondering
what you guys think of that, since you are obviously very strict about
things like style, HTML vs XHTML, etc. (although, I've never heard any
criticism in this group of inline PHP, for example, such as when I asked
about it a while back).

Personally, I find the "PHP in HTML" and "HTML in PHP" models pretty messy
except for adding little bits of dynamic content to the pages.

PHP in HTML: all the <?php and ?> tags ruin the flow of the HTML and make it
difficult to read. Difficult to use for large projects, where much of the
code doesn't belong in any particular place in the HTML.

HTML printed from PHP: Discourages Indentation on the HTML so it is hard to
see at a glance which echo "<div>"; corresponds to which echo "</div>".

My preferred way is to use PHP for the SQL plus a templating engine for the
HTML (ironic since PHP was originally just a templating engine). My
preferred engine is Smarty:

http://smarty.php.net/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top