用php动态组合页面

时间:2017-02-16 08:16:02

标签: php templates

所以,我正在建立一个有两种语言的网站,我使用php来整理页眉,页脚和模板。我有这样的代码:

$lang = "en"; //name of a folder
$section = "start"; //name of a template in "en" folder

$content = explode("/", $_SERVER['REQUEST_URI']); // for example, www.example.en/folder/en/template

$url = $content[3];

if (!empty($content[2]) && (in_array($content[2], $languages))) { $lang = $content[2]; }
if (!empty($content[3]) && (in_array($content[3], $sections))) { $section = $content[3]; } //

$sectionTitles = $sectionTitles[$lang];

require_once ("header.php");
require_once ("templates/$lang/$section.php");
require_once ("footer.php");

我有疑问:为了在$ lang(本例中为en)文件夹下使用子文件夹,我如何修改此代码?例如,www.example.en / folder / en / blogentries / template?

提前谢谢!

1 个答案:

答案 0 :(得分:0)

@Mohammad的帮助下,我做了以下事情:

if(sizeof($content) == 5){

    $url = $content[4];

    if (!empty($content[2]) && (in_array($content[2], $languages))) { $lang = $content[2];}
    if (!empty($content[4]) && (in_array($content[4], $sections))) { $section = $content[4];}

    require_once ("header.php");
    require_once ("templates/$lang/subfolder/$section.php");
    require_once ("footer.php");
} else {

    $url = $content[3];

    if (!empty($content[2]) && (in_array($content[2], $languages))) { $lang = $content[2];}
    if (!empty($content[3]) && (in_array($content[3], $sections))) { $section = $content[3];}

    require_once ("header.php");
    require_once ("templates/$lang/$section.php");
    require_once ("footer.php");
}

希望这也有助于其他人。