簡體   English   中英

PHP模板幫助

[英]PHP Template Help

因此,我相信我先前的問題對我想要的東西是完全錯誤的。 我很抱歉。

無論如何,這是我認為會帶來問題的問題:我目前正在使用PHP模板(我相信這是正確的措辭)。 它被命名為“ index.php”,它包含我的布局以及以下代碼,該代碼從名為/ content /的目錄中調用正文的內容,例如“關於”,“聯系人”等。

<?php  
$default = 'index'; //Whatever default page you want to display if the file doesn't exist or you've just arrived to the home page.  
$page = isset($_GET['p']) ? $_GET['p'] : $default; //Checks if ?p is set, and puts the page in and if not, it goes to the default page.  
$page = basename($page); //Gets the page name only, and no directories.  
if (!file_exists('content/'.$page.'.php'))    { //Checks if the file doesn't exist  
    $page = $default; //If it doesn't, it'll revert back to the default page  
    //NOTE: Alternatively, you can make up a 404 page, and replace $default with whatever the page name is. Make sure it's still in the inc/ directory.  
}  
include('content/'.$page.'.php'); //And now it's on your page!  
?>  

上面的代碼在模板的index.php頁面中,它調用主體內容,如我之前提到的,例如“關於”,“聯系”等。現在,模板的頁面index.php稱為默認頁面“索引”。 php”(來自“ / content”目錄)。 但是,我希望我的主要index.php(而不是從/ content /調用的那個)具有與網站其余部分不同的布局。 在仍然能夠對使用不同布局的內容使用上述編碼的同時,我如何實現呢?

...這有意義嗎? 還是我只是在胡言亂語? -幫助將不勝感激。

只需創建一個條件:

if ($page == $default) {
    //different layout
}
else {
    ....
}

整個頁面的布局是否不同? 還是隨着網站內容的更改,頁眉和頁腳保持不變?

如果是后者,則可以這樣設置:兩個文件(例如“ header.php”和“ footer.php”)包含您的標頭代碼和頁腳代碼,並且它們之間有一個內容div。 您可以根據所處的頁面來打印進入內容div的所有內容,其方式類似於在上面設置代碼的方式。

那有意義嗎? 因此,您將擁有三個不同的包含文件,一個用於頁眉,一個用於頁腳,一個用於頁面內容(取決於您所在的頁面)

如果我理解正確,您只想從根目錄index.php切換布局,如果有人單擊您的鏈接,您將向根目錄index.php傳遞一個“ p”參數,該參數通過從目錄中加載文件來更改整個布局。 / content文件夾。

<?php  
    if( isset($_GET['p'] ) ) {
        $default = 'index';
        if (!file_exists('content/'.$_GET['p'].'.php')) {  
            $page = $default;
        }
        $page = basename($page);
        include('content/'.$page.'.php'); //And now it's on your page! 

    } else {
            // your root index.php
    }
 ?>  

將其放在根index.php的頂部,並在else條件下構建根index.php站點。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM