将代码与模板中的布局分离

发布于 2024-10-12 05:27:44 字数 1062 浏览 3 评论 0原文

我有一个围绕 MVC 模式构建的应用程序。 视图是 php,但主要是 html,嵌入了最少的 php 代码,像这样的东西 -

Welcome <?php echo $USERNAME ?>

<table>
<?php foreach ($USERS as $row) : ?>
<tr><td><?php echo $row->name ?><td><?php echo $row->address ?></tr>
<?php endforeach ?>

只想在这个文件中显示布局逻辑,并且我想保持简单!

这对我来说非常有效,但我在某些方面遇到了困难。 例如,查看上面代码中的表格,想象每列(名称和地址)都有一个标题 NameAddress

现在假设我想让列可排序。所以我做了类似的事情 -

<tr><th><a href="?sort=name">Name</a><td><a href="?sort=addr">

但这还不够好。我的视图需要查看按哪一列排序并添加向上或向下箭头。如果当前排序列已经排序,则需要将当前排序列的链接更改为 ?sort=name_reverse,以便单击它以其他方式排序。 现在在我的模板中编写漂亮整洁的代码有点太复杂了...

所以我可以让我的控制器创建包含 -

<tr><th><?php echo $HEADING[0] ?><th><?php $HEADING[1] ?> 

等的变量。但这意味着我的控制器现在正在生成实际的 HTML,这实际上是页面模板的责任。它消除了模板以某些不同方式格式化页面的能力......而且感觉不对。

但是我怎样才能最好地处理这个问题,我觉得我需要我的页面控制器生成包含 HTML 的变量......

有什么建议吗?

I have an app built around an MVC pattern.
The view is php but mostly html with minimal php code embedded, stuff like this -

Welcome <?php echo $USERNAME ?>

and

<table>
<?php foreach ($USERS as $row) : ?>
<tr><td><?php echo $row->name ?><td><?php echo $row->address ?></tr>
<?php endforeach ?>

I only want display layout logic in this file,and I want to keep it simple!

This is mostly working really weel for me however I'm struggling a bit certain aspects.
For example look at the table in the code above and imagine that each column (name, and address) has a title
NameAddress

Now imagine I want to make the columns sortable. So I make it something like -

<tr><th><a href="?sort=name">Name</a><td><a href="?sort=addr">

But this isn't good enough. My view needs to look at which column is being sorted by and add an up or down arrow. It needs tochange the link of the currently sorted column to ?sort=name_reverse if that column is already being sorted so that clicking on it sorts it the other way.
It's a bit too complicated to write nice neat code in my template now...

So I can get my controller to create variables containing -

<tr><th><?php echo $HEADING[0] ?><th><?php $HEADING[1] ?> 

etc. BUT this means that my controller is now generating actual HTML which is really the responsibility of the page template. It removes the ability of the template to format page in some different ways... And just feels wrong.

But how can I best deal with this, where I feel I need my page controller to be generating variables containing HTML...

Any suggestions?

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

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

发布评论

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

