Tuesday, February 23, 2010

Simple Template Engine Basic Architecture

Hi Everybody,
[This is my re-post from another site that I have written an article with title

Simple Template Engine Basic Architecture

on Sun Aug 02, 2009 3:33 am ]
I would like to show the newbies a simple 15 line code, how a basic template engine in php works

create two folders in side your website as follows
    templates
    cache

Create index.php in your root folder and index.html inside template folder
Open index.php in your favorite editor [I use, netbeans or dream weaver] and add the following code and save. Make sure your cache folder has read / write permission

[code]
  1. <?php
  2. //Author: Najeem M Illyas, Chief Software Architect, tcc247.com
  3. $tpl_var = array('{DOC_TITLE}','{BODY_BGCOLOR}','{BODY_CONTENT}');
  4. $tpl_values = array('Test Title','#6633ff','This is my first template engine project<br /> -Najeem M Illyas');
  5. $fp = fopen('templates/index.html','r',false);
  6. $read_data = fread($fp,filesize('templates/index.html'));
  7. $new_data = str_replace($tpl_var, $tpl_values, $read_data);
  8. fclose($fp);
  9. if($fp = fopen('cache/index.html.php','w',true))
  10. {
  11. fwrite($fp,$new_data,(filesize('templates/index.html')+1024));
  12. fclose($fp);
  13. include_once('cache/index.html.php');
  14. }
  15. ?>

[/code]

Open index.html and add the following

[code]
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3. <head>
  4. <title>{DOC_TITLE}</title>
  5. <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
  6. </head>
  7.  
  8. <body bgcolor="{BODY_BGCOLOR}">
  9. {BODY_CONTENT}
  10. <br />
  11. This is html text
  12. </body>
  13. </html>

[/code]

Now the code:

The code in php is for reading the html file and making a simple buffer and then the template variables are replaced with array values in the buffer and finally writing the new data in the buffer to new file index.html.php. This file is included to the index.php file for demonstrating the final result. Now you can see the variables are replaced with values from array. This concept can be expanded to any desired level for making big systems like Smarty etc.

Run the index.php file and you will find generated file in cache folder.

Here we are utilizing the power of PHP.

Regards,
Najeem M Illyas
http://www.manchesterinfoservices.com/

Sunday, January 10, 2010

Zend Framework

Dear friends,

Zend Framework is one of the best PHP MVC frameworks from the PHP company, Zend. Zend framework has an advanced set of libraries capable of handling a lot of functionality in PHP. Zend framework reduces development time very drastically and reusable codes can be generated with Zend framework. Zend framework supports easy theming of application. Zend framework has namespace support and hence it is competing with languages like Java. Zend framework also supports url routing, multiple layout for multiple views etc. Zend library is extending and hence all new versions coming with all latest requirements. It includes gdata, twitter, flicker etc. It has library for videos , uploads and lot of other useful functionality.

Regards,

Najeem M Illyas

PHP Frameworks

Dear friends,

Here I'm trying to list and explain some PHP frameworks. There are variety of frameworks depends on the purpose.

    • Zend Framework
    • Symfony
    •  AgilePHP
    • Atomik Framework
    • CakePHP
    • Drupal
    • CodeIgniter
    • Joomla
    • Helix 
    • Horde 
    • e107 
    • eZ Components 
    • FUSE 
    • Kohana 
    • KumbiaPHP 
    • Mambo 
    • PEAR 
    • PHP For Applications 
    • PHP ActiveRecord 
    • PHP Work 
    • Qcodo 
    • QCubed 
    • Seagull
    • SilverStripe (Sapphire) 
    • SPIP 
    • Swiftlet 
    • Symfony 
    • TYPO3 
 All these opensource frameworks are very helpful in developing customized web application solutions, such as portals, news, cms, ecommerce and any level of extentions.
I'll explain some important frameworks in next post or in an update of this post. Your comments are welcomed.


You can ask questions or post any doubts in PHP based developments.


Regards,
Najeem M Illyas

PHP.net news & announcements

Most Viewed Videos

Latest CNN News

CNN.com - Video