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&text=CCCCCC&text_size=12&text_family=1&js=1&text_s_color=ff8800&text_s_weight=bold&text_s_style=normal&text_s_variant=normal&text_s_decoration=underline&text_s_color_h=ffae52&text_s_weight_h=bold&text_s_style_h=normal&text_s_variant_h=normal&text_s_decoration_h=underline&text_i_color=ff8800&text_i_weight=normal&text_i_style=italic&text_i_variant=normal&text_i_decoration=none&text_i_color_h=ffae52&text_i_weight_h=normal&text_i_style_h=italic&text_i_variant_h=normal&text_i_decoration_h=underline&text_c_color=CCCCCC&text_c_weight=normal&text_c_style=normal&text_c_variant=normal&text_c_decoration=underline&text_c_color_h=FFFFFF&text_c_weight_h=bold&text_c_style_h=normal&text_c_variant_h=normal&text_c_decoration_h=underline&text_u_color=ff8800&text_u_weight=bold&text_u_style=normal&text_u_variant=normal&text_u_decoration=none&text_u_color_h=ffae52&text_u_weight_h=bold&text_u_style_h=normal&text_u_variant_h=normal&text_u_decoration_h=underline"></script><noscript>Enable JavaScript or visit <a href="http://www.tsviewer.com/index.php?page=ts_viewer&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} {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 -->
2.586 views