Jump to content
xisto Community
Sign in to follow this  
Ahsaniqbalkmc

How To Create Custom Tags?

Recommended Posts

I am trying to get deeper into the PHP and improve my scripting skills so that I can develop better websites that are not mere static pieces of content but interactive applications as well. One problem I recently ran into was the need of simplifying the process of link building. In a normal website, each time you have to create a link to an internal page, you will have to type the relative or absolute address of that page. It is fine but I need to simplify the process.If you have edited a wikipedia article, you might know how they have simplified the process of link building to internal pages. Instead of typing <a href="http://forums.xisto.com/no_longer_exists/ Page</a>, all you have to do is to type [exmaple page] and the script automatically searches the database for the page title "example page" and creates a link to it. I am really impressed with this method and I think it can make my life as a webmaster pretty easier, as I am a bit lazy.I have some basic knowledge of php including how to interact with a mysql database. I also know the basics of object oriented programming and can use it to achieve simple tasks. All I need to know is where to start. An alternate way of knowing where to start might be to do a bit of googling on the subject and probably I would find useful information but I am trying to do it this way so that I can discuss my specifics here.

Share this post


Link to post
Share on other sites

For this purpose I am using output buffering with my own callback function, for example I have an array like this:

$array['[example page]'] = '<a href="http://forums.xisto.com/no_longer_exists/ Page</a>';

You just need to dynamically create it, it could be a DB value or something like that, when I just echo [example_page] and in the output buffering callback function when I have all content in a variable I can replace all the found array keys in the content with array values.

I use this method for my template engine.

// A class constructorfunction __construct(){ob_start();if ( !ob_start("ob_gzhandler") ){ob_start();}ob_start( array( &$this , "ob_templater" ) );}

I call the ob_start 3 times for the callback, for the gzip encoding and for sending the right content size of the page, due to gzip.

// Rest in peacefunction End(){ob_end_flush();ob_end_flush();header("Content-Length: ". ob_get_length() );header("Content-Type: text/html; charset=UTF-8");ob_end_flush();exit;}

private $Templater =  array('{language}' => 'en', '{extension}' => 'html');// the callback functionfunction ob_templater($Buffer){return strtr( $Buffer, $this->Templater );}

So if you echo in the page {language} it will automatically change it to en in the template, you just need to put the values in the $Templater array and cache ir or reload on every execution or add values with the files needed, I also have an Add method:

// Add valuesfunction Add($Values = array()){foreach ($Values as $Key => $Value){$this->Templater[ '{'. $Key .'}' ] = $Value;}}

and all you need is just add it, this method will replace the existing values with the new values if the same key exists.

<?php$this->template->Add( array('language' => $this->Language, 'extension' => $this->Extension );?>

When using output buffering, ob_clean(); is really a good way to clean the contents, also you will need to use the End(); function to always end the output buffering properly and avoid using just exit; or die; even though it's nothing bad.

Also, it really fast to use output buffering, it isn't much slower to use it, as the function strtr is much faster than str_replace and preg_replace;

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.