Forum Moderators: coopster
Now, my question is, how much does shorthand affect the performance of parsing the code, whether negative or positive?
As an example, stepping in and out of PHP when looping through an array:
<?php $array = array('value 1', 'value 2', 'value 3', 'value 4', value 5'); ?>
<?php foreach ($array as $key=>$val): ?>
<li><a href="?val=<?php echo $key; ?>"><?php echo $val; ?></a></li>
<?php endforeach; ?>
This is an example of the sort of shorthand that I mean. Of course, stats on that you might have or know of on any other type of shorthand are always welcome.
Thanks in advance :)
The sort of context that I had envisioned was one where you have quite a large chunk of (predominantly) HTML code that needs to be repeated. Instead of having to
print out heaps of HTML, you can let it just display. I don't think this explanation is coming across as much as I like it. It makes sense in my head ^_^
That being the case, where HTML is in a PHP script, it is clearest for it to be enclosed in "", so that the variables may be inserted with ease and debugging is easy. I've spent hours rewriting the style of code you suggest above into more readable formats - and in doing so all the bugs became easy to spot.
I use them but I also use some of the above, no problems in my mind, there is no absolute rule on this and you need to analyze your situation/site and make the best decisions for your users.
speed is one of the most important things on the web
a lot of the templating systems or cms's that I've seen are crap
However I have seen improvements in page load times ever since I began writing valid MarkUp and CSS some years ago. That's for sure.
stating templates as a hard and fast rule is a way to make sure that newer coders make slow sites
Without templates:
- Cheap slow hosting: $25/mo.
- Total hosting cost: $300
- Every change: 25 pages x 5mins = 2h 5mins
- Just one change a week: 108 h 20 mins
- Total time costing at $50/h: $5416.67
- Final cost excluding intial dev: $5716.67
With templates:
- Dedicated: $200/mo.
- Total hosting cost: $2400
- Every change: 1 page x 5 mins = 5 mins
- Just one change a week: 4 h 20 mins
- Total time costing at $50/h: $216.67
- Final cost excluding initial dev: $2616.67
I have deliberately undervalued programmer time and underestimated change count. I have costed both programmers the same, despite the fact that realistically the templated site can be maintained by a non-PHP programmer with knowledge only of HTML, much less expensively. In more realistic cases the benefits of using some form of template system will be far higher.
You then need to look a step further at what happens next. Under site growth to say 100 pages, the model becomes even more in favour of the templated system.
Finally, don't forget how easy it is to miss a page when manually changing a small detail across a non-templated site. If you missed one in 25 pages each time, over a year of changes you'd average two mistakes a page and have a very ragged looking site.
It is my belief that long sections of HTML should not be anywhere near to PHP. They should be in template files (separately maintained) or stored in a CMS system. I find it is rare that you need much HTML in a PHP script.
Some argue that PHP itself is a template engine. Thus, you don't actually need another template engine on top. It's not a question of mixing HTML code with PHP code. It's a question of mixing business logic with presentation logic.
Besides, template engines add complexity (as in too complicated for beginners) or they can have performance issues (if you don't cache).
I'm aware that a developer has to be more disciplined when working with PHP templates because of the lack of restrictions.
NN
"stating templates as a hard and fast rule is a way to make sure that newer coders make slow sites"
what do you mean specifically by templates?
just a header and footer include?
something like smarty?
in my mind those are the opposing ends of the templating spectrum. I think that newer coders should try to understand where in the spectrum their needs fall. This is a more complex understanding than just "templates save time and money".
I guarantee that your comparison is too basic for the complex question of templates.