2016-02-11 87 views
-2

也許它是重複的,但我不知道它是如何調用並找不到。我需要加載一個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 echo $ title; ?>它在哪裏說「我需要在這裏放置標題」,以及'include'template_file.php';'作爲你的函數的主體? – Kenney

+0

是的,它說:「我需要在這裏放置標題/內容」它需要由'$ title'和'$ content'從函數參數值替換 –

+0

對不起,我在noobie中使用php。你可以發佈代碼示例嗎? –

回答

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°解決方案:您需要實施TA功能來從外部源變量:

<?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'];假如你希望通過提交表單拿到冠軍

我沒有足夠的細節來告訴你更多。

+0

謝謝!我知道這些方法,但在我的情況下,這不是很好的解決方案,不幸的是。 –

相關問題