5

How can i parse {if game > 4}{somecontent}{/if} from a template using PHP.

hakre
  • 193,403
  • 52
  • 435
  • 836
Speedy Wap
  • 478
  • 4
  • 7
  • 18
  • 1
    What exactly do you want to "parse"? What's your expected result? – Phil Feb 16 '11 at 01:42
  • It might also be useful to know the *type* of PHP template. There is more than one. – cwallenpoole Feb 16 '11 at 01:46
  • lets say that index.tpl has this condition. then we set $game = 5. this means that when the tpl file is parsed the {somecontent is displayed} since the condition enclosing it is true – Speedy Wap Feb 16 '11 at 01:49
  • @cwallenpoole Given their other [question](http://stackoverflow.com/questions/5011566/preg-replace-in-php), I'd say it doesn't matter. – Phil Feb 16 '11 at 01:49
  • @user Then I'd say you're doing it wrong. Why write your own PHP templating engine when there are many available? Check out [Twig](http://www.twig-project.org/) or [Smarty](http://www.smarty.net/) – Phil Feb 16 '11 at 01:50
  • Or, check out [phptemplatinglanguage](http://phptemplatinglanguage.com) :P – alex Feb 16 '11 at 01:53
  • i am trying to learn how to do this. Smarty is too big and complex to understand so I am trying to know an easier way of solving just this one problem. – Speedy Wap Feb 16 '11 at 01:55
  • 2
    This is over your head. Explaining how to write a parser (and by a strict interpretation of your question phrasing you would also need an interpreter) is absolutely out of scope for [php]-Stackoverflow and a lazy two-liner question sans bounty. -- What you can do however is asking about *converting* your pseudo template language into php, like all other php templating thingys do. – mario Feb 16 '11 at 01:57
  • pseudo code I have, but I dont have a good understanding of the regex and they are stopping me from creating this algorithm. I cant even find some decent tutorials on regex on the net which can help me understand regex to a very high level (i guess not many know or use regex thats why it is so scarcly used) – Speedy Wap Feb 16 '11 at 02:01
  • 2
    http://regular-expressions.info/ and here is a list of tools that aid in designing regular expressions http://stackoverflow.com/questions/89718/is-there-anything-like-regexbuddy-in-the-open-source-world -- But there is no magic regex that automatically parses AND evaluates pseudo code for you. You can't do that the super-easy way. The approach from your last question http://stackoverflow.com/questions/5010925/templating-in-php-using-tpl-files (which you didn't feel the need to tell us about) is not workable for also supporting `{if}` statements. – mario Feb 16 '11 at 02:06
  • You cannot learn regex in a day, nor it is wise to build a complex template parser using regex if you don't have a good handle on it. Take ElbertF's advice and just use PHP tags. Or, use Flexy. It's simple and lightweight. – Jared Farrish Feb 16 '11 at 02:09
  • 1
    I have a feeling that (and don't quote me on this) the major templating engines would **not** use regular expressions to parse templates. I'm guessing some other [lexical analysis](http://en.wikipedia.org/wiki/Lexical_analysis) is at work – Phil Feb 16 '11 at 02:09
  • @Phil Brown - I didn't even WANT to get into that argument. :) – Jared Farrish Feb 16 '11 at 02:10
  • 1
    @Jared Seems I was right about at least one engine - http://www.twig-project.org/doc/hacking.html. @user Well worth reading if you want to understand templating – Phil Feb 16 '11 at 02:17
  • @Phil Brown - That could be an answer: How does a lexical parser differ from a regex parser when confronting a simple if block structure, and what are the pros/cons of each? That's really all the OP was asking in the original questions (the comments notwithstanding). – Jared Farrish Feb 16 '11 at 02:29

2 Answers2

6

What's wrong with using plain old PHP? It's much faster and a whole lot simpler.

<?php if ( $game > 4 ): ?>
some content
<?php endif ?>

If you really insist, here's a start (untested):

<?php
preg_match_all('/\{if ([^}]+)\}.+?\{\/if\}/s', $content, $matches)

foreach ( $matches as $match )
{
    $expression = $match[1];

    // Evaluate expression

    $content = preg_replace($match[0], $true ? $match[1] : '', $content);
}
?>

This is pretty simple, it get's really hairy when you want to work with nested statements.

Elbert Alias
  • 1,770
  • 1
  • 16
  • 25
  • The thing is that want to understand how it could be possibly done using php. It is about my learning curve. I do know that php is much faster and a whole lot simpler, but when the projects grow very large with multiple themes it is usually simpler to use templating. – Speedy Wap Feb 16 '11 at 01:57
  • "projects grow very large" should not lead to "roll your own templating system". If you can't handle one of the tried and trusted templating systems, I doubt that making your own is a good idea. – Jared Farrish Feb 16 '11 at 02:04
  • @user It is definitely not simpler if you're writing the templating engine yourself – Phil Feb 16 '11 at 02:06
  • @user381595 I've updated my answer with a quick example. As other's have said though, _especially_ with big projects you don't want to introduce unnecessary layers of complexity. – Elbert Alias Feb 16 '11 at 02:08
  • 4
    haha +1 - I like how your example code says `// Evaluate expression` so casually. But I fear the next upcoming question... – mario Feb 16 '11 at 02:11
  • thanks for helping me on atleast getting a starting point. This is the sort of pseudo code I had in mind but I just cant write regex at all so it really fails me. But this would help me learn more as it is all about learning. Thanks once again. – Speedy Wap Feb 16 '11 at 02:12
  • @mario I wish I could upvote your comment a couple more hundred times ;) – Phil Feb 16 '11 at 02:15
0

You can parse that syntax using smarty template engine.

http://www.smarty.net/crash_course

Jason
  • 15,064
  • 15
  • 65
  • 105