Hallo beste mensen,

Sinds kort heb ik mijn template systeem werken dat op php draai en html bestanden gebruikt als view pages.
Dus ik dacht ik maak een preg_replace functie die statements zoals <!-- IF 1 == 1 --> Bla bla <!-- ENDIF --> veranderd in <?php if(1 == 1){?>Bla Bla<?php }?>
Ik kwam er toen achter dat je geen php in html bestanden kunt gebruiken. (Het kan wel maar dan moet je een .htaccess bestand maken, wat ik dus had gedaan. Maar dat werkte alleen bij losse html bestanden.)
Hoe kan ik dit dus wel laten werken.

Voor de duidelijkheid mijn scripts (Voor zo ver ze duidelijk zijn te beschouwen :P)

Functions.php;
<?php
class template
{
private $file = null;
private $error = null;
private $getFile = false;
private $html = null;
private $CSSfile = null;

// This function will check if the file if it exists and if it isn't empty. If it is it wil get the file.
public function outputfile($file = false)
{
if($file != null && $file != false)
{
if(!preg_match("/(.+?).html$/si",$file))
{
$this->error = "<b>Parse error:</b> The file has to be a html document!";
}
elseif(!file_exists($file))
{
$this->error = "<b>Parse error:</b> The file ".$file." does not exsist!";
}
$this->file = $file;
}
if(file_exists($this->file))
{
$this->html = file_get_contents($this->file);
$this->getFile = true;
}
}

// This function replaces statements.

public function htmlReplaces()
{
if($this->getFile == false)
{
$this->outputfile();
}

$this->html = preg_replace_callback('#\<\!-- INCLUDE (.+?).html --\>#',
create_function('$matches','return file_get_contents("styles/control/template/".$matches[1].".html");'),$this->html);

$match = preg_match('#\<\!-- IF D_(.+?) --\>#',$this->html,$matches);

$this->html = preg_replace('#\<\!-- IF D_(.+?) --\>#','<?php if($matches['1']){?>',$this->html);

$this->html = preg_replace('#\<\!-- ENDIF --\>#','<?php }?>',$this->html);
}

// This function replaces the language statements
public function language($langFile)
{
if($this->getFile == false)
{
$this->outputfile();
}
include($langFile);
foreach($lang as $key => $value)
{
$this->html = preg_replace("#\{L_".$key."\}#s",$value,$this->html);
}
}

public function assain_vars($vararray)
{
foreach ($vararray as $key => $value)
{
$this->html = preg_replace("#\{".$key."\}#s",$value,$this->html);
}
}

// This function parses the whole template
public function parse()
{
if($this->error == null)
{
if($this->getFile == false)
{
$this->outputfile();
}
return $this->html;
}
else
{
return $this->error;
}
}
}

Index.php:
<?php

// Devines the root path
$root_path = (defined('ROOT_PATH')) ? ROOT_PATH : './';
$phpEx = substr(strrchr(__FILE__, '.'), 1);

// Includes common.php
include_once($root_path.'common.'.$phpEx);



// Sets the output file
$template->outputfile("styles/control/template/index.html");

// Makes the html statements work
$template->htmlReplaces();

// Sets the language file
$template->language("language/nl/common.php");

// Some text grabbed from the database

$sql = mysql_query("SELECT * FROM `news_homepage`");
$news = mysql_fetch_array($sql);
$template->assain_vars(array(
"NEWS_TITLE" => $news['title'],
"NEWS_TEXT" => $news['text'],
"NEWS_AUTHOR" => $news['author'],
"NEWS_DATE_POST" => $news['date_post'],
"NEWS_DATE_EDIT" => $news['date_edit'],
"NEWS_TIME_POST" => $news['time_post'],
"NEWS_TIME_EDIT" => $news['time_edit']
));

// Echo's template
echo $template->parse();


?>

