在 PHP 中创建视图 - 最佳实践

发布于 2024-11-13 09:48:30 字数 1431 浏览 2 评论 0原文

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

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

发布评论

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

评论(3

醉生梦死 2024-11-20 09:48:30

如果您不想使用模板引擎,则可以利用 PHP 的基本模板功能。

实际上,您应该只编写 HTML,每当需要输出变量的值时,使用 打开 PHP 部分,并使用 ?> 关闭它。对于示例,我假设 $data 是您的数据对象。

例如:

<div id="fos"><?php echo $data->getWhatever(); ?></div>

请注意,所有 PHP 控制结构(如 ifforeachwhile 等)也有一个语法,可以用于模板化。您可以在他们的 PHP 手册页中查找这些内容。

例如:

<div id="fos2">
<?php if ($data->getAnother() > 0) : ?>
    <span>X</span>
<?php else : ?>
    <span>Y</span>
<?php endif; ?>
</div>

如果您知道服务器上将启用短标记,那么为了简单起见,您也可以使用它们(不建议在 XML 和 XHTML 中使用)。使用短标签,您可以简单地使用 打开 PHP 部分,并使用 ?> 关闭它。另外, 是回显某些内容的简写。

第一个带有短标签的示例:

<div id="fos"><?=$data->getWhatever()?></div>

您应该注意在哪里使用换行符和空格。浏览器将收到与您编写的相同的文本(PHP 部分除外)。我的意思是:

编写此代码:

<?php
    echo '<img src="x.jpg" alt="" />';
    echo '<img src="y.jpg" alt="" />';
?>

与此代码不同:

<img src="x.jpg" alt="" />
<img src="y.jpg" alt="" />

因为在第二个代码中,您在 img 元素之间有一个实际的 \n ,它将是由浏览器翻译为空格字符,并显示为图像之间的实际空格(如果它们是内联的)。

If you don't want to use a templating engine, you can make use of PHP's basic templating capabilities.

Actually, you should just write the HTML, and whenever you need to output a variable's value, open a PHP part with <?php and close it with ?>. I will assume for the examples that $data is your data object.

For example:

<div id="fos"><?php echo $data->getWhatever(); ?></div>

Please note that, all PHP control structures (like if, foreach, while, etc.) also have a syntax that can be used for templating. You can look these up in their PHP manual pages.

For example:

<div id="fos2">
<?php if ($data->getAnother() > 0) : ?>
    <span>X</span>
<?php else : ?>
    <span>Y</span>
<?php endif; ?>
</div>

If you know that short tag usage will be enabled on the server, for simplicity you can use them as well (not advised in XML and XHTML). With short tags, you can simply open your PHP part with <? and close it with ?>. Also, <?=$var?> is a shorthand for echoing something.

First example with short tags:

<div id="fos"><?=$data->getWhatever()?></div>

You should be aware of where you use line breaks and spaces though. The browser will receive the same text you write (except the PHP parts). What I mean by this:

Writing this code:

<?php
    echo '<img src="x.jpg" alt="" />';
    echo '<img src="y.jpg" alt="" />';
?>

is not equivalent to this one:

<img src="x.jpg" alt="" />
<img src="y.jpg" alt="" />

Because in the second one you have an actual \n between the img elements, which will be translated by the browser as a space character and displayed as an actual space between the images if they are inline.

情场扛把子 2024-11-20 09:48:30

使用单独的文件读取数据:

<?php
 if ($foo == False)
  {
  $bar = 1;
  }
 else
  {
  $bar = 0;
  }
?>

然后在 HTML 文件中引用结果状态:

require 'logic.php';

<html>
  <!--...-->
  <input type="text" value="<?php echo $bar; ?>" > //Logic is separated from markup
  <!--...-->
</html>

Use a separate file to read the data:

<?php
 if ($foo == False)
  {
  $bar = 1;
  }
 else
  {
  $bar = 0;
  }
?>

Then reference the resulting state in the HTML file:

require 'logic.php';

<html>
  <!--...-->
  <input type="text" value="<?php echo $bar; ?>" > //Logic is separated from markup
  <!--...-->
</html>
惜醉颜 2024-11-20 09:48:30

我不知道我真的明白你的问题。所以如果我的答案不准确,我想删除

这个类,它将创建简单的视图,

class View
{

public function render($filename, $render_without_header_and_footer = false)
{
    // page without header and footer, for whatever reason
    if ($render_without_header_and_footer == true) {
        require VIEWS_PATH . $filename . '.php';
    } else {
        require VIEWS_PATH . '_templates/header.php';
        require VIEWS_PATH . $filename . '.php';
        require VIEWS_PATH . '_templates/footer.php';
    }
}


private function checkForActiveController($filename, $navigation_controller)
{
    $split_filename = explode("/", $filename);
    $active_controller = $split_filename[0];

    if ($active_controller == $navigation_controller) {
        return true;
    }
    // default return
    return false;
}

private function checkForActiveAction($filename, $navigation_action)
{
    $split_filename = explode("/", $filename);
    $active_action = $split_filename[1];

    if ($active_action == $navigation_action) {
        return true;
    }
    // default return of not true
    return false;
}

private function checkForActiveControllerAndAction($filename, $navigation_controller_and_action)
{
    $split_filename = explode("/", $filename);
    $active_controller = $split_filename[0];
    $active_action = $split_filename[1];

    $split_filename = explode("/", $navigation_controller_and_action);
    $navigation_controller = $split_filename[0];
    $navigation_action = $split_filename[1];

    if ($active_controller == $navigation_controller AND $active_action == $navigation_action) {
        return true;
    }
    // default return of not true
    return false;
}
}

所以现在您可以创建模板并可以从任何地方调用它,就像

$this->view->my_data = "data";
$this->view->render('index/index');
//

在您的index/index.php上您可以调用数据$this- >我的数据;

i dont know it i get realy your question. so if my answer not exaclty i will like to de deleted

this class will create simple view

class View
{

public function render($filename, $render_without_header_and_footer = false)
{
    // page without header and footer, for whatever reason
    if ($render_without_header_and_footer == true) {
        require VIEWS_PATH . $filename . '.php';
    } else {
        require VIEWS_PATH . '_templates/header.php';
        require VIEWS_PATH . $filename . '.php';
        require VIEWS_PATH . '_templates/footer.php';
    }
}


private function checkForActiveController($filename, $navigation_controller)
{
    $split_filename = explode("/", $filename);
    $active_controller = $split_filename[0];

    if ($active_controller == $navigation_controller) {
        return true;
    }
    // default return
    return false;
}

private function checkForActiveAction($filename, $navigation_action)
{
    $split_filename = explode("/", $filename);
    $active_action = $split_filename[1];

    if ($active_action == $navigation_action) {
        return true;
    }
    // default return of not true
    return false;
}

private function checkForActiveControllerAndAction($filename, $navigation_controller_and_action)
{
    $split_filename = explode("/", $filename);
    $active_controller = $split_filename[0];
    $active_action = $split_filename[1];

    $split_filename = explode("/", $navigation_controller_and_action);
    $navigation_controller = $split_filename[0];
    $navigation_action = $split_filename[1];

    if ($active_controller == $navigation_controller AND $active_action == $navigation_action) {
        return true;
    }
    // default return of not true
    return false;
}
}

soo now you can create your templates and can call it from any where just like

$this->view->my_data = "data";
$this->view->render('index/index');
//

and on your index/index.php you can call the data $this->my_data;

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