使用PHP显示HTML内容的最佳方法是什么?

使用PHP显示HTML内容的最佳方法是什么?

问题描述:

I have a php function as shown below, which takes in an array that has values from an SQL query (in another, not shown function). In this case, the SQL query returns a set of filenames and their associated picture names.

// Take in an array of filters;
// each element is a dictionary of column name 
// to the value of that row from SQL query
function displayFilters($filters) {
    foreach($filters as $filter){
        //output images in img tags, with their appropriate classes
        echo "  <div class='w3-quarter'>
                    <div class='w3-card-2'>
                        <img src='images/".$filter['File_Name']."' style='width:100%'>
                            <div class='w3-container'>
                                <h4>".$filter['Filter_Name']."</h4>
                            </div>
                    </div>
                </div>";
    }

} 

Now, this works fine and will properly display the images as I want. However, I keep having to modify this code, change classes, properties and what not, and if I need to add/modify to the div, I have to go into this function to change everything. Is there a better way to do this than going into the echo function to change it? I'd rather not have to use Javascript if possible, but if that is the only clean way to do it, can someone point me to a way to do this?

我有一个如下所示的php函数,它接受一个包含SQL查询值的数组(在另一个中) ,未显示功能)。 在这种情况下,SQL查询返回一组文件名及其关联的图片名称。 p>

  //接受一个过滤器数组; 
 //每个元素都是一个字典 列名称
 //来自SQL查询
function displayFilters($ filters){
 foreach($ filters as $ filter){
 //输出img标签中的图像及其相应的类\  n echo“&lt; div class ='w3-quarter'&gt; 
&lt; div class ='w3-card-2'&gt; 
&lt; img src ='images /".$ filter ['File_Name']  msgstr“”'style ='width:100%'&gt; 
&lt; div class ='w3-container'&gt; 
&lt; h4&gt;“。$ filter ['Filter_Name']。”&lt; / h4&gt; \  n&lt; / div&gt; 
&lt; / div&gt; 
&lt; / div&gt;“; 
} 
 
} 
  pre> 
 
 

现在,这个 工作正常,将正确显示我想要的图像。 但是,我不得不修改这段代码,更改类,属性和什么不是,如果我需要添加/修改div,我必须进入这个函数来改变一切。 有没有比进入echo函数更改它更好的方法呢? 如果可能的话,我宁愿不必使用Javascript,但如果这是唯一干净的方法,有人可以指点我这样做吗? p> div>

Use a PHP Template Engine


http://www.smarty.net/

Smarty is fast and lean with a small memory footprint.


http://twig.sensiolabs.org/ (Looks remarkably like Dwoo)

Twig is a modern template engine for PHP (Fast, Secure, Flexible)


http://dwoo.org/ (Inspired by Smarty)

Dwoo is a templating system used to make creating websites easier and more structured.


http://platesphp.com/ (Inspired by Twig)

Plates is a native PHP template system that’s fast, easy to use and easy to extend.


http://phptal.org/

PHPTAL is a PHP implementation of ZPT work. To be short, PHPTAL is a XML/XHTML template library for PHP.

I have written how to retrieve query and how to display in below code. You can use this one. you can also use image tag within table <tr> tag.

  $con=mysqli_connect('localhost','username','password','databasename');
  $sql="SELECT  `COL 1` ,  `COL 2` ,  `COL 3` ,  `COL 4` ,  `COL 5` ,  `COL 12` FROM  `TABLE` WHERE 1 ORDER BY  `COL 3`  ;";
  $result=mysqli_query($con,$sql);


<table style="width:100%"  >
 <tr>
 <th>Col1</th>
 <th>Clo2</th> 
 <th>Col3</th>
  </tr>

<?
while ($row=mysqli_fetch_row($result))
{

      echo "<tr><td>".$row[0]."</td><td>".$row[1]."\t".$row[2]."\t".$row[3]."\t".$row[4]."</td><td>".$row[5]."</td></tr>";  
 }

?>

     </table>

    <?php    function displayFilters($filters) {
            foreach($filters as $filter){ ?>
                //output images in img tags, with their appropriate classes
                <div class='w3-quarter'>
                            <div class='w3-card-2'>
                                <img src='images/<?=$filter['File_Name']>' style='width:100%'>
                                    <div class='w3-container'>
                                        <h4><?=$filter['Filter_Name']></h4>
                                    </div>
                            </div>
                        </div>
<?php
            }

        } ?>

I do it this way and it seems easy than learning a new template engine