man5 Posted September 6, 2014 Share Posted September 6, 2014 So why would one use a php template engine like smarty, other than to seperate the code from html? Is this method preffered for creating larger web applications with php? Quote Link to comment Share on other sites More sharing options...
Ionut-Bajescu Posted September 7, 2014 Share Posted September 7, 2014 (edited) I use Blade template engine. I like {{$var}} more than <?=$var?> One example: @if(true) asdsdasd @endif is more readable than <?php if(true): ?> asdasda <?php endif; ?> Edited September 7, 2014 by Ionut-Bajescu Quote Link to comment Share on other sites More sharing options...
Jacques1 Posted September 7, 2014 Share Posted September 7, 2014 It's definititely recommended to use a proper template engine. (Ab)using PHP for templates comes with a lot of problems: In theory, it may be possible to just separate the PHP logic from the HTML markup, but in reality, it rarely happens. If programmers get the chance to write spaghetti code, they often do, be it because of sloppiness, time pressure, lack of knowledge or whatever. PHP “templates” violate the principle of least privilege. The sole purpose of a template is to generate output, so there's absolutely no reason why it should have the full power of a web programming language. There's no well-defined data interface between the application and the “template”: By default, the “templates” simply have unrestricted access to all global variables and all constants. This can partially be fixed by wrapping the include statements in functions. But then there's still unrestricted access to all superglobals and all constants. PHP is a template engine from the 90s. It doesn't have any modern features whatsoever: no template inheritance, no auto-escaping, no custom tags. The PHP syntax is way too verbose. Compared to Twig, it feels like having to write twice as much. Sure, pure PHP is good enough for small applications and quick hacks. But for anything bigger, use Twig, Smarty or one of the other engines. Quote Link to comment Share on other sites More sharing options...
deathbeam Posted September 7, 2014 Share Posted September 7, 2014 IMO better is use PHP as templating language, becouse PHP IS templating language. Using template engines like Smarty, Twig, Blade or Moustache is imo only slowing your code. I use Blade template engine. I like {{$var}} more than <?=$var?> One example: @if(true) asdsdasd @endif is more readable than <?php if(true): ?> asdasda <?php endif; ?> It is similar enought. Why people should learn new "language" only for writing templates? HTML, CSS, JS and PHP is enought. Quote Link to comment Share on other sites More sharing options...
trq Posted September 7, 2014 Share Posted September 7, 2014 IMO better is use PHP as templating language, becouse PHP IS templating language. Using template engines like Smarty, Twig, Blade or Moustache is imo only slowing your code. Until fairly recently I felt the same, however, you should read the points brought up by Jacques1 again. In particular, I can't go past: "PHP is a template engine from the 90s. It doesn't have any modern features whatsoever: no template inheritance, no auto-escaping, no custom tags." Quote Link to comment Share on other sites More sharing options...
Ionut-Bajescu Posted September 7, 2014 Share Posted September 7, 2014 IMO better is use PHP as templating language, becouse PHP IS templating language. Using template engines like Smarty, Twig, Blade or Moustache is imo only slowing your code. It is similar enought. Why people should learn new "language" only for writing templates? HTML, CSS, JS and PHP is enought. template engine !== new language. You can learn in just two days how to use Twig or Blade. Quote Link to comment Share on other sites More sharing options...
deathbeam Posted September 7, 2014 Share Posted September 7, 2014 (edited) template engine !== new language. You can learn in just two days how to use Twig or Blade. That´s why I put language into " ". It is something like that, another thing to learn when you do not have to. Until fairly recently I felt the same, however, you should read the points brought up by Jacques1 again. In particular, I can't go past: "PHP is a template engine from the 90s. It doesn't have any modern features whatsoever: no template inheritance, no auto-escaping, no custom tags." Template inheritance can be done with "include". Auto-escaping can be done with simple preg_replace when using object buffering to get PHP file. Here is stripped down example what I am doing: public function draw($file) { extract($this->fields); ob_start(); include $file; $html = ob_get_clean(); echo preg_replace("insert auto","escaping here", $html); } And custom tags? Not needed really. But I agree that Jacques is right in most of his points, but properly used PHP as template engine is a lot better than using 3rd party or custom-built template engines in terms of speed and without longer learning curve. Edited September 7, 2014 by deathbeam Quote Link to comment Share on other sites More sharing options...
Jacques1 Posted September 7, 2014 Share Posted September 7, 2014 Why is learning new tools such a big problem? A developer should be able to learn and use all kinds of tools, especially in web programming. If they can't, then, frankly, this job is not for them. You're worried about performance? Do you have concrete figures at hand, or is this just a vague feeling? All modern engines cache the templates as plain PHP scripts, which means there's no technical difference between a handwritten PHP “template” and a Twig template after the initial parsing stage. I very much doubt that there's any significant performance loss. But feel free to prove your point. Template inheritance can be done with "include". No, no, you don't understand. Template inheritance means that I can create a base template and then override specific parts of it in subtemplates – similar to inheritance in OOP. This is incredibly useful and leads to very clean templates. Auto-escaping can be done with simple preg_replace when using object buffering to get PHP file. And that tells us what exactly? That you can throw together some lame tag replacer in 2 minutes? No doubt about that. But I'm talking about a professional template engine. And custom tags? Not needed really. How do you know what I need? Quote Link to comment Share on other sites More sharing options...
deathbeam Posted September 7, 2014 Share Posted September 7, 2014 No, no, you don't understand. Template inheritance means that I can create a base template and then override specific parts of it in subtemplates – similar to inheritance in OOP. This is incredibly useful and leads to very clean templates. This can be done with closures. Why is learning new tools such a big problem? A developer should be able to learn and use all kinds of tools, especially in web programming. If they can't, then, frankly, this job is not for them. You're worried about performance? Do you have concrete figures at hand, or is this just a vague feeling? All modern engines cache the templates as plain PHP scripts, which means there's no technical difference between a handwritten PHP “template” and a Twig template after the initial parsing stage. I very much doubt that there's any significant performance loss. But feel free to prove your point. It isn´t problem and I was using both Smarty and Twig. But I am not fan of them. Why? Becouse IMO it is overkill. I never liked using more code than I need, and using XXXKBs more of code just to have comfortable way to differ presentation from bussiness logic is not my style. PHP short tags and alternative tags like endif, endfor etc are enought readable for me. And yes, modern templating engines are caching their templates, but they still need to cache them at first load (and re-cache after every modification) and that takes some more miliseconds (yes, it isn´t noticeable, but it IS performance lost). And that tells us what exactly? That you can throw together some lame tag replacer in 2 minutes? No doubt about that. But I'm talking about a professional template engine. Okay, I agree and this is valuable point for me (yes, it is still my own opinion, I know that others can have different opinions on this). Again, I will repeat what I said before. I do not like using more code than I need. And using professional template engine only becouse it have great auto-escaping feature.... How do you know what I need? As I said, this is my subjective opinion, my 2 cents. It is feature without what I can continue living. Quote Link to comment Share on other sites More sharing options...
Jacques1 Posted September 8, 2014 Share Posted September 8, 2014 I understand this feeling. I also used to be very sceptical towards new tools: Why use jQuery when I might as well write plain JavaScript? Why use a PHP framework when my oldschool PHP scripts seem work just fine? But it's just a feeling. As you've already pointed out, a lot of your revervations against template engines are purely subjective. If you approach this question rationally and go by hard facts rather than personal opinions, I think you'll come to a different results. I already listed the objective advantages of template engines, so what are the concrete drawbacks? Performance? Show me the benchmarks. Also note that a lot of PHP applications run on the slowest possible Apache-CGI setup on the slowest possible shared host, so maxing out the template performance is simply not a concern for them. Added complexity? Sure, there are some extra scripts in my lib directory. But in practice, that doesn't affect me in any way. The actual API is very simple with minimal overhead. Of course you're free to say: I just don't like template engines. Fine, don't use them. Nobody said you should. But I think the OP wanted to read an objective comparison rather than our personal preferences. 1 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.