dongqian0763 2012-05-31 08:46
浏览 7
已采纳

PHP - 最佳实践 - 网站模板[关闭]

I just wanted to see if the way I am constructing my website templates was the best and most dynamic way of doing it. Currently I have my website playout however for the areas that I want the content to change I use the following code.

<? 
 if (preg_match("/\W/",$page) || !file_exists("../$page.html")) {
  include("../home.html");
 }
 else {
  include("../$page.html");
 }
?>

The navigation then links to that page by saying something like,

<a href="index.php?page=about">About</a>

Is this the best way to construct a template using PHP or are there better ways? Thankyou for the help.

  • 写回答

2条回答 默认 最新

  • doubianyan9749 2012-05-31 09:02
    关注

    There is no best or most dynamic way of creating website templates.

    All you can do is learn from experience and others.

    Your code is one way to switch between templates. Some immediate improvements could be:

    • Use && instead of || because of short circuiting properties of the AND operator. If the page does not match then there is no need to do the file check

    • Use a array lookup table to map page value to filename template (e.g. 'home'->'homepage.html' etc.), mainly this helps in preventing serious exploits where external input is used for file paths.

    • Don't file check because why have a link with a non existent template?

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 VS2022创建MVC framework提示:预安装的程序包具有对缺少的注册表值的引用
  • ¥15 weditor无法连接模拟器Local server not started, start with?
  • ¥20 6-3 String类定义
  • ¥15 嵌入式--定时器使用
  • ¥20 51单片机学习中的问题
  • ¥30 Windows Server 2016利用兩張網卡處理兩個不同網絡
  • ¥15 Python中knn问题
  • ¥15 使用C#,asp.net读取Excel文件并保存到Oracle数据库
  • ¥15 C# datagridview 单元格显示进度及值
  • ¥15 thinkphp6配合social login单点登录问题