views:

230

answers:

2

A HTML file is generated by a PHP file. Generally speaking, what a user can see from a HTML file can be divided into two parts, one part is static; another part is dynamic, this part is represented by variables in the PHP file.

For example:

$html=<<<eod
$title<br/>
Total Price:$row[column1]
Quota:$row[column2]
<pre>$row[column3]</pre>
Balance:<label class="price">$row[column4]</label>
Current Unit_price:<span class="price">$row[column5]</span>
$row[column6] readers are expected. Announcer:$row[column6]
<hr>
eod;
echo $html;

Total Price: Quota: Balance: Current Unit_price: readers are expected.Announcers:

is one part, it is static;

$row[column1]$row[column2]$row[column3]$row[column4]$row[column5]$row[column6]

is another part, the text is generated by variables, and the content of the text is dynamic. I knew that I can do it by wrapping them with <span>, <div>, <label>, etc. But is there a better way to do it without any markers? How to differentiate both when displaying them? How to control the color or font of two parts respectively? Is there an elegant way to do it?

+3  A: 

You control it with CSS. Wrap your dynamic content in a span or div with a specific class and then style that however you like. Simple example:

<table>
<thead>
<tr>
  <th>Item</tr>
  <th>Description</th>
  <th>Price</th>
</tr>
</thead>
<tbody>
<tr>
  <td><!-- dynamic content --></td>
  <td><!-- dynamic content --></td>
  <td><!-- dynamic content --></td>
</tr>
<tr>
  <td><!-- dynamic content --></td>
  <td><!-- dynamic content --></td>
  <td><!-- dynamic content --></td>
</tr>
</tbody>
</table>

Here you don't even need another class:

thead th { background: #CCC; }
tbody td { background: yellow; }

Here the <tbody> contains all the dynamic content so it's easy to segregate. Another example:

<p>The total price is <span class="price dynamic">$19.95</span>.</p>

with:

span.dynamic { color: red; }

and so on.

cletus
It is a way to control it with a span or div. Is there a better way to do it without <span> or <div>?
Steven
The only way you're going to be styling a Webpage (being HTML) is with CSS. There are multiple ways to apply CSS but you will need some kind of demarcation. You can't change the formatting of a section of text without some kind of tag wrapping that section.
cletus
You should define "better". Do you mean shorter? Cleaner?
MitMaro
Cleaner, more readable and more friendly.
Steven
And more natural.
Steven
A: 

If you are trying to avoid having to place html directly in your PHP you could look into a template engine. Some examples are Dwoo and Smarty.

It is also possible to separate your code into separate files and use PHP directly as a template engine.

MitMaro