HTML if en while statements maken in een template systeem.

Overzicht Reageren

Sponsored by: Vacatures door Monsterboard

Daniel Dorgelo

Daniel Dorgelo

25/11/2010 19:16:14
Quote Anchor link
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
Code (php)
PHP script in nieuw venster Selecteer het PHP script
1
<?php if(1 == 1){?>
Bla Bla
Code (php)
PHP script in nieuw venster Selecteer het PHP script
1
<?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;
Code (php)
PHP script in nieuw venster Selecteer het PHP script
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
<?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:
[
code]<?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 -->
Gewijzigd op 25/11/2010 21:29:51 door Daniel Dorgelo
 
PHP hulp

PHP hulp

20/04/2024 07:45:43
 
Kris Peeters

Kris Peeters

26/11/2010 14:57:09
Quote Anchor link
Daniel Dorgelo op 25/11/2010 19:16:14:
... Sinds kort heb ik mijn template systeem werken ...


Welk template systeem?
Zelf geschreven? Iets bestaands?
 
Joran den Houting

Joran den Houting

26/11/2010 15:01:39
Quote Anchor link
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!
 
Daniel Dorgelo

Daniel Dorgelo

26/11/2010 16:56:48
Quote Anchor link
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.
 
Peter Dorn

Peter Dorn

26/11/2010 17:01:22
Quote Anchor link
Onze oplossing is: phtml bestanden! Hier staat onze HTML in, met af en toe wat PHP:

<table class="details">
<tbody>
<tr>
<th>
Code (php)
PHP script in nieuw venster Selecteer het PHP script
1
<?php echo gettext( 'Name' ) ?>
:</th>
<td>
Code (php)
PHP script in nieuw venster Selecteer het PHP script
1
<?php echo $this -> shop -> getName( ) ?>
</td>
</tr>
</tbody>
</table>
 
Daniel Dorgelo

Daniel Dorgelo

26/11/2010 17:28:45
Quote Anchor link
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/
Gewijzigd op 26/11/2010 17:38:24 door Daniel Dorgelo
 
Jordi Kroon

Jordi Kroon

26/11/2010 18:36:33
Quote Anchor link
Je bedoeld teamspeak?

Heb je hem in ee. Tabelstaand
 
Joey Drieling

Joey Drieling

26/11/2010 18:51:01
Quote Anchor link
Code (php)
PHP script in nieuw venster Selecteer het PHP script
1
2
3
RewriteEngine on
RewriteBase /
RewriteRule ^(.*)\.html$ $1.php [L]
 
Daniel Dorgelo

Daniel Dorgelo

26/11/2010 19:33:47
Quote Anchor link
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?
 

26/11/2010 19:48:18
Quote Anchor link
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 mysql_fetch_assoc, niet mysql_fetch_array.
Bouw foutafhandeling in.
Gewijzigd op 26/11/2010 19:49:54 door
 
Daniel Dorgelo

Daniel Dorgelo

26/11/2010 23:19:25
Quote Anchor link
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.
 



Overzicht Reageren

 
 

Om de gebruiksvriendelijkheid van onze website en diensten te optimaliseren maken wij gebruik van cookies. Deze cookies gebruiken wij voor functionaliteiten, analytische gegevens en marketing doeleinden. U vindt meer informatie in onze privacy statement.