PHP中的简单模板方法

发布于 2024-09-06 23:24:41 字数 474 浏览 3 评论 0原文

我正在尝试为我的 PHP CMS 构建模板系统的最佳方法。我有点困惑,所以我正在寻找一些建议或想法。这是我想要的设置:

每个页面都由各种小部件(或内容块,如果您愿意)组成。每个小部件都有 MVC 架构,视图很简单,主要由 HTML 和少量 PHP 组成。部件控制器将从模型传递信息来填充视图。

到目前为止,一切都很好。问题是:我希望视图采用字符串格式,而不是仅仅将窗口小部件视图文件包含到页面上。我可以做到这一点,但我当前的解决方案失去了 MVC 方法的一些简洁性和简单性,因为模型和视图往往会结合在一起。

我可能会在这里寻求月亮,但我想我会寻求想法,以防我错过一些明显明显的东西。有没有一种方法可以让我保留一个简单的视图,能够填充它,执行任何语句和循环,并将结果保留为字符串?我想使其尽可能简单,并希望避免一些大型正则表达式解析解决方案。

请注意,我正在寻找通用方法或代码示例,但我并不是在寻找要集成的现有模板系统。如果您需要澄清任何事情,只需添加评论,我会更新问题。

I'm trying to wrap my head around the best way to do a templating system for my PHP CMS. I'm a little stuck and so I'm looking for a few suggestions or ideas. Here is my desired setup:

Each page is composed of various widgets (or content blocks, if you prefer). Each widget has an MVC architecture, with the View being simple, composed of mainly HTML with a little PHP. The Widget Controller will pass info from the Model to populate the View.

So far, so good. Here's the catch: Rather than just including the Widget View file onto the page, I'd like to have the View in string format. I can do that, but my current solutions lose some of the cleanness and simplicity of the MVC approach as the Model and View tend to get combined.

I might be asking for the moon here, but I thought I would ask for ideas in case I'm missing something blatantly obvious. Is there a way that I can keep a simple View, be able to populate it, execute any statements and loops, and keep the result as a string? I'd like to keep it as simple as possible and would like to avoid some large regex parsing solution.

Please note that I'm looking for a general approach or code examples, but I'm not looking for an existing templating system to integrate. If you need clarification on anything, just add a comment and I'll update the question.

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

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

发布评论

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

评论(3

葬﹪忆之殇 2024-09-13 23:24:41

在包含视图脚本之前使用 ob_start() ,并且 ob_get_contents() 之后。

Use ob_start() before you include the view script, and ob_get_contents() afterwards.

寄人书 2024-09-13 23:24:41

使用输出缓冲和函数作用域的好处:

类似:

class View {
   var $params;
   function get_view_template($file_path) {
      extract($this->params);
      ob_start();
      include $file_path;
      $string = ob_get_contents();
      ob_end_clean();
      return $string;
   }
}

Use output buffering and the benefit of function scoping:

Something like:

class View {
   var $params;
   function get_view_template($file_path) {
      extract($this->params);
      ob_start();
      include $file_path;
      $string = ob_get_contents();
      ob_end_clean();
      return $string;
   }
}
逆光下的微笑 2024-09-13 23:24:41

看看Smarty。您完全可以分离视图并将小部件的模板包含到页面的模板中等等。

编辑:好吧,我没有看到“我不想要现有系统”,那么您创建一些变量注册表怎么样,每次您想要将变量传递给模板系统时,您调用类似 VariableRegistry::assign('variable_name', $value) 的内容。我正在使用类似的东西(与 http 响应上的 Smarty 集成)。这样做的好处是,您可以将所有传递的变量序列化为 json、xml 等,这在以后的 Ajax 和 REST api 中会派上用场:-)

Have a look at Smarty. You can absolutely separate the views and include the widgets' templates into pages' templates and so on.

EDIT: Ok, i didn't see the "i don't want existing system", so what about you create some variable registry and each time you want to pass a variable to your templating system, you call something like VariableRegistry::assign('variable_name', $value). I'm using something like it (integrated with Smarty on http responses). The good thing about this is that you can serialize all your passed variables to json, xml etc., that comes in handy for Ajax and REST api later :-)

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