doubo6658 2016-01-05 08:43
浏览 67
已采纳

使用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?

  • 写回答

3条回答 默认 最新

  • doupeizheng3918 2016-01-05 09:20
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥17 pro*C预编译“闪回查询”报错SCN不能识别
  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?