2012-04-12 14 views
1

Ich erstelle eine dynamische Website mit PHP/MySQL. Meine Vorlage für Druckdaten mit reinem PHP-Code + HTML. jetzt, besser, optimiert, schneller weg für Struktur von mix php + html? (Ohne Template-Engine)mischen HTML-Vorlage mit PHP-Code: PHP HTML-Vorlage

für jede Seite ich habe: V1 (PHP-Code vor Wrapper)

<?PHP include (DEFINE_SITE . '/templates/header.php'); 

// isset : post : get : SELECT : INSERT ... Or ANY CODE OF PHP 
?> 

<div id="wrapper"> 
<div id="leftsidebar"><?PHP // Left DATA ?></div> 
<div id="centercontent"> 

<?PHP // while {} Print result of php ANY LOOPS ..... ?> 

</div> 
<div id="rightsidebar"><?PHP // Right DATA ?></div> 
</div> 
<?PHP include (DEFINE_SITE . '/templates/footer.php'); ?> 

für jede Seite ich habe: Ex.2 (nach Seitenleiste und vor Zentrum Inhalt)

<?PHP include (DEFINE_SITE . '/templates/header.php'); 

<div id="wrapper"> 
<div id="leftsidebar"><?PHP // Left DATA ?></div> 
<?PHP // isset : post : get : SELECT : INSERT ... Or ANY CODE OF PHP ?> 
<div id="centercontent"> 

<?PHP // while {} Print result of php ANY LOOPS ..... ?> 

</div> 
<div id="rightsidebar"><?PHP // Right DATA ?></div> 
</div> 

<?PHP include (DEFINE_SITE . '/templates/footer.php');?> 

Was ist besser? e.x 1 oder e.x 2? deine Meinung ?

+0

Besser in welchem ​​Kontext **? –

+0

optimierte Struktur, schnelleres Laden. – BBKing

+2

Definieren Sie die optimierte Struktur. Auch schnelleres Laden? Sie sind im Grunde gleich. Wenn überhaupt, werden Sie keine Millisekunde zwischen den beiden abrasieren. Wenn Sie Leistung wünschen, verwenden Sie nicht "include", was das Betriebssystem dazu zwingt, das "stat" für die Datei auszuführen. Sie sollten jedoch niemals (minimale) Leistung aus Gründen der Klarheit und Wartbarkeit des Codes handeln. –

Antwort

0

Warum die Abneigung gegen eine Vorlage-Engine? In Wirklichkeit ist PHP eine Vorlagen-Engine, aber vielleicht, wenn Sie ein paar auschecken (wie Twig), könnte es Ihnen helfen, etwas flexibler, schneller und reaktionsfähiger zu gestalten, ohne die Template-Engine .... oder Sie könnten es lieben, wie ich ;-)

+1

yeap! Twig ist das beste Template-Engine Nach RainTPL, Smarty mit Cache-Unterstützung. Aber ich verwende es gerade. – BBKing

0

Ich schlage vor, dass Sie Ihre HTML und PHP getrennt halten. Wenn Sie kein Vorlagensystem verwenden möchten, warum tun Sie das nicht so?

eine HTML-Vorlage bilden:

<div id="wrapper"> 
    <div id="leftsidebar">{LEFT}</div> 
    <div id="centercontent">{CONTENT}</div> 
    <div id="rightsidebar">{RIGHT}</div> 
</div> 

In einer anderen PHP-Datei:

$content = file_get_contents('template.html'); 
$templateVars = array(); 

startVar('CONTENT'); 
echo '<p>This is the content.</p>'; // New template vars are also allowed 
endVar('CONTENT'); 

echo replaceTemplateVars($content); 

/** 
* Replace template variables recursively with the key-value pairs that are in the $templateVars queue 
* @param string $content (default NULL) is code to convert. 
* @return the string that is replaced 
*/ 
function replaceTemplateVars($content = NULL) 
{ 
    global $templateVars; 
    $matches = array(); 
    preg_match_all("/{[A-Z0-9_:]*}/", $content, $matches); // $matches = All vars found in your template 
    foreach($matches[0] as $key) 
    { 
     if(isset($templateVars[substr($key, 1, -1)])) 
     { 
      $content = str_replace($key, $templateVars[substr($key, 1, -1)], $content); // Substitute template var with contents 
     } 
     else 
     { 
      $content = str_replace($key, "", $content); // Remove empty template var from template 
     } 
    } 

    // Check if the replacement has entered any new template variables, if so go recursive and replace these too 
    if(preg_match_all("/{[A-Z0-9_:]*}/", $content, $matches)) 
    { 
     $content = replaceTemplateVars($content); 
    } 

    return $content; 
} 

/** 
* Start output buffer for a template key 
* @param string $key is not used. Only for overview purposes 
*/ 
function startVar($key) 
{ 
    ob_start(); 
} 

/** 
* End output buffer for a template key and store the contents of the output buffer in the template key 
* @param string $key 
*/ 
function endVar($key) 
{ 
    setVar($key, ob_get_contents()); 
    ob_end_clean(); 
} 

/** 
* @param string $key to store value in 
* @param mixed $value to be stored 
*/ 
function setVar($key, $value) 
{ 
    global $templateVars; 
    $templateVars[$key] = $value; 
} 

Und das ist, was Sie auf Ihrem Bildschirm erhalten:

<div id="wrapper"> 
    <div id="leftsidebar"></div> 
    <div id="centercontent"><p>This is the content.</p></div> 
    <div id="rightsidebar"></div> 
</div> 

So Vorverarbeitung erfolgt Zuerst und am Ende wird der gesamte HTML-Code ausgegeben.

Natürlich könnten Sie Funktionen wie startPrependVar() und startAppendVar() hinzufügen und alles in eine Template-Klasse einfügen, um den globalen Gültigkeitsbereich zu umgehen.