1

I am working with a pretty simple php template right now, stored in one index.php and fetch the files via a GET and access the files like: http://mydomain.com/index.php?page=news if the file is called news.php. See the code snippet below.

 <?
        $fileend = ".php";
        $file="{$_GET["page"]}$fileend";
        $news="news.php";
        if(file_exists($file))
            include($file);
        else
            include($news);
    ?>

Now I want to use a templage engine where I can have different tags for each page, still I don't want to change my file system. Right now my php files looks like this:

<h1>Page Title</h1>
<p>Page Content</p>

I don't use a header as the files are included in my index.php.

I have looked at TinyButStrong and other engines. But I don't know how I should proceed. It should be quite easy. Please let me know how I can do when I don't want to change a lot in my file structure and keep it simple.

asheeshr
  • 4,088
  • 6
  • 31
  • 50
  • Please be aware that your current application is vulnerable to file inclusion. http://en.wikipedia.org/wiki/Remote_file_inclusion – Leon Apr 01 '11 at 10:51

3 Answers3

1

Try the smarty template engine. It is easy to use and you can use a dedicated template folder. I don't know whether you will have to change a lot because I don't know your file structure ;-)

strauberry
  • 4,189
  • 5
  • 34
  • 50
  • Thanks for your answer. I know smarty but I don't know if it's the best solution. My file structure is pretty simple. I have the files in the main folder and in subdirectories. I want to access my files simply through typing the url index.php?page=2011/page1 if the file is in the subdirectory 2011. I don't want to create a .tpl file for every page but I still want to have different title tags in the header. – theproximityeffect Apr 01 '11 at 09:43
  • You can use html files also in smarty and use $smarty->display($_GET['yourTemplateFile']); But there may exist better engines of course. – strauberry Apr 01 '11 at 09:51
0

Another template engine what you can use is Separate (see http://separate.esud.info/). It is possible to include files with

<!-- INCLUDE filename --> 
Thomas
  • 1
  • 1
0

You might give Zend_Layout and Zend_View a try. You don't need to use the MVC part of Zend Framework for this. Biggest plus when using these 2 classes is that templates are simply PHP files. You don't have to use Zend_Layout though, but when working with layouts it does make things easier.

Htbaa
  • 2,319
  • 18
  • 28
  • So, need I a template for every singe file or is it enough with one main template (index.php) where all my other files are included? – theproximityeffect Apr 01 '11 at 10:38
  • You can have your index.php be the entry point which decides which template (individual files) to include. Don't stuff everything together in 1 file, it makes maintenance a living hell. – Htbaa Apr 01 '11 at 11:08