Php 如何加载页面模板并将内容放入?
也许它是重复的,但我不知道它如何调用,也找不到。我需要加载一个php文件(我的块的模板)并在其中设置内容。 例如,模板文件:Php 如何加载页面模板并将内容放入?,php,Php,也许它是重复的,但我不知道它如何调用,也找不到。我需要加载一个php文件(我的块的模板)并在其中设置内容。 例如,模板文件: <?php include_once 'boot.inc' ?> <div class="widget"> <div class="widget-title">I need to put title here</div> <div class="widget-body">I need to put
<?php include_once 'boot.inc' ?>
<div class="widget">
<div class="widget-title">I need to put title here</div>
<div class="widget-body">I need to put content here</div>
</div>
我可以用php实现吗?1°解决方案:您需要将代码更改为:
<?php include_once 'boot.inc' ?>
<?php include_once 'template.php' ?>
<div class="widget">
<div class="widget-title"><?php echo $title; ?></div>
<div class="widget-body"><?php echo $content; ?></div>
</div>
因为变量存储在template.php
<?php
function echoTitle(){
$title = 'Add code to get title here';
echo $title;
}
function echoContent(){
$content = 'Add code to get content here';
echo $content;
}
2°解决方案:您需要实现一个从外部源检索变量的函数:
<?php include_once 'boot.inc' ?>
<?php include 'functions.php' ?>
<div class="widget">
<div class="widget-title"><?php echoTitle(); ?></div>
<div class="widget-body"><?php echoContent(); ?></div>
</div>
将此处获取标题的添加代码
和此处获取内容的添加代码
替换为获取内容的代码,例如:
$title=$\u POST['title']代码>假设您希望通过提交表单获取标题
我没有足够的细节来告诉你更多。你有没有试过把
放在上面写着“我需要把标题放在这里”的地方,并且包括“template_file.php”
作为函数的主体?是的,当它说“我需要在这里放置标题/内容”时,它需要替换为函数ArgumentsOrry中的$title
和$content
值,我对php一无所知。你能发布代码示例吗?好吧,试试我的建议(我前面评论中的代码示例),看看会发生什么。顺便说一句,你可能想调查一下。是的,这很有效!谢谢你,兄弟!谢谢我知道这些方法,但不幸的是,对我来说,这不是一个很好的解决方案。
<?php include_once 'boot.inc' ?>
<?php include 'functions.php' ?>
<div class="widget">
<div class="widget-title"><?php echoTitle(); ?></div>
<div class="widget-body"><?php echoContent(); ?></div>
</div>
<?php
function echoTitle(){
$title = 'Add code to get title here';
echo $title;
}
function echoContent(){
$content = 'Add code to get content here';
echo $content;
}