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条)

报告相同问题?

悬赏问题

  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥15 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败