duanbiao4025 2013-01-04 13:10
浏览 34
已采纳

php模板和html整洁[关闭]

Would this technically pass as a basic level template system in php?

So there are two files, index.php and Template_Module.php

Here is index.php code

<?php

include("Template_Module.php");

$set_name = "My Website";
$set_desc = "My Website for random bla bla";
$set_key  = "site, random, bla, for";

echo template_header_object($set_name, $set_desc, $set_key);

and here is Template_Module.php code

<?php

function template_header_object($set_title, $set_desc, $set_keywords) {

     $title_object = htmlspecialchars($set_title);    //lets get html title to pass
     $desc_object  = htmlspecialchars($set_desc);     //lets get meta description to pass
     $keyw_object  = htmlspecialchars($set_keywords); //lets get meta keywords to pass

     $template_create_object .= <<<OBJECT
<!doctype html>
     <html>
         <head>
             <meta charset="utf-8">
                 <title>$title_object</title>
    <!-- general meta -->
             <meta name="description" content="$desc_object">
             <meta name="keywords" content="$keyw_object">

    <!-- mobile specific meta -->
             <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">           

    <!-- open graph meta -->
             <meta property="og:title" content="$title_object">
             <meta property="og:image" content="/assets/img/logo_fbtype.png">
             <meta property="og:site_name" content="$title_object">
             <meta property="og:description" content="$desc_object">

    <!-- style sheets and scripts -->
             <link href="/assets/css/style.css" rel="stylesheet">
             <link href="/assets/css/reset.css" rel="stylesheet">
             <link href="/assets/img/favicon.png" rel="shortcut icon">

                <!--[if lt IE 9]>
                <script src="http://html5shim.googlecode.com/svn/trunk/html5.js"></script>
                <![endif]-->

             <script src="http://code.jquery.com/jquery-latest.js"></script>
         </head>
     <body></body></html>        
OBJECT;

     return $template_create_object;

}

And is there a way I can improve the way html is rendered to browser, so the source code looks nicer instead of being completely off from how it looks at $template_create_object .= <<<OBJECT and is there a better way for passing values from index.php to template_object.php

  • 写回答

1条回答 默认 最新

  • dongsunny1113 2013-01-04 14:51
    关注
    1. Yes, it's a perfectly good way of doing a basic template in PHP. It's very basic, and doesn't offer much flexibility, but it's a quick and easy starting point. If you don't need anything more complex, it'll suit you fine. (If you do need something more complex, there are plenty of open source PHP templating packages you can use)

    2. Seriously, don't worry about how the finished HTML code looks. You shouldn't need to look at it anyway (If you need to debug it, use the browser's DOM view; it's a much easier way to work with it than viewing the source, even if the source if neat and tidy).

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?