Jump to content


Photo

conditionals in a templating system


  • Please log in to reply
36 replies to this topic

#1 Jordan Pittman

Jordan Pittman

    Young Padawan

  • Publishing Betazoids
  • Pip
  • 56 posts
  • Gender:Male

Posted 03 August 2006 - 01:10 PM

how would i implement conditionals in my templating system, if/elseif/else/switch because i have tried and every time my code would not work like it should have, also if someone could tell me a good way to implement loops kinda like phpbb's assign_block_vars but eaiser

#2 Matthew.

Matthew.

    Official Spammer .Matt

  • Members
  • PipPipPipPip
  • 2,749 posts
  • Gender:Male
  • Location:England

Posted 03 August 2006 - 01:24 PM

Hmm,you mean like how IPB does it?

e.g:

<div class="answear">
   <if condition="$foo[a_var] == TRUE">
		yep, true
<else />
		Nope, false :(
</if>
</div>

If so im afraid ill admit that that's a little beyond me right now,and ill be watching this topic with me :)I could probably give you a few ideas if i thought about it for a little while but nothing working.

If not could you explain a little more / better give an example :P

Edited by .Matt, 03 August 2006 - 01:26 PM.


#3 rc69

rc69

    PHP Master PD

  • P2L Staff
  • PipPipPipPip
  • 3,827 posts
  • Gender:Male
  • Location:Here
  • Interests:Web Development

Posted 03 August 2006 - 04:47 PM

They're really not that hard, just tricky to figure out the first time. I'd explain how to do it, but i don't have time atm, so the best i can do is say, here, have a look at my template class. You want to ignore the comment, and look at the function under it.
deceptive-logic.com/template.php (yes, it's not a link for a reason).

#4 Matthew.

Matthew.

    Official Spammer .Matt

  • Members
  • PipPipPipPip
  • 2,749 posts
  • Gender:Male
  • Location:England

Posted 03 August 2006 - 05:20 PM

They're really not that hard, just tricky to figure out the first time. I'd explain how to do it, but i don't have time atm, so the best i can do is say, here, have a look at my template class. You want to ignore the comment, and look at the function under it.
deceptive-logic.com/template.php (yes, it's not a link for a reason).


Oh ok so you have it like

<div class="answer">
<!-- SWITCH $GLOBALS['jam']_good -->
Jam is good
<!-- STOP $GLOBALS['jam'] -->
</div>

im shocked at just how different my template class is to yours, mainly because mine took 5 minutes i suspect and isnt meant to be good lol. Ill get it up ina sec.

Ill admit i cheated for something.

<li><a href="{furl}/?id={qid}" {c}>{l_title}</a></li>
<rewrite_on>
<li><a href="{furl}/article/{qid}/" {c}>{l_title}</a></li>
You can see what i wanted to do i reckon, if mod_rewrite is available then use 1 link, if it isnt use another link format. So i exploded it lol :P I laugh at my rather large noobness here.

mediascratch.uni.cc/ms/inc/class.template.phps (same as)
wth - my formatting is ruined in places when shown as phps o.0

Tell me that aint bad rofl :( I'm going to rewrite it so please point out mistakes, ill add conditionals as well i think :P

Edited by .Matt, 03 August 2006 - 06:45 PM.


#5 Jordan Pittman

Jordan Pittman

    Young Padawan

  • Publishing Betazoids
  • Pip
  • 56 posts
  • Gender:Male

Posted 03 August 2006 - 06:42 PM

i want something like
If, elseif, else, switch, loop(for and while) statements
also i can make the [ and ] things changeable if i need to
<!-- IF ([var] equals "test") -->
test
<!-- ELSEIF ([var] equals "test2") -->
hmm.. whats a test2
<!-- ELSEIF ([var] equals "test3") -->
hmm.. whats a test3
<!-- ELSE -->
whaaaaa i want a better value
<!-- ENDIF -->

<!-- SWITCH [i] -->
<!-- CASE ("test2") -->
i see the test 2 switch, mabye i otta buy it lol
<!-- CASE ("Test") -->
i think test means trying to buy time to code
<!-- ENDSWITCH -->

<!-- WHILE([i] <= 1000) -->
{i}
<!-- ENDWHILE -->

<!-- FOR([i]=0, [i]<=1000, [i]++) -->
{i}
<!-- ENDFOR -->


#6 danieldaniel

danieldaniel

    Young Padawan

  • Members
  • Pip
  • 75 posts
  • Gender:Male
  • Location:Columbus, Ohio
  • Interests:Skateboarding, Macs, BMX, Cocoa, PHP, CSS etc.

Posted 03 August 2006 - 06:51 PM

Are there tutorials anywhere on it? I've been wondering the same thing.

#7 cheerio

cheerio

    Young Padawan

  • Members
  • Pip
  • 246 posts
  • Gender:Male

Posted 03 August 2006 - 06:53 PM

Maybe you can use preg_replace and replace (for example):
<!-- IF ([var] equals "test") -->
with
if($var == "test")

and then in PHP you can use eval() and then echo the result or something

I've never done anything like this before, but maybe that can work?

#8 Matthew.

Matthew.

    Official Spammer .Matt

  • Members
  • PipPipPipPip
  • 2,749 posts
  • Gender:Male
  • Location:England

Posted 03 August 2006 - 06:54 PM

To the extent itunes wants i would be very very suprised if there was any tutorials danieldaniel, tbh id be suprised if there was any.

Edited by .Matt, 03 August 2006 - 06:55 PM.


#9 cheerio

cheerio

    Young Padawan

  • Members
  • Pip
  • 246 posts
  • Gender:Male

Posted 03 August 2006 - 08:16 PM

Aright, I just made this in dreamweaver, maybe somebody can expand on it, I suck with regular expressions:

template.html
?><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body>
<!-- IF [x] equals "index" -->
Welcome to my website! You are viewing the HOME PAGE
<!-- ELSE -->
You are in some other randomn page...
<!-- /ELSE -->
</body>
</html>

parser.php
<?php
$file = "test2.html";
$content = file_get_contents($file);

//Well make it so that anything between [ and ] is a variable name.
//You can use if,else,and elseif
$content = preg_replace("/<!-- IF \[(.*?)\] (equals|is equal to) \"(.*?)\" -->/i","<?php if(\$$2 == \"$4\")\n{ ?>",$content);
$content = preg_replace("/<!-- (\/ELSE|\/IF) -->/i","<?php } ?>",$content);
$content = preg_replace("/<!-- ELSE -->/i","<?php } else { ?>",$content);
echo eval($content);
?>


#10 danieldaniel

danieldaniel

    Young Padawan

  • Members
  • Pip
  • 75 posts
  • Gender:Male
  • Location:Columbus, Ohio
  • Interests:Skateboarding, Macs, BMX, Cocoa, PHP, CSS etc.

Posted 03 August 2006 - 08:23 PM

So IPB basically created a parser sort of? I thought you guys meant this was a method of coding like OOP.

Edited by danieldaniel, 03 August 2006 - 08:23 PM.


#11 cheerio

cheerio

    Young Padawan

  • Members
  • Pip
  • 246 posts
  • Gender:Male

Posted 03 August 2006 - 08:24 PM

I guess there really is only one way to find out, and thats to look at IPB's code or find some other script on google.

#12 rc69

rc69

    PHP Master PD

  • P2L Staff
  • PipPipPipPip
  • 3,827 posts
  • Gender:Male
  • Location:Here
  • Interests:Web Development

Posted 03 August 2006 - 10:49 PM

<div class="answer">
<!-- SWITCH $GLOBALS['jam']_good -->
Jam is good
<!-- STOP $GLOBALS['jam'] -->
</div>

im shocked at just how different my template class is to yours, mainly because mine took 5 minutes i suspect and isnt meant to be good lol. Ill get it up ina sec.

Ill admit i cheated for something.

<li><a href="{furl}/?id={qid}" {c}>{l_title}</a></li>
<rewrite_on>
<li><a href="{furl}/article/{qid}/" {c}>{l_title}</a></li>
You can see what i wanted to do i reckon, if mod_rewrite is available then use 1 link, if it isnt use another link format. So i exploded it lol :huh: I laugh at my rather large noobness here.

It's more like:
<div class="answer">
<!-- START blockName -->
	<!-- SWITCH jam_good -->
	Jam is good
	<-- STOP jam_good -->
	<!-- SWITCH jam_bad -->
	Jam is bad
	<-- STOP jam_bad -->
<!-- END blockName -->
</div>
It's a really old template class, one of my first break throughs that made me finish my DBMS, so there's a lot to be desired from it, but it works.
Your template class looks a lot more complex then mine, but you gave me an idea with the <rewrite_on> thing for my latest revision of my template class (4th round tweaking it closer to desireable, this time i'm practically redoing it from the ground up).

As for what itunes wants, if i sat down for a month and actually worked away at it, i could probably come up with something that would work. But off the top of my head, you would be hard pressed to find anything like that. I believe i saw a similar phpBB mod, but what you're trying to do would be much easier to do all the loops and everything in pure php (no html parsing), and then add the results to the template like how my class does it.

#13 Matthew.

Matthew.

    Official Spammer .Matt

  • Members
  • PipPipPipPip
  • 2,749 posts
  • Gender:Male
  • Location:England

Posted 04 August 2006 - 11:31 AM

Ok i tried doing some simples ifs today, the problem is, eval echos out the code straight away, and you cant use use return like:
eval('return '. $code . ';');
as you can have an IF or any other code when using return. Even if you could you have the other problem of the rest of the template file.

So thats me given up. I would like to include it myself but i know i need help :)

Edited by .Matt, 04 August 2006 - 11:32 AM.


#14 Jordan Pittman

Jordan Pittman

    Young Padawan

  • Publishing Betazoids
  • Pip
  • 56 posts
  • Gender:Male

Posted 04 August 2006 - 12:03 PM

my parser is made in OOP :) and i will try the code given and see if i can make variables avaliable somehow, i did want something close to PHPBB3's Templating system or the phpBB2 XS mod templating system

#15 Jordan Pittman

Jordan Pittman

    Young Padawan

  • Publishing Betazoids
  • Pip
  • 56 posts
  • Gender:Male

Posted 04 August 2006 - 12:15 PM

well i get
Parse error: syntax error, unexpected '<' in /home/itunes66/public_html/test.php(98) : eval()'d code on line 2 so yeah and i tried removing the php tags and still no good

#16 Matthew.

Matthew.

    Official Spammer .Matt

  • Members
  • PipPipPipPip
  • 2,749 posts
  • Gender:Male
  • Location:England

Posted 04 August 2006 - 12:18 PM

Well it can be done (which is proven by the applications themselves of course) but to ask a question like this and expect something usable is a bit much, its a complicated thing :P

The code given does work but it will need modifying quite drasticially lol.I got quite far but it was taking time i want to be working on other things :) + i got stuck lol. Deleted what ive done now :( Wish i saved it could have helped you.

btw the line:
if(\$$2 == \"$4\")\n{ ?>",$content);

Should be:

if(\$$1 == \"$3\")\n{ ?>",$content);

on the code given for it to work.

edit: itunse, i think what you are parsing through eval is just php is it?

This is a basic version that works:

<?php
$file = "test.tpl";
$temp_data = file_get_contents($file);

//Well make it so that anything between [ and ] is a variable name.
//You can use if,else,and elseif

		$temp_data = preg_replace("/<if \[(.*?)\] (equals|is equal to) \"(.*?)\">/i","if(\$$1 == \"$3\")\n{ echo \"",$temp_data);
		$temp_data = preg_replace("/<(\/else|\/if)>/i"," \"; }",$temp_data);
		$temp_data = preg_replace("/<else>/i","\"; } else { echo \"",$temp_data);
		
$var = "hello";
echo eval($temp_data);
?>

<if [var] equals "hello">
Hello!
<else>
Its not hello!
</else>

I change the structure though :)

Edited by .Matt, 04 August 2006 - 12:20 PM.


#17 Jordan Pittman

Jordan Pittman

    Young Padawan

  • Publishing Betazoids
  • Pip
  • 56 posts
  • Gender:Male

Posted 04 August 2006 - 12:30 PM

the code bove did not work either also i am runnning php4 is that a problem (i dont know why it would be though)

#18 Matthew.

Matthew.

    Official Spammer .Matt

  • Members
  • PipPipPipPip
  • 2,749 posts
  • Gender:Male
  • Location:England

Posted 04 August 2006 - 01:03 PM

gah im on 5 the answer no though. Nothing there should make it incompatable.

remember like i said you can just put awhole template file through it though. Did you try using just the template file i supplied above?

I had to bugger about seperating the conditionals away form the rest of the file and putting it back parsed which put me off.

#19 cheerio

cheerio

    Young Padawan

  • Members
  • Pip
  • 246 posts
  • Gender:Male

Posted 04 August 2006 - 02:42 PM

btw the line:

if(\$$2 == \"$4\")\n{ ?>",$content);

Should be:

if(\$$1 == \"$3\")\n{ ?>",$content);

I don't know why, but when i did it (on my local server) i had to use 2 and 4 because whatever was in () was becoming a variable, i don't know why but that's just how it was for me

and like i said, i'm not a big reg expert or anything, but that's just what iw as able to come up with real quick

#20 Jordan Pittman

Jordan Pittman

    Young Padawan

  • Publishing Betazoids
  • Pip
  • 56 posts
  • Gender:Male

Posted 04 August 2006 - 04:30 PM

well thanks anyway hopefully ill some up with an answer but my regex sucks so




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users