如何在一个php包含文件中包含多个页面内容? [英] How do you include multiple page content in one php include file?

查看:108
本文介绍了如何在一个php包含文件中包含多个页面内容?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很想知道如何将各种元素(header,footer,nav)包含在一个php文件中以包含在各种页面中,而不是创建多个php文件来单独调用?



在我的包含文件中,我有以下代码:

 <?php 
echo '< h1 class =sub-elements>当前提交期间的元素为:< span style =font-weight:bold; color:#ff6000;> Carnival,Residue,Maudlin< / span>。< / h1>';
?>

输出到运行php的html页面上:

 <?php include('php-includes / current-elements.php');?> 

是否有一种方法可以包含特定的div或类,而不是整个文件本身?



我只是不想为每个想包含在同一页面上的小元素创建一个单独的php包含文件。



下面的代码是我在同一个包含文件中调用另一个页面上显示的另一个元素的内容。

  function elements()
{
echo'< h1 class =sub-elements>当前提交期间的元素为:< span style =font-weight:bold; color:#ff6000;>测试与LT; /跨度>。< / H1>;
elements();
}
?>

任何帮助都会很棒!

解决方案

听起来你正在试图避免模板。基本思想是为每个head,footer,nav定义一个单独的文件,并包含来自内容模板的文件。



head.php



 <!doctype html> < HTML> ... 



nav.php



 <身体GT; < a href = ..>第1页< / a> < a href = ..>第2页< / a> < a href = ..>第3页< / a> 



foot.php



 < /体> < / HTML> 

为了创建一个完整的页面,您可以这样做:

 <?php 
include'head.php';
包含'nav.php'

一篇关于李子汁的文章。

包含'foot.php';
?>

现在,如果我理解正确,您只需要包含一个文件并能够生成各种元素(头,导航,whathaveyou)。你可以通过在函数中包含include语句来做到这一点:

ubertemplate



 <?php 
function head(){include'head.php'; }
函数nav(){include'nav.php'; }

function randomElement(){?>
一篇关于李子汁的文章。 <?php
}

函数totalymisguideddynamiccontents(){
echo< div> foobar< span> moo< / span>< / div>
}

function(){include'foot.php'; }
?>

..然后在您的最终页面中调用以下函数:

uberpage



 <?php 

包含'ubertemplate.php';
?>

<?php head()?>
< script src = superlib.js>< / src>
<?php nav()?>

Yomama穿着一件黄色外套时这么大,人们都大喊大叫!

<?php foot()?>

最后,如果我理解正确,并且您认为超级页面方法似乎是个好主意,...那么你应该尝试一下。这可能是认识到它存在缺陷的唯一方法。我并不是想嘲笑你的想法,但有更好的方法来处理模板,第一种方法,你试图避免的方法是更清洁(但这只是我的看法)。

要获得更好的方法,请查看树枝模板或进行更多研究并找到适合您需求的框架。

I am wondering how would I include various elements (header, footer, nav), in one php file to be included on various pages, rather than creating multiple php files to be called separately?

In my includes file I have the following code:

<?php
echo '<h1 class="sub-elements">The elements for the current submission period are:<span style="font-weight:bold; color:#ff6000;"> Carnival, Residue, Maudlin</span>.</h1>';
?>

Outputting onto an html page that is running php I have:

<?php include('php-includes/current-elements.php');?>

Is there a way to include a particular div or class, something other than the entire file itself?

I just didnt want to create a separate php include file for every little element that i want to include on the same page.

The code below is what I had within the same include file to call a different element to be displayed on another page.

<?php
function elements ()
{
echo'<h1 class="sub-elements">The elements for the current submission period are:<span style="font-weight:bold; color:#ff6000;"> TESTING</span>.</h1>';
elements();
}
?>

Any help would be great!

解决方案

It sounds like you are trying to avoid templates. The basic idea is to define a separate file for each of head, footer, nav and include those from your content template.

head.php

<!doctype html> <html> ... 

nav.php

<body> <a href=..> page1 </a> <a href=..> page2 </a> <a href=..> page3 </a>

foot.php

</body> </html>

And to create a complete page, you would do:

<?php
include 'head.php';
include 'nav.php'

An article about prune juice.

include 'foot.php';
?>

Now, if I understand correctly, you want to include only one file and be able to generate the various elements (head, nav, whathaveyou). You could do this by wrapping the include statements in functions:

ubertemplate

<?php
function head() { include 'head.php'; }
function nav() { include 'nav.php'; }

function randomElement() { ?>
    An article about prune juice. <?php
}

function totalymisguideddynamiccontents() {
    echo "<div> foobar <span> moo </span></div>"
}

function() { include 'foot.php'; }
?>

..then in your final page, call these functions:

uberpage

<?php

include 'ubertemplate.php';
?>    

<?php head() ?>
<script src=superlib.js></src>
<?php nav() ?>

Yomama so big when she wears a yellow coat people yell TAXI!. 

<?php foot() ?>

Finally, if I understand correctly and you think the uberpage approach seems like a good idea, ...then you should probably try it out. That may well be the only way to realize that it is flawed. I don't mean to mock your idea, but there are better ways to approach templates and the first way, the one you are trying to avoid, is cleaner (but that's only my opinion).

For a better way, look at twig templates or do some more research and find a framework that suits your needs.

这篇关于如何在一个php包含文件中包含多个页面内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