开发者

How to display static text and dynamic text(generated by variables) differently in PHP?

开发者 https://www.devze.com 2022-12-12 20:48 出处:网络
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, on开发者_StackOverflow中文版e part is static; another part is dynamic, t

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, on开发者_StackOverflow中文版e 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?


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消