需要一些关于 PHP/HTML 模板的建议

发布于 2024-10-28 09:58:06 字数 981 浏览 2 评论 0原文

我读了这篇文章我真的很喜欢这种模板解决方案,但我不确定一件事。有了这个系统,业务逻辑和表示逻辑不是会在两个不同的 php 文件中吗?这一切都很好,因为它将它们分开,但是如果只有某些页面上的表示逻辑,或者如果业务逻辑量非常少怎么办?

用户页面的默认页面有时仅是表示逻辑,有时仅是业务逻辑,这似乎很奇怪。似乎有两个明显的解决方案:

1)让所有默认页面都具有链接到不同页面上的表示逻辑的业务逻辑(即使没有)。这样做的问题是存在很多“不必要的”页面。好处是它是一致的。

2)如果页面没有业务逻辑,则仅包含表示逻辑。这样做的问题是,在查看文件名时,哪个 php 页面包含业务和表示逻辑是不一致的。

另外,这可能有点偏离主题,但是有什么方法可以对此进行模板化吗?

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

考虑到我在每一页上都有这个,我想知道是否有一种方法可以对此进行模板化,以便代码可以全部放在一个文件中并被调用。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

格子衫的從容 2024-11-04 09:58:06

您的模板应包含严格的表示逻辑。

这意味着您可以包含:

  • 循环(打印列表等)
  • 条件(例如,仅在某个值非空时打印元素)
  • 格式化函数(去除空格、整数等)
  • 少量模板组合辅助函数调用(例如“请在此处包含另一个子模板”),

没有其他内容 - 尤其不是业务逻辑!(如果我忘记了)重要的事情,请在评论中提及)

此外,您的模板(“视图”)永远不应该用作您的应用程序使用的目标 URL。您应该让 URL 指向(可能是多个)“控制器”脚本,然后该脚本调用必要的业务逻辑并将结果传递到您的模板以通过包含该模板进行显示。如果您熟悉模型-视图-控制器,您会发现这很容易掌握;如果您不熟悉,请先熟悉一下。

最后,这是一种可以模板化所提供的标记的方法:

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

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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文