Jump to content

Creating a template engine...


Norin

Recommended Posts

Hello everybody,

I've started to work on building my own template engine for my 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 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:

 

[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:

 

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:

 

/\[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:

 

$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 ! :)

Link to comment
Share on other sites

As I said earlier, I need to seperate my logical and graphical units for the website. I will be working with people who have absolutely no clue about PHP, these people need to be able to create their own HTML file while someone else will be working on the PHP.

Link to comment
Share on other sites

So?  It's not like they have to learn php 100%, just how to write an if statement/loop and how to echo a variable.  I'm sure they could learn that just as easily as they could learn your own custom template syntax.

 

Your 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]

 

PHP way

<?php if ($USER_STATUS_ADMIN):  ?>
    <?=$SHOW_ADMIN_LINK?>
<?php elseif ($USER_STATUS_WRITER): ?>
    <?=$showSHOW_WRITER_LINK?>
<?php elseif ($USER_STATUS_TESTER): ?>
    <?=$SHOW_TESTER_LINK?>
<?php else: ?>
    <?=$SHOW_ALL_LINK?>
<?php endif; ?>

 

Not that much different.  You could always just write a simple filter to use something other than <?php/?> as the opening/closing tag, such as maybe {{/}}

 

Link to comment
Share on other sites

It's unfortunately not really in my power to ask to change how the project was planned. There have already been a bunch of pages created using the [iF] and {} values.

 

Would it be possible to focus on how to achieve the initial goal please ?

Link to comment
Share on other sites

Would it be possible to focus on how to achieve the initial goal please ?

 

A persons initial goal isn't always the correct way to do something.  Had you mentioned before that work had already been done on the templates in that syntax, I would have been more likely to solve the initial problem.  As it was, your posted read to me as if you were just starting, and not much work had yet been invested into this, so I suggested an alternative that would save you time and be more flexible in the future before you went down the road to far.

 

As for your problem, I would suggest using a string scanning method.  To answer your question about how to retrieve the if condition, take this example:

$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 ');
if ($pos !== false){
  //found an if, now find the ] that closes the if
  $pos += 4; //Move ahead four chars, '[iF ' = 4 chars
  $end = strpos($str, ']', $pos); //third parameter stars the search at the given offset, in this case it starts at the location of the [iF
  if ($end !== false){
    //found the end of the if
    //get the middle content
    $content = substr($str, $pos, ($end-$pos));
  }
}

 

$content would contain your 'USER_STATUS_ADMIN' text. To process the whole thing, just use code similar to the above in a loop, and keep track of your progress in the string as you move through it.

 

 

Link to comment
Share on other sites

Thanks a lot !

 

I followed your advice and could get it partly done. Seems like everybody is okay to remove the whole [iF] pattern since I said that I would code it on my own in the PHP file (told em that it would be faster too since I'd be saving on manipulations). However, they absolutely want no PHP in the HTML file, so what will happen is that I will do the whole PHP file by including all the if etc and will only replace the {} tags. They told me that this was a must since it would allow everyone to continue working on the HTML files (they also create a document with each HTML file explaining what {} tags must contains).

Link to comment
Share on other sites

No offense, but chances are a 'big' system like Smarty will still run better than a smaller system you build

 

A big suggestion is a caching system. If the data is pretty static, you'll save a ton of processing keeping a pre-compiled version of the markup.

Link to comment
Share on other sites

No offense, but chances are a 'big' system like Smarty will still run better than a smaller system you build

 

A big suggestion is a caching system. If the data is pretty static, you'll save a ton of processing keeping a pre-compiled version of the markup.

 

Agreed. Even though Smarty is big and clunky, it still converts its templating language into raw PHP, which is a lot faster than processing all that stuff every page load.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

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.