查看源代碼時,爲什麼我的HTML格式太差?爲什麼使用Smarty呈現時,我的HTML格式很差?
與合作:
的index.php:
<?php
// load required files
require 'class/Slim/Slim.php';
require 'class/RedBean/rb.php';
// register slim auto-loader
\Slim\Slim::registerAutoloader();
// set up database connection
R::setup('mysql:host=localhost;dbname=slimcms','root','');
R::freeze(true);
// initialize app
$app = new \Slim\Slim(array(
'mode' => 'development'
,'debug' => true
,'view' => new \Slim\Views\Smarty()
,'templates.path' => './templates'
));
$view = $app->view();
$view->parserDirectory = dirname(__FILE__) . '/class/Smarty/';
$view->parserCompileDirectory = dirname(__FILE__) . '/compiled';
$view->parserCacheDirectory = dirname(__FILE__) . '/cache';
// handle GET request for index
$app->get('/', function() use ($app){
$books = R::findAll('book');
//print_r($books);
$app->render('home.tpl',array('books'=>$books));
});
$app->run();
模板/ home.tpl:
<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>Home</title>
</head>
<body>
<div id="content">
{foreach name=aussen item=book from=$books}
{foreach key=key item=value from=$book}
{if $key == 'id' }
<a href="{$key}/{$value}">{$key}</a>
{else}{$key}{/if}
{/foreach}
<hr />
{/foreach}
</div>
</body>
</html>
當我觀看通過鍍鉻源代碼:
<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>Home</title>
</head>
<body>
<div id="content">
<a href="id/1">id</a>
rating price title <hr />
<a href="id/2">id</a>
rating price title <hr />
</div>
</body>
</html>
我會預期:
<!DOCTYPE html>
<html lang="en">
<head>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type">
<title>Home</title>
</head>
<body>
<div id="content">
<a href="id/1">id</a> rating price title
<hr>
<a href="id/2">id</a> rating price title
<hr>
</div>
</body>
</html>
除了調試,美化格式化HTML的實用目的是什麼?即使對於調試,你也不需要它。 – axiac
同意@axiac,讓HTML變得毫無意義。瀏覽器必須閱讀它,而不是你。 – halfer