PHP 视图,使用模板

发布于 2024-12-19 09:08:16 字数 1676 浏览 2 评论 0原文

好吧,我的问题很简单,但有点难以接受解决方案,但无论如何..如下,我有一个“迷你框架”,需要编写单个方案,对我有很大帮助,加速了某些事情的工作,但是,问题是即使在视图中,在某种程度上,使用模板方案也非常简单,也非常有趣,因为当您必须更改与以下内容相关的任何内容时可视化,模板仅发生变化,但随后,及时渲染此模板,这是最好的方法吗?我目前正在这样工作:

<?php

          class View {

                 private $vars;

                 public function __get ( $var ) {
                        if ( isset( $this->vars [ $var ] ) ) {
                               return $this->vars[ $var ];
                        }
                 }

                 public function assign ( $var , $value ) {
                        $this->vars [ $var ] = $value;
                 }

                 public function show ( $template ) {
                        include_once sprintf ( "%s\Templates\%s" , __DIR__ , $template ) ;
                 }

          }

这不是完整的代码,我正在构建结构并审查方案,所以我执行以下操作..

<?php
          require_once 'MVC/Views/View.php';
          $View = new View ( ) ;

          $View->assign( 'title' , 'MVC, View Layer' ) ;
          $View->show ( 'test.phtml' );

并且模板

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
       <head>
              <meta http-equiv="Content-Type" content="text/html;charset=UTF-8" />
              <title><?php echo $this->title ?></title>
       </head>
       <body>

       </body>
</html>

输出是正确的,一切都按预期工作,但我的问题是:这是最好的方法吗?包含该文件并让 play 解释 .phtml 中编写的代码

Well, my question is very simple but a little hard to accept the solution, but anyway .. is the following, I have a 'mini-framework', something to work on writing up of a single scheme, helps me a lot, accelerate work on some things, however, the question is even with the view, in a way, using a scheme of templates is very easy and also very interesting because when you have to change anything related to visualization, the template changes only, but then, in time to render this template, which is the best way? I'm currently working this way:

<?php

          class View {

                 private $vars;

                 public function __get ( $var ) {
                        if ( isset( $this->vars [ $var ] ) ) {
                               return $this->vars[ $var ];
                        }
                 }

                 public function assign ( $var , $value ) {
                        $this->vars [ $var ] = $value;
                 }

                 public function show ( $template ) {
                        include_once sprintf ( "%s\Templates\%s" , __DIR__ , $template ) ;
                 }

          }

It is not the complete code, I am building structures and reviewing the scheme yet, so I do the following ..

<?php
          require_once 'MVC/Views/View.php';
          $View = new View ( ) ;

          $View->assign( 'title' , 'MVC, View Layer' ) ;
          $View->show ( 'test.phtml' );

And the template

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
       <head>
              <meta http-equiv="Content-Type" content="text/html;charset=UTF-8" />
              <title><?php echo $this->title ?></title>
       </head>
       <body>

       </body>
</html>

The output is correct, all working as expected, but my question is: is this the best way to do? including the file and letting the play interprets the code written in .phtml

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

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

发布评论

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

评论(1

鹿港小镇 2024-12-26 09:08:16

在许多框架中,我看到了这种语句:

public function show ( $template ) {
  ob_start();
  require sprintf ( "%s\Templates\%s" , __DIR__ , $template ) ;
  return ob_get_flush();
}

使用输出缓冲区,您可以将模板计算为字符串,而不是直接在输出中发送。当您需要在评估模板后更改标题或进行后处理时,这会很方便。

使用 require 而不是 include_once 将允许您多次渲染相同的模板(例如,如果您想要某种模板组合),并且如果找不到模板文件,则会收到错误(include 不会给出错误)情况)。

In many frameworks I saw this kind of statements:

public function show ( $template ) {
  ob_start();
  require sprintf ( "%s\Templates\%s" , __DIR__ , $template ) ;
  return ob_get_flush();
}

Using the output buffer you can have the template evaluated to a string instead of directly sent in the output. This can come handy when you need to change headers after evaluating the template or to make post-processings.

Using require instead of include_once will allow you to render the same template more times (e.g. if you want to have some kind of templates composition) and to get an error if the template file is not found (include doesn't give an error in that situation).

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