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


How to load page template and put content into?

也许它是重复的,但我不知道它是如何调用的,也找不到。我需要加载一个 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° 解决方案:您需要将代码更改为:

<?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替换为代码以获取内容,例如:

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

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