conditionals in a templating system
#1
Posted 03 August 2006 - 01:10 PM
#2
Posted 03 August 2006 - 01:24 PM
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
Edited by .Matt, 03 August 2006 - 01:26 PM.
#3
Posted 03 August 2006 - 04:47 PM
deceptive-logic.com/template.php (yes, it's not a link for a reason).
#4
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 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
Edited by .Matt, 03 August 2006 - 06:45 PM.
#5
Posted 03 August 2006 - 06:42 PM
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
Posted 03 August 2006 - 06:51 PM
#7
Posted 03 August 2006 - 06:53 PM
<!-- 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
Posted 03 August 2006 - 06:54 PM
Edited by .Matt, 03 August 2006 - 06:55 PM.
#9
Posted 03 August 2006 - 08:16 PM
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
Posted 03 August 2006 - 08:23 PM
Edited by danieldaniel, 03 August 2006 - 08:23 PM.
#11
Posted 03 August 2006 - 08:24 PM
#12
Posted 03 August 2006 - 10:49 PM
It's more 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 I laugh at my rather large noobness here.
<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
Posted 04 August 2006 - 11:31 AM
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
Posted 04 August 2006 - 12:03 PM
#15
Posted 04 August 2006 - 12:15 PM
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
Posted 04 August 2006 - 12:18 PM
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
Posted 04 August 2006 - 12:30 PM
#18
Posted 04 August 2006 - 01:03 PM
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
Posted 04 August 2006 - 02:42 PM
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 mebtw the line:
if(\$$2 == \"$4\")\n{ ?>",$content);
Should be:if(\$$1 == \"$3\")\n{ ?>",$content);
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
Posted 04 August 2006 - 04:30 PM
0 user(s) are reading this topic
0 members, 0 guests, 0 anonymous users