【问题标题】:RenderBody in phpphp中的渲染体
【发布时间】:2011-11-17 00:23:19
【问题描述】:

如果您使用过 ASP.NET MVC,您就会熟悉 RenderBody。基本上,您有一个布局页面和几个正文页面。像这样的:

layout.cshtml:

<html>
  <head>
    <title>Your Title</title>
  </head>
  <body>
    @RenderBody()
  </body>
</html>

index.cshtml:

@{
   layout = "layout.cshtml";
}

<p>Hello World!</p>

所以当你调用 index.cshtml 时,它的所有内容都会显示在布局的@RenderBody 部分。当您的页面使用单一布局时,这非常有用。

现在,我的问题是,我怎样才能在 php 中实现类似于上面代码的东西?

编辑

对于那些不熟悉 ASP.NET 的人来说,当你有一个这样的 index2.cshtml 文件时:

@{
   layout = "layout.cshtml";
}

<p>Hello World, once again!</p>

然后当你这次调用 index2.cshtml 'Hello World, again!'将被打印。所以基本上,当你定义页面的布局时,它的所有内容都显示在其布局的@RenderBody 部分中。您不必明确定义要包含在布局中的页面。

【问题讨论】:

  • 不使用模板是什么意思?
  • 对不起,我的意思是框架:)

标签: php asp.net


【解决方案1】:

我不了解 ASP.NET,但您最有可能在 PHP 中做同样的事情:

<html>
  <head>
    <title>Your Title</title>
  </head>
  <body>
    <?php include('body.php'); ?>
  </body>
</html>

然后body.php 可以包含

<p>Hello World!</p>

(非常)简单的路由示例:

$router    =  new RequestRouter; //this class would route a request to a set of templates stored in a persistent storage engine like a database
$request   =  $_SERVER['QUERY_STRING'];
$templates =  $router->resolve($request); //would return an array with the templates to be used
include('master.php');

master.php:

<html>
  <head>
    <title>Your Title</title>
  </head>
  <body>
    <div>
        <?php include($templates['top']); ?>
    </div>
    <div>
        <?php include($templates['middle']); ?>
    </div>
    <div>
        <?php include($templates['bottom']); ?>
    </div>
  </body>
</html>

然后您可以为数据库中的每个页面定义一个topmiddlebottom 模板:)

【讨论】:

  • 也许我必须更具体一些。当您调用具有相同布局的第二个页面时,将显示其内容。我的意思是,如果你有 index2.cshtml 并且它的内容是“Hello World, again”,那么它就会被显示出来。因此,根据您的建议,也许最接近的方法是在不同情况下使用 switch 来包含不同的文件。
  • 或者创建一个路由引擎并将 URL 路由到一组要包含的正文模板:)
  • 你能给我举个例子吗?
【解决方案2】:

您也可以使用Twig

main_layot.twig:

<!DOCTYPE html>
<html>
    <head>
        <title>Example</title>
    </head>
    <body>
            {% block content %}{% endblock %}
    </body>
</html>

和内容:

{% extends "main_layout.twig" %}

{% block content %} Content {% endblock %}

【讨论】:

    【解决方案3】:

    我知道这是一个较老的问题,但来自 ASP.net+MVC3 开发我找到了一个更好的解决方案。

    创建一个 master.php 页面,例如这个(使用 doctype 和其他任何内容,等等。你明白了)

    master.php:

    <head> 
        my_stuff, meta tags, etc.
        <title><?php echo $page_title; ?></title>
    </head>
    <body>
        <?php include('$page_content') ?>
    </body>
    

    接下来我有一个单独的文件夹只是为了保持整洁,你不需要(例如 Content/) 将你所有的内容文件放在这个文件夹中,你在 ASP.net 页面中包含的内容,我将调用我的 default.php

    default.php:

    <div>
        Hello World
    </div>
    

    然后创建你要加载页面内容的文件,我会调用我的 index.php

    index.php:

    <?php
        $page_title = 'Hello Example';
        $page_content = 'Content/default.php';
        include('master.php');
    ?>
    

    缺点:

    • 每页两个文件,这可以通过将页面内容直接放在变量中来规避,但为了整洁,我更喜欢单独的文件。

    好处:

    • 没有额外的 .htaccess 或任何额外的服务器要求
    • 允许每个页面传递无限数量的变量
    • 完全按照您的要求模仿 ASP.net RenderBody() 函数:D

    这绝不是一个原创的想法,我找到了另一个使用这种方法的网页,这正是我想要在我的网页中做的事情。

    这篇 SO 帖子与我在谷歌搜索如何做同样的事情时发现的相同,所以我想用我的解决方案来回答。

    【讨论】:

      猜你喜欢
      • 2017-03-28
      • 2014-09-02
      • 1970-01-01
      • 1970-01-01
      • 2012-03-19
      • 2011-10-30
      • 1970-01-01
      • 2014-12-14
      • 1970-01-01
      相关资源
      最近更新 更多