14, February 2012

Creating a basic template engine... - webmaster forum

 
Webdigity webmaster forums
[ Home | Help | Search | Forum's Shop | Archive | Login | Register | Webmaster Directory ]
Webdigity Webmaster Forums  >  Web Development  >  PhP
Topic: Creating a basic template engine...
« previous next »
Pages: [1] Print
Instabuck - The easy way to sell digital products online

Author Topic: Creating a basic template engine...  (Read 260 times)
Atari ST fan
*
Posts: 7
50 credits
Members referred : 0


« on: Dec 06, 2011, 11:06:14 am »

Hello everybody,
I've started to work on building a template engine for a website since I need to seperate the logical units from the graphical units and template engines such as Smarty and TBS are way too big for what I really need: a very basic,simple   bgocled.com   template engine which manage simple tasks such as value reference and conditions.

The value referencing has been quite easy, a simple str_replace would do the job correctly. Where it gets complicated is at the conditional management. I need it to make, for exemple:

Code:
[IF USER_STATUS_ADMIN]{SHOW_ADMIN_LINK}[ELSEIF USER_STATUS_WRITER]{SHOW_WRITER_LINK}[ELSEIF USER_STATUS_TESTER]{SHOW_TESTER_LINK}[ELSE]{SHOW_ALL_LINK}[/IF]
turn into:

Code:
if $user_status_admin {
echo $show_admin_link;
}elseif $user_status_writer{
echo $show_writer_link;
}elseif $user_status_tester{
echo $show_tester_link;
}else{
echo $show_all_link;
}
I must admit that I am quite rusted in PHP, so to reach this goal I first thought about using a regex to dispatch all my values... I used this regex:

Code:
/\[IF (.*?)\](.*?)(\[\/IF\]|\[ELSEIF (.*?)\](.*?)\[\/IF\])/i
Before long I realized that I could only retrieve a single ELSEIF value. So I went with a string scaning method using this code:

Code:
$str = '[IF USER_STATUS_ADMIN]{SHOW_ADMIN_LINK}[ELSEIF USER_STATUS_WRITER]{SHOW_WRITER_LINK}[ELSEIF USER_STATUS_TESTER]{SHOW_TESTER_LINK}[ELSE]{SHOW_ALL_LINK}[/IF]';

$pos = strpos($str, '[IF ');
A new problem arose... Finding the first "[IF " was going fine, but finding the USER_STATUS_ADMIN between "[IF " and "]" was giving me trouble...

So here I am now, which method I should focus my energy on and how to achieve my goal.

Any help on the matter would be greatly appreciated,

Thanks !
Atari ST fan
*
Posts: 7
50 credits
Members referred : 0


« Reply #1 on: Dec 07, 2011, 07:33:05 am »

No one can help me? This problem has troubled me for a long time!
I am a metal monkey!
Administrator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 5779
46271 credits
Members referred : 3



« Reply #2 on: Dec 07, 2011, 01:03:14 pm »

I would prefer to use code when I want to insert code. This way I include the template file, and the whole thing would be much faster.

Trial and Error my two best teachers Cool
Join us @ facebook or twitter

Last blog : Butterfly Marketing 2.0
Trackback URI for this entry : http://www.webdigity.com/trackback.php?topic=14039
Tags : 6 Spotlights rally Spotlights portable Spotlights hand held Spotlights Led Maize Lamp Bookmark this thread : Digg Del.icio.us Dzone more....

Pages: [1] Print 
Webdigity Webmaster Forums  >  Web Development  >  PhP
Topic: Creating a basic template engine...
« previous next »
Jump to:
User Area
Welcome, Guest. Please login or register.
Did you miss your activation email?
Feb 14, 2012, 09:44:46 am





Login with username, password and session length

Donate to our community, and get a permanent link back to your site!

Donate to our community, and get a permanent link back to your site!






Web Design Gallery · Whois Lookup · Pagerank · Tag Browsing · Lo-fi version · Syndication · Webmaster forum history · Advertise
Developed by HumanWorks © 2005 - 2012 Webdigity webmaster community · sublime directory
Webdigity Webmaster Forums | Powered by SMF 1.0.12. © 2001-2005, Lewis Media. All Rights Reserved.