如何加载页面模板并将内容放入?

时间:2016-02-11 22:49:49

标签: 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 content here</div>
</div>

使用此模板插入新块的php函数:

function nm_new_block($title, $content) {};

我可以在php中完成吗?

1 个答案:

答案 0 :(得分:2)

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
    $title="My Title";
    $content="My Content";
?>

并且您不需要此功能,因为变量存储在template.php

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>

functions.php

<?php
function echoTitle(){
    $title = 'Add code to get title here';
    echo $title;
}
function echoContent(){
    $content = 'Add code to get content here';
    echo $content;
}

Add code to get title hereAdd code to get content here替换为代码以获取内容ex:

$title = $_POST['title'];假设您希望通过提交表单获取标题

我没有足够的细节告诉你更多。