将代码与模板中的布局分开 [英] Separating code from layout in template

查看:54
本文介绍了将代码与模板中的布局分开的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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

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

等但是,这意味着我的控制器现在正在生成实际的HTML,这实际上是页面模板的责任.

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.

但是我该如何最好地处理呢?我觉得我需要页面控制器来生成包含HTML的变量...

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

有什么建议吗?

推荐答案

将代码分开,以将表创建到ViewHelper中.从模板配置方法调用.让ViewHelper渲染具有所有必需设置的实际表.然后,您的主模板应仅包含以下行:

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

基本上,构建该表所需的任何逻辑都将包含在ViewHelper中,这就是为什么您可能希望使其成为类.完整的实现会根据您的需求而有所不同.

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>

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

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.

这篇关于将代码与模板中的布局分开的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