开发者

RenderBody in php

开发者 https://www.devze.com 2023-04-05 07:28 出处:网络
If you have used ASP.NET MVC you\'d be familiar with RenderBody. Basically, you have one layout page and several body pages. Something like this:

If you have used ASP.NET MVC you'd be familiar with RenderBody. Basically, you have one layout page and several body pages. Something like this:

layout.cshtml:

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

index.cshtml:

@{
   layout = "layout.cshtml";
}

<p>Hello World!</p>

So when you call index.cshtml, all of its content will be shown in the layout's @RenderBody section. This is really useful when your pages use a single layout.

Now, my question is, how could I achieve something similar to the code above in php?

EDIT

For those who 开发者_开发百科are not familiar with ASP.NET, when you have an index2.cshtml file like this:

@{
   layout = "layout.cshtml";
}

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

Then when you call index2.cshtml this time 'Hello World, once again!' would be printed. So basically, when you define the page's layout, all of its content is displayed in the @RenderBody section of its layout. You don't have to explicitly define what page to include in the layout.


I don't know ASP.NET but here's how you'd most probably do the same in PHP:

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

and body.php could then contain

<p>Hello World!</p>

(very) Simple routing example:

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

You could then define a top, middle and bottom template for each page in your database :)


You can do it (also) with Twig:

main_layot.twig:

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

and content:

{% extends "main_layout.twig" %}

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


I know this is an older question, but coming from ASP.net+MVC3 development I found a better solution.

Create a master.php page, such as this (with doctype and whatever else, etc. you get the idea)

master.php:

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

next I have a separate folder just to keep it neat, you don't need to (e.g Content/) Place all your content files in this folder, what you include in your ASP.net pages, i'll call mine default.php

default.php:

<div>
    Hello World
</div>

Then create the file you want to hit to load the page content, i'll call mine index.php

index.php:

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

The cons:

  • two files per every page, this could be circumvented by putting the page content directly in the variable, but I prefer a separate file for neatness.

Benefits:

  • No extra .htaccess or any additional server requirements
  • Allows an infinite number of variables to be passed by each page
  • Mimics the ASP.net RenderBody() function exactly like you want :D

It's by no means an original idea, I found another web page that utilized this approach and it's exactly what I wanted to do in my web page.

This SO post is the same thing i found when googling for how to do the same thing, so I wanted to answer with my solution.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号