评论(4

记忆で 2024-10-19 05:27:44

将创建表的代码分离到 ViewHelper 中。配置从模板的方法调用。让 ViewHelper 使用所有必需的设置渲染实际的表。然后,您的主模板应该只包含这样的一行:

<?php echo table($data, $sortOptions, $otherConfig) ?>

基本上,构建该表所需的任何逻辑都将包含在 ViewHelper 中,这就是您可能希望将其设为一个类的原因。完整的实施将根据您的需求而有所不同。

如果您不想将所有逻辑放入一个大型表帮助程序中,请考虑为表的任何部分创建帮助程序函数。例如,像

<th><?php echo sortLink('Name', $current); ?></th>

这样,sortLink 帮助器将决定是否以及如何创建和插入任何排序链接,因此您不必在主模板中包含该逻辑或任何条件。

Separate the code to create the table into a ViewHelper. Configure the method call from the template. Have the ViewHelper render the actual table with all required settings. Your main template should then contain only a line like this:

<?php echo table($data, $sortOptions, $otherConfig) ?>

Basically, any logic required to build that table will be contained within the ViewHelper which is why you might want to make it a class. The complete implementation will vary depending on your needs.

If you dont want to put all the logic into one large Table Helper, consider making helper functions for any parts of the table. For instance, something like

<th><?php echo sortLink('Name', $current); ?></th>

This way, the sortLink helper would decide whether and how to create and insert any sort links, so you dont have to have that logic or any conditionals in the main template.

我的黑色迷你裙 2024-10-19 05:27:44

很好的问题,这对于这个网站来说非常罕见。

首先,您必须接受这样一个事实:任何现实生活中的模板都不会简单。
就是这样。它称为表示逻辑,它遵循表示本身的复杂性。

因此,只需使用与以前相同的方式即可 - 通过使用相同的循环和条件运算符以及 - 可能 - 一些自制的辅助函数,这会破坏模板的纯度,但会很有帮助。

事实上,你几乎已经成功了。您可以使用预定义变量,但它们不应包含 HTML,而应仅包含要显示的值。

<tr>
 <th>
  <a href="<?=$links['name']['url']?>">
   Name
   <?if($links['name']['dir']=='asc'):?><img src="arrow.gif"><?endif?>
   <?if($links['name']['dir']=='desc'):?><img src="reverse_arrow.gif"><?endif?>
  </a>
 <td>

很想以此为循环,但我建议不要这样做,因为它会导致您发明一些 HTML 构建器,这是另一个故事,而且不如纯 HTML 本身那么可靠。

你唯一的责任是:一个程序员,寻求代码帮助,应该带来代码,而不是啰嗦的废话。实际 HTML 的示例会好得多,这样回答者就不会凭空想象出来。

Excellent question, which is very uncommon for this site.

First of all you HAVE to accept the fact that any real life template will never be simple.
That's it. It's called presentation logic and it follows the complexity of the presentation itself.

So, just make it the same way as before -- by using the same loop and conditional operator and -- possibly -- some home-brewed helper functions, which will spoil template purity but greatly helps.

In fact, you have almost nailed it. you can use predefined variables, however they should contain no HTML but just values to be displayed.

<tr>
 <th>
  <a href="<?=$links['name']['url']?>">
   Name
   <?if($links['name']['dir']=='asc'):?><img src="arrow.gif"><?endif?>
   <?if($links['name']['dir']=='desc'):?><img src="reverse_arrow.gif"><?endif?>
  </a>
 <td>

it is tempting to make a loop out of this but I would advise against that, because it will lead you to inventing some HTML builder which is another story and not that reliable as pure HTML itself.

The only blame for you: a programmer, asking for the code assistance, should bring the code, not wordy blab. An example of the actual HTML would be much better, allowing answerers not to invent it out of the head.

黑白记忆 2024-10-19 05:27:44

如果我可以添加一个建议,您可以将所有 PHP 输出转换为 DOM/XML,并将其提供给 XSLT 处理器,在该处理器中,无论多么复杂,演示都更加一致,因为 XSLT 和 XHTML 都基于 XML。您确实可以将演示文稿与内容分开,无论是在物理位置还是用于交付演示文稿的技术方面。

If I may add a suggestion, you can convert all PHP output to DOM/XML and feed it to an XSLT processor where the presentation, however complex, is more consistent, because XSLT and XHTML are both based on XML. You do get presentation separated from content both in terms of physical location and technology used to deliver it.

浅浅 2024-10-19 05:27:44

一种解决方案是使用 Javascript 排序,jQuery 有一些不错的解决方案。

如果这是不可能的,您可以查看一些模板解决方案,例如 Smarty 来为您处理模板。

One solution is to use Javascript sorting, there is some good ones for jQuery.

If that is not possible y0ou might have a look on some template solution like Smarty to handle templateing for you.

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