简体   繁体   English

什么等效于PHP中来自ASP.NET的主视图?

[英]What is the equivalent to Master Views from ASP.NET in PHP?

I'm used to working in ASP.NET / ASP.NET MVC and now for class I have to make a PHP website. 我曾经在ASP.NET/ASP.NET MVC中工作,现在要上课,我必须创建一个PHP网站。

What is the equivalent to Master Views from ASP.NET in the PHP world? PHP世界中相当于ASP.NET的主视图?

Ideally I would like to be able to define a page layout with something like: 理想情况下,我希望能够使用以下内容定义页面布局:

Master.php Master.php

<html>
    <head>
        <title>My WebSite</title>
        <?php headcontent?>
    </head>
    <body>
        <?php bodycontent?>
    </body>
</html>

and then have my other PHP pages inherit from Master, so I can insert into those predefined places. 然后让我的其他PHP页面继承自Master,因此我可以将其插入这些预定义的位置。

Is this possible in PHP? 这在PHP中可行吗?

Right now I have the top half of my page defined as "Header.html" and the bottom half is "footer.html" and I include_once both of them on each page I create. 现在,我将页面的上半部分定义为“ Header.html”,下半部分定义为“ footer.html”,并且在创建的每个页面上都包含了它们。 However, this isn't ideal for when I want to be able to insert into multiple places on my master page such as being able to insert content into the head. 但是,当我希望能够插入到母版页上的多个位置(例如能够将内容插入头部)时,这不是理想的选择。

Can someone skilled in PHP point me in the right direction? 懂PHP的人可以为我指出正确的方向吗?

You can use Smarty or develop your own template engine. 您可以使用Smarty或开发自己的模板引擎。 Basically you can define a simple layout like this: 基本上,您可以定义一个简单的布局,如下所示:

layout.tpl: layout.tpl:

<html>
    <head>
        <title>My WebSite</title>
        {$header}
    </head>
    <body>
        {$bodycontent}
    </body>
</html>

define your header: 定义标题:

header.tpl header.tpl

<script type="text/javascript" src="js/myjs.js"></script>

and your content file: 和您的内容文件:

home.tpl home.tpl

<div>hello {$var}! </div>

And finally your controller: 最后是您的控制器:

index.php index.php

<?php
$header = new Smarty();
$headerOutput = $header->fetch('header.tpl');

$content = new Smarty();
$content->assign('var', 'world');
$contentOutput = $content->fetch('home.tpl');

$layout = new Smarty();
$layout->display('header', $headerOutput);
$layout->display('bodycontent', $contentOutput);
$layout->display('layout.tpl');

?>

Since PHP is a programming language and not a framework, it doesn't have that functionality out of the box. 由于PHP是一种编程语言,而不是框架,因此它没有现成的功能。

There are many solutions to this problem, probably the most complex one would be to actually use a framework. 这个问题有很多解决方案,最复杂的解决方案可能是实际使用框架。 There are many PHP frameworks to choose from. 许多PHP框架可供选择。 Downside would be that you'd be learning a framework and not PHP. 缺点是您将学习框架而不是PHP。

A simpler solution is to use a Templating engine . 一个更简单的解决方案是使用模板引擎 You'd be closer to PHP but you'd still have to learn how to use such engine. 您将更接近PHP,但是您仍然必须学习如何使用这种引擎。

And you're already using the simplest solution: including files from a main file. 而且您已经在使用最简单的解决方案:包括来自主文件的文件。 You can also include PHP files that is going to get executed and not only static html. 您还可以包括将要执行的PHP文件,而不仅仅是静态html。

If you don't want to use a Templating engine, one solution could be like this: 如果您不想使用模板引擎,则一种解决方案可能是这样的:

<html>
    <head>
        <title>My WebSite</title>
        <?php include('headcontent'); ?>
    </head>
    <body>
        <?php include('bodycontent'); ?>
    </body>
</html>

And in bodycontent : 身体内容上

<?php
switch ($_GET['page'];
    case 1:
        include('page1');
        break;
    case 2:
        include('page2');
        break;
}
?>

bodycontent will be like a very simple controller. bodycontent就像一个非常简单的控制器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM