dongshan3806 2011-04-02 01:01 采纳率: 0%
浏览 59
已采纳

需要一些使用PHP / HTML进行模板化的建议

I read this post and I really like this solution to templating but I am unsure of one thing. With this system wouldn't the business logic and presentation logic be in two different php files? This is all well and good as it separates them but what if there is only presentation logic as there are on some pages or what if there is a very low amount of business logic?

It seems weird that the default page the users page to will sometimes be only presentational logic and sometimes only business logic. It seems like there are two obvious solutions to this:

1) Have all default pages have business logic (even if there is none) that link to the presentational logic on a different page. The problem with this is that there are then a lot of "unnecessary" pages. The good is that it is consistent.

2) If there is no business logic for a page then just only include the presentational logic. The problem with this is that it is inconsistent when looking at filenames as to what php page includes the business and presentational logic.

Also, and this may be a little off-topic, but is there any way to template this?

<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <title>My Website</title>
        <meta http-equiv="content-type" content="text/html; charset=UTF-8" />
        <link href="styles.css" rel="stylesheet" type="text/css"/>
        <link href="favicon.png" rel="shortcut icon" />
    </head>
    <body>
    </body>
</html>

Considering I have this on every page I was wondering if there was a way to template this so the code could all be in one file and recalled.

  • 写回答

1条回答 默认 最新

  • dongyi1777 2011-04-02 01:15
    关注

    Your templates should contain strictly presentational logic.

    This means that you can include:

    • Loops (to print lists etc)
    • Conditionals (e.g. only print an element if some value is non-null)
    • Formatting functions (strip spaces, round numbers, etc)
    • A small number of template-composition helper function calls (e.g. "please include this other sub-template here")

    but nothing else -- and especially not business logic! (if I forgot something important, please mention it in a comment)

    Also, your templates ("views") should never be used as the target URLs that you application uses. You should have the URLs point to (possibly one of many) "controller" scripts, which then invoke the necessary business logic and pass the results to your template for display by including the template. You will find this easy to grasp if you are familiar with Model-View-Controller; if you are not, familiarize yourself first.

    Finally, here's one way you could template the markup you gave:

    <html xmlns="http://www.w3.org/1999/xhtml">
        <head>
            <title><?php echo $title;?></title>
            <meta http-equiv="content-type" content="text/html; charset=UTF-8" />
    <?php foreach ($stylesheets as $stylesheet) : ?>
            <link href="<?php echo $stylesheet;?>" rel="stylesheet" type="text/css"/>
    <?php endforeach; ?>
            <link href="favicon.png" rel="shortcut icon" />
        </head>
        <body>
        </body>
    </html>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?