4 ms·
> They didn't use a template engine. PHP is a templating engine. Writing a templating language for PHP, like Smarty, is like writing a second templating langua
by habibur 18y ago
> They didn't use a template engine.
PHP is a templating engine. Writing a templating language for PHP, like Smarty, is like writing a second templating language running on the first templating language.
- wvenable 18y agoPHP is a crappy template engine. Inlining code into an HTML document requires a lot of discipline, a lot of code to write, and is error prone. Why not let software handle that task for you? You want to create more work for yourself?
- habibur 18y agoDon't want to inline code? Well don't inline code. No second templating engine can reduce complexting any more than using PHP directly as a templating language. PHP was built as a templating engine from day 1. There is a reason why php scripts start with <? --- ?>
- wvenable 18y ago"No second templating engine can reduce complexting" Oh yeah, compare: <?php echo htmlentities($firstName, ENT_COMPAT, "UTF-8"); ?> with: {$firstName} Isn't that reduced complexity? And that's not even getting into other constructs. In my template language (based on smarty), a single {loop} tag expands to 10 lines of PHP code. Would you also claim that no programming language can ever reduce complexity over assembly language? Probably not. This is just the same thing. Compiling a higher level language into a lower level language.
- habibur 18y agoWell, here is what I would write: Smarty: {$firstName} vs PHP: <?=h($firstName)?> And, loops? Your: {loop} vs PHP's loop: <?while(loop()){?> Of course you have to define function h() and loop() somewhere. Just don't do it in your template file and you are fine. Hardly saving you even one line of code. We are down to counting how many charecters each need. Would you also claim that no programming language can ever reduce complexity over assembly language? Probably not. This is just the same thing. Compiling a higher level language into a lower level language. I woun't.
- wvenable 18y ago"PHP: <?=h($firstName)?>" Bzzzt wrong. And this is why having a template engine might be a good thing. <?= isn't 'safe' because it can be disabled in the php.ini file (and is disabled by default). If only you had a template language that keeps you having to know that! Also, the default is that your html escapes -- you can't forget to wrap your variable in the h() function. So it's already less error prone. And this is still just a simple example. "PHP's loop: <?while(loop()){?>" It's at least <?php while(list($key, $item) = loop($iterator)){?> to be correct and useful. And the template engine can inline the code (rather than call a loop() function) for better performance. All those correctly expanded PHP tags intermixed in the HTML makes the template harder to follow. You might want to type twice as much identical code for each output variable, but I don't want to do that. It is an advantage that it's cleaner and clearer.
- habibur 18y ago<?= isn't 'safe' This is a web app for God's sake. You have full control on what your php.ini have. Which one is more complex? Install a templating language? or ensure your short-tags=On (or whatever that is) in your php.ini. And if you are talking about hosted service, short tags are by default kept "On" on most hosted services, if not all. <?php while(list($key, $item) = loop($iterator)){?> No, I can write any of the following if I want the variables localy. <?while($r=loop($rs)){?> <---- this is the version I use most frequently. <?foreach($rs as $key=>$item){?> or even <?while(extract(loop($rs))){?> Now compare all these with any other language beside PHP. Java, C# or Python. response.write "<html><head><title>" + myTitle + "</title>"; response.write "<body bgcolor=\"white\">"; while(loop inside iterator){ response.write "<tr><td" + print_var; } Imagine typing your full page like this! You need to enclose each of your line inside a quote and also escape all quotation marks inside it. Here code and design are mixed in real sense. These are the languages that really need a templating engine. And the template engine can inline the code (rather than call a loop() function) for better performance. Here is an idea, better performace is gained by not loading the templating engine at all. Whatever number of chars typing saved is more complicated by loading and initializing the whole templating engine. Even that is not enough, which is why Smarty has {if}, {else}, {switch} statements. So long for seperating code and design. You might want to type twice as much identical code for each output variable I am not typing identical code even once. If only you had a template language that keeps you having to know that! We can continue discussion without making attacks? No?
- eelco 18y agoHiding complexity is not the same as reducing it.
- knome 18y agoThere is no reducing it. There is controlling who deals with it. Hiding gobs of details behind a single {loop} construct was probably a huge hassle. For the designers and non-coders touching the templates though, the complexity is gone. It isn't really, of course. It was only transferred from them to the programmer. But from their point of view, it is.