index.html:
<!-- INCLUDE overall_header.html -->
<link href="../theme/stylesheet.css" rel="stylesheet" type="text/css" />
<div class="main">
<div id="banners">
<div class="teamspeak">
<script type="text/javascript" charset="utf-8" src="http://www.tsviewer.com/ts3viewer.php?ID=940957&amp;text=CCCCCC&amp;text_size=12&amp;text_family=1&amp;js=1&amp;text_s_color=ff8800&amp;text_s_weight=bold&amp;text_s_style=normal&amp;text_s_variant=normal&amp;text_s_decoration=underline&amp;text_s_color_h=ffae52&amp;text_s_weight_h=bold&amp;text_s_style_h=normal&amp;text_s_variant_h=normal&amp;text_s_decoration_h=underline&amp;text_i_color=ff8800&amp;text_i_weight=normal&amp;text_i_style=italic&amp;text_i_variant=normal&amp;text_i_decoration=none&amp;text_i_color_h=ffae52&amp;text_i_weight_h=normal&amp;text_i_style_h=italic&amp;text_i_variant_h=normal&amp;text_i_decoration_h=underline&amp;text_c_color=CCCCCC&amp;text_c_weight=normal&amp;text_c_style=normal&amp;text_c_variant=normal&amp;text_c_decoration=underline&amp;text_c_color_h=FFFFFF&amp;text_c_weight_h=bold&amp;text_c_style_h=normal&amp;text_c_variant_h=normal&amp;text_c_decoration_h=underline&amp;text_u_color=ff8800&amp;text_u_weight=bold&amp;text_u_style=normal&amp;text_u_variant=normal&amp;text_u_decoration=none&amp;text_u_color_h=ffae52&amp;text_u_weight_h=bold&amp;text_u_style_h=normal&amp;text_u_variant_h=normal&amp;text_u_decoration_h=underline"></script><noscript>Enable JavaScript or visit <a href="http://www.tsviewer.com/index.php?page=ts_viewer&amp;ID=940957">TeamSpeak Viewer</a> to display the TeamSpeak server.</noscript>
</div>
</div>
<div class="news">
<span class="corners-top"><span></span></span>
{NEWS_TITLE}<br />{NEWS_TEXT}
<div class="post_date">
{L_POSTED_BY}: {NEWS_AUTHOR} &nbsp;&nbsp;{NEWS_DATE_POST}, {NEWS_TIME_POST}<br />
<!-- IF D_1 == 1 -->
{L_LAST_EDIT}: {NEWS_DATE_EDIT}, {NEWS_TIME_EDIT}
<!-- ENDIF -->
</div>
<span class="corners-bottom"><span></span></span>
</div>
</div>
<!-- INCLUDE overall_footer.html -->
Daniel Dorgelo op 25/11/2010 19:16:14

... Sinds kort heb ik mijn template systeem werken ...


Welk template systeem?
Zelf geschreven? Iets bestaands?
Waarom dan toch html bestanden? is het dan niet gewoon makkelijker om je template-systeem te baseren op php bestanden?

Ik zie zo snel geen fout waarom het niet zal werken, misschien dat ik zo nog even voor je zal spitten!
Ik heb het geschreven deels met behulp van deze handige site :p en verder zelf functions toegevoegd etc.

Verder wil ik graag HTML uitvoerpagina's gebruiken want dan heb ik mijn soorten code's (PHP en HTML)mooi gescheiden heb en het leek mij net wat zakelijker staan/werken.
Onze oplossing is: phtml bestanden! Hier staat onze HTML in, met af en toe wat PHP:

<table class="details">
<tbody>
<tr>
<th><?php echo gettext( 'Name' ) ?>:</th>
<td><?php echo $this -> shop -> getName( ) ?></td>
</tr>
</tbody>
</table>
Aha en als ik geen phtml zou willen gebruiken. Is er dan een andere oplossing?

EDIT: Bij phtml zien mijn pages er ander uit dan bij gewoon html wat ik strange vind :S

En dan nog een vraag. Ik heb een teamviewer banner rechts van mijn pagina in een div die op float right staat. maar daardoor steekt hij onder uit de body door mijn footer heen. Voorbeeld: http://www.control-esports.nl/
Je bedoeld teamspeak?

Heb je hem in ee. Tabelstaand

RewriteEngine on
RewriteBase /
RewriteRule ^(.*)\.html$ $1.php [L]
Ja ik bedoelde teamspeak ja :P en nee het staat niet in een tabel want ik wilde namelijk van alle tables af. Het is nu een floating div (Dus float: right; in css)

En vraag aan Joey: Moet ik dit in een php script verwerken of in een .htaccess bestand?
Daniel Dorgelo op 26/11/2010 19:33:47

Ja ik bedoelde teamspeak ja :P en nee het staat niet in een tabel want ik wilde namelijk van alle tables af. Het is nu een floating div (Dus float: right; in css)

En vraag aan Joey: Moet ik dit in een php script verwerken of in een .htaccess bestand?


Inderdaad, je moet tabellen wegdoen.
Die zooi van Joey moet in een .htaccess, die 'herschrijft' de naam van bestanden. Dus als je met je browser naar index.html gaat, dan wordt index.php gepakt.
Ook kan je nog deze regels toevoegen:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
Die zorgen ervoor dat als het bestand echt bestaat, of als de directory bestaat, dat die dan niet herschreven wordt. Ook kan het nog zijn dat je / voor je paths naar bijvoorbeeld het css bestand moet zetten.

Maar je punt is dus dat de view bestanden html zijn en dat daarin php niet werkt (wat correct is). Waarom kan je niet gewoon php bestanden pakken...? Kijk nog maar eens naar je code.

Verder, selecteer altijd wat je wilt hebben gebruik geen *.
In SQL horen geen backticks (`).
Gebruik [php]mysql_fetch_assoc[/php], niet [php]mysql_fetch_array[/php].
Bouw foutafhandeling in.
Aha bedankt voor alle info tot dusver, maar ik ben er achter gekomen dat het php bestanden ook niet werkt. Dit komt denk ik omdat ik de functie file_get_contetns gebruik.

Reageren