как да изкарам html кода :(

XxLPxX

Registered
index.php
<?php
define('index.php',true);
require('classes/body.php');
$obj=new Site;
$obj->connect();
$obj->header('Уеб класация');
$obj->head_title='Твоята уеб класация';
$obj->body_index();
$obj->footer();

?>

body.php
<?php
class site{
protected $host='localhost';
protected $user='root';
protected $pass='';
protected $db='directory';
public function connect(){
mysql_connect($this->host,$this->user,$this->pass);
mysql_select_db($this->db);
}
var $head_title;
public function header($title){
echo'<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>'.$title.'</title>
</head>
<style>
body{margin:0px;background:#999;}
.header{width:700px;height:100px;border:1px #444 dashed;background:#888;text-shadow:#fff 2px 2px 5px}
.menu{width:700px;height:30px;border:1px #444 dashed;background:#888}
.footer{width:700px;height:30px;border:1px #444 dashed;background:#888;line-height:30px;}
.button{width:100px;height:30px;float:left;background:#777;line-height:30px;font-family:verdana;font-size:12px;text-shadow:#fff 1px 1px 5px}
.button:hover{background:#f60;}
a{text-decoration:none;color:#000;}
a:hover{text-decoration:none;color:#000;}
a:active{text-decoration:none;color:#000;}
.content{float:left;height:400px;background:#888;border:1px #444 dashed;text-shadow:#fff 1px 1px 5px}
.category{width:180px;float:left;margin-left:20px;height:400px;background:#888;border:1px #444 dashed;text-shadow:#fff 1px 1px 5px}
.wrapper{width:704px;}
</style>
<body>
';
}
protected $link1='Начало';
protected $link2='Топ 100';
protected $link3='Категории';
protected $link4='Добави сайт';
protected $link5='Статистика';
protected $link6='Най-нови';
protected $link7='Контакти';
public function body_index(){
include("/includes/layout/".$this->content[0]);

}
public $content=array('index.php','add_site.php','most_new.php','contacts.php','categories.php','view_cat.php');

function body($title){
echo'<center><div class="header"><h2>'.$this->head_title.'</h2></div><br/>
<div class="menu">
<div class="button"><a href="#">'.$this->link1.'</a></div>
<div class="button"><a href="#">'.$this->link2.'</a></div>
<div class="button"><a href="#">'.$this->link3.'</a></div>
<div class="button"><a href="#">'.$this->link4.'</a></div>
<div class="button"><a href="#">'.$this->link5.'</a></div>
<div class="button"><a href="#">'.$this->link6.'</a></div>
<div class="button"><a href="#">'.$this->link7.'</a></div>
</div><br/><div class="wrapper">
<div class="content" style="width:700px">'.$title.
include('includes/layout/'.$this->content[0]);
'</div>';

}


public function footer(){
echo'<div style="clear:both"></div><br/><div class="footer">© All rights reserved.</div>';
}

}
?>

index.php (includes/layout)
<?php
require('includes/layout/menu.php');
if(defined('index.php'))
{
$style='style="width:500px"';
}
else
{
$style='style="width:700px"';
}
echo'<div class="wrapper">
<div class="content" '.$style.'>Топ сайтове
</div>';
if(defined('index.php'))
{
echo'<div class="category">Категории</div></div>';
}

?>

menu.php
<?php
echo'<center><div class="header"><h2>'.$this->head_title.'</h2></div><br/>
<div class="menu">
<div class="button"><a href="#">'.$this->link1.'</a></div>
<div class="button"><a href="#">'.$this->link2.'</a></div>
<div class="button"><a href="#">'.$this->link3.'</a></div>
<div class="button"><a href="#">'.$this->link4.'</a></div>
<div class="button"><a href="#">'.$this->link5.'</a></div>
<div class="button"><a href="#">'.$this->link6.'</a></div>
<div class="button"><a href="#">'.$this->link7.'</a></div>
</div><br/>';
?>

Опитах се да изкарам html извън класовете , тоест във class site да има колкото се може по малко html код , но не мога :(
Съжалявам за дългия код :(
 
Привет,

Да попитам първо: защо държиш този код да се генерира от класа, а не да е част от index.php примерно? Иначе се сещам за няколко начина, що се отнася до желанието ти кодът на класа да е по-пригледен, без HTML код в него.

Чрез допълнителни, външни функции, които генерират HTML, а класът само извиква функциите:

html.php
Код:
<?php
function generate_header($title) {
?>
<!DOCTYPE...>...
<title><?php echo($title) ?></title>...
<?php
}

index.php
Код:
<?php
require('html.php');
class Site {
    public function header($title) {
        generate_header($title);
        return true;
    }
}

$site = new Site();
$site->header('Уеб класация');
?>

Чрез вмъкване на не-PHP файлове, съдържащи HTML-а (могат да вземат данни чрез дефинирани константи или променливи):

header.php
Код:
<!DOCTYPE...>...
<title><?php echo(HTML_TITLE) ?></title>...

index.php
Код:
class Site {
    public function header($title) {
        if ($title) define('HTML_TITLE', $title);
        else define('HTML_TITLE', 'My Site');
        require('header.html');
   }
}

$site = new Site();
$site->header('Уеб класация');

Първото предложение има неудобството да вика функции, които да викат функции, които да извеждат HTML. Второто предполага няколко малки файла, които да съдържат различни части от HTML-а.
Разбира се, аз лично бих предпочел болшинството от HTML-кода да си ми е в index.php, а вече по-динамичната част да се генерира от site класа, но ти си знаеш най-добре как ще ти е най-удобно и т.н.

Ако не ти харесват тези идеи или не съм разбрал напълно какво ти е нужно - пиши, пък ще го мъдрим още (а междувременно и други може да се включат към темата). :)
 
Мерси много за идеята , проблема е , че ми е малко трудно да я схвана ..
Целта ми е да няма html кодове в тези файлове където са класовете .. и този html да се отдели на нов файл . :)
 
XxLPxX каза:
Мерси много за идеята , проблема е , че ми е малко трудно да я схвана ..
Целта ми е да няма html кодове в тези файлове където са класовете .. и този html да се отдели на нов файл . :)
http://smarty.myblog.bg/
естествено има и други статии за smarty, които можеш да разгледаш
 
Мерси много :) може ли повече линкове за смарти на ЛС , получаваш точка :)
 

Back
Горе