PDA

View Full Version : [PHP HELP] template system



VistaBoy
28-12-2008, 09:00 AM
Okay i have been trying to make something like this for a long long time but never got it to work. i would like to know if someone can show me how to do this

in a .tpl



<-- if $member->group == admin -->

adminCP link

<-- endif -->

<-- if $member->group == vip -->

stuff
<-- elseif -->

wow

<-- endif -->



i know it has to use preg_replace etc but i am lose and need the help.

Anzrew
28-12-2008, 06:44 PM
A good idea, I can get you started but you can learn the rest :P

Read up on:
file_get_contents()
preg_replace()
str_replace()
eval()
Regular Expression Matching

// First, import the file contents into a variable
$content = file_get_contents("PATH/TO/TEMPLATE.TPL");

// Replace if with a php if
$content = preg_replace("'<-- if (.*) -->'","<?if(\\1) {?>", $content);

// Replace end if with php end if
$content = str_replace("<-- endif -->","<?}?>", $content);

// Lets run the outcome through the php engine again, and show the result.
echo eval("?>$content<?");

Why use str_replace second, as long as <-- endif --> always occurs EXACTLY like that you can easily replace it with }. str_replace is slightly faster than preg_replace so its best to use that where possible.

This should get you started on the varibles you need to look at and the basic method of how you do it. I cannot guarntee the above will work (I haven't tested it) but the theory there should, so it maybe a case of trial and error to get this particular method to work. I should add that this is very basic but if you are really interested I am sure you can learn how to make it better and to your needs.

Hope that helps :) in some way. Alternativly you could try and google "php template parsing" (I just did and saw 1 relevant result).

Jxhn
28-12-2008, 08:44 PM
What the **** is that? You can't combine html conditional statements with php unless I'm missing something here.

Tomm
28-12-2008, 08:45 PM
Use Smarty. Has what you want built into it.

Jackboy
28-12-2008, 08:49 PM
What the **** is that? You can't combine html conditional statements with php unless I'm missing something here.

It's for a template. He wants to do like vbulletin do, <if blah blah> etc.

It is possible using str_replace. fopen or w/e etc etc depending on template.

VistaBoy
28-12-2008, 10:52 PM
A good idea, I can get you started but you can learn the rest :P

Read up on:
file_get_contents()
preg_replace()
str_replace()
eval()
Regular Expression Matching

// First, import the file contents into a variable
$content = file_get_contents("PATH/TO/TEMPLATE.TPL");

// Replace if with a php if
$content = preg_replace("'<-- if (.*)-->'","<?if(\\1) {?>", $content);

// Replace end if with php end if
$content = str_replace("<-- endif -->","<?}?>", $content);

// Lets run the outcome through the php engine again, and show the result.
echo eval("?>$content<?");

Why use str_replace second, as long as <-- endif --> always occurs EXACTLY like that you can easily replace it with }. str_replace is slightly faster than preg_replace so its best to use that where possible.

This should get you started on the varibles you need to look at and the basic method of how you do it. I cannot guarntee the above will work (I haven't tested it) but the theory there should, so it maybe a case of trial and error to get this particular method to work. I should add that this is very basic but if you are really interested I am sure you can learn how to make it better and to your needs.

Hope that helps :) in some way. Alternativly you could try and google "php template parsing" (I just did and saw 1 relevant result).

Thank you ill +rep if i can.


Use Smarty. Has what you want built into it.
I hate Smarty and i want to use a custom one.

Okay so far i got this but i am unsure if its right.


<?php

class template
{

var $template_dir = '.templates/';
var $file_ext = '.tpl';
var $buffer;

function buff_template ($file)
{
if( file_exists( $this->template_dir . $file . $this->file_ext ) )
{
$this->buffer = file_get_contents( $this->template_dir . $file . $this->file_ext );
return $this->parse_variables($this->buffer);
}
else
{
die();
}
}

function parse_variables($input)
{
$search = preg_match_all('/<-- {.*?} -->/', $input, $matches);

foreach($matches as $value)
{
$input = preg_replace("'<-- if (.*) -->'","<? if(\\1) {?>", $input);
$input = preg_replace("'<-- foreach (.*) -->'","<? foreach(\\1) {?>", $input);
}

$content = str_replace("<-- end -->","<?}?>", $content);

return $input;
}

}

$engine = new template;

echo eval("?>$engine->buff_template(login)<?");
?>

login.tpl


<-- if $member->group == admin -->

adminCP link

<-- end -->

<-- foreach $array as $value => $key -->

key: $key value: $value

<-- end -->

Jxhn
29-12-2008, 09:22 AM
Ah, sorry I see what you mean now. The above code looks fine to me, aslong as you eval the parsed input. This could cause security issues if someone got into the template system though.

Anzrew
29-12-2008, 09:58 AM
Hey VistaBoy,
Your code that you gave looks correct, one small thing you need to change is the eval statement:

echo eval("?>$engine->buff_template(login)<?");

Needs to become:
echo eval("?>".$engine->buff_template(login)."<?");

That should then be fine, I haven't run it but it seems logically correct :D! Just a matter of trial and error now.

Dentafrice
29-12-2008, 05:38 PM
One question, why are you echoing the eval out, outside of the class?

Why can't you just do:



echo $engine->buff_template( 'login' ); // echo since you return it


Why don't you eval the lines inside of the class, instead of having to call it outside of the script?

You should just be able to call the template very simply, without having to enclose it in eval, if you get what I'm saying :)

Kyle!
29-12-2008, 07:10 PM
Smarty is your best option here, it works the way you want it, and its really easy to code for.

Want to hide these adverts? Register an account for free!