News:

Want to get involved in developing SMF, then why not lend a hand on our github!

Main Menu

Why Not Use PHP's Alternate Syntax For Templates?

Started by user2037, September 20, 2007, 08:05:19 AM

Previous topic - Next topic

user2037

Doing so would remove the need for escaping quotes and encourage coders to keep business logic out of templates. It should also make designers lives easier as most editing software can handle syntax highlighting outside quoted strings.

php.net/manual/en/control-structures.alternative-syntax

Dannii

It's slower. It also hardly makes a difference, and won't make any designers I know lives easier.
"Never imagine yourself not to be otherwise than what it might appear to others that what you were or might have been was not otherwise than what you had been would have appeared to them to be otherwise."

user2037

Quote from: eldʌkaː on September 20, 2007, 08:12:30 AM
It also hardly makes a difference

I'll agree to disagree here. The appearance of template files is radically simpler from a non-coder's point of view. This is especially true if the presentation logic is separated from the business logic with a thoughtful design.

Compare a relatively simple example (imaging you know nothing about PHP)

<?php
echo '<html>
<h1>'
;
if (
$context['user']['is_logged']) {
  echo 
'Hello '$context['user']['name'];
}
else {
 echo 
'Hi stranger, please log-in';
}
echo 
'</h1>
</html>'
;
?>



With

<html>
<h1>
<?php if ($user_logged_in): ?>
  Hello <?= $user_name ?>
<?php else: ?>
  Hi stranger, please log-in
<?php endif; ?>
</h1>
</html>

Dannii

You're changing a lot more than just the syntax there...
"Never imagine yourself not to be otherwise than what it might appear to others that what you were or might have been was not otherwise than what you had been would have appeared to them to be otherwise."

Dragooon


Kindred

This is the way that Joomla and mambo run their templates..

and the first thing I do with any template I get for those systems is to convert it to straight php with echo statements instead of dropping and and out of php statements mixed with HTML.
Слaва
Украинi

Please do not PM, IM or Email me with support questions.  You will get better and faster responses in the support boards.  Thank you.

"Loki is not evil, although he is certainly not a force for good. Loki is... complicated."

Bulakbol

For me, the html mixed with php is confusing to read. And I don't like seing <? and ?> all over the place. Maybe because I am a beginner.
Please do not PM me for support · My Mods and Theme · SMF Coding Guidelines · Modifications Approval Guidelines

Rudolf

Quote from: user2037 on September 26, 2007, 11:05:47 AM
This is especially true if the presentation logic is separated from the business logic with a thoughtful design.

You are misinterpreting the separating the business logic from the presentation logic concept. It is not about separating one scripting language from the other. It is about separating the logic. Which is not done in either of the two examples you quote. You just separate the two languages, and not even very good, because they are still mixed.

In a system using any kind of templating mechanism the source files take care of the programming tasks needed to perform your busniess logic, while he template files take care of the presentation logic. Most of the times the presentation logic needs programming too, and that's when you start mixing php in your html. It's not bad, it's downright necessary to do so.
The argument of separating PHP from HTML for the designers making things easier is not really correct. Look at Smarty. For a designer to use it he/she has to learn a new syntax, and then still has to learn some programming concepts: variables, control structures, loops and whatnot. With the same effort you can simply explain to the designer how the echo command works. Believe me it's extremely simple :P, and those who can't understand it don't deserve to be called designers. HTML is much harder to grasp then echo.

So there's really no good reason for not using plain php with echo statements. Just make sure that you separate the business logic from the templates. (Don't do database queries, any data manipulation etc.)
I will update all my mods in the next few weeks. Thanks for your patience.

SVG-Collapse (you need an SVG compliant browser)

motumbo

Quote from: Kindred on September 26, 2007, 01:14:21 PM
This is the way that Joomla and mambo run their templates..

and the first thing I do with any template I get for those systems is to convert it to straight php with echo statements instead of dropping and and out of php statements mixed with HTML.

This is also the way Wordpress does its templates.  As a result, Wordpress is much easier to customize than SMF.  A child could figure it out.  It's that simple. 

Well, Wordpress also doesn't use tables for layout nor have javascript embedded in the templates here and there and everywhere.  Clutter complicates things in SMF.

Personally, I lean toward the echo out everything but do mix straight HTML in from time to time, depending how much php is in there.  It's just easier to tweak when you don't have to worry about where echoed statements end and begin.

<div>
<?php time(); ?>
</div>

Is easier for me than:

echo '
<div>',
php time(),
'</div>';

If I have a lot of HTML and a little bit of PHP, I use the "alternate syntax".

Rudolf

Much easier for whom? Frankly, I do not really care to make things understandable for "children". You see, many times to do something so even a child can understand requires that I limit myself to the basic things. I stopped a long time caring, I just do what needs to be done without worrying that people can't understand it and/or customize it.
As I already said, anyone who can't understand those small concepts or take the time to learn some basic rules then it doesn't deserves to have a website. I'm tired of seeing people who made a website using some tool and pretend to be web designers, web programmers and whatnot.

If you are talking about a source code so messy that it's impossible to understand it, that's another story. I've seen projects (and have to work now on some) that are a complete mess code-wise.
SMF is quite linear and easy to modify to your needs. You just need to do your homework. is that too much to ask?
I will update all my mods in the next few weeks. Thanks for your patience.

SVG-Collapse (you need an SVG compliant browser)

Dannii

Can you give an example in the alternate syntax with foreach statements? (As they're the big ones used in SMF)
"Never imagine yourself not to be otherwise than what it might appear to others that what you were or might have been was not otherwise than what you had been would have appeared to them to be otherwise."

user2037

I'll agree that pages with a lot of PHP may not see much benefit. However, templates typically consist of mostly non-PHP markup. And while templates require some programming its best to minimize that so that the non-template-related code stays out of templates (as it simplifies changes).

Regarding child-friendliness, simpler and easier to read is always better. There is a time to hack things together and a time to carefully design. Prototyping can be sloppy but maintenance requires consistency and readability. Typically it takes much longer to debug than to write from scratch so you almost always save time by carefully designing something first.

Foreach example:

<ul>
<?php foreach($records as $record): ?>
<li><?= $record ?></li>
<?php endforeach; ?>
</ul>

PHP.net's documentation has more examples.

Dannii

It's too simple, and doesn't show what would actually be used for SMF. Please convert something major from Display.template.php or whatever, cause I'm not convinced at all that it's any simpler or easier to understand. Especially not if you have to have PHP tags on each line.
"Never imagine yourself not to be otherwise than what it might appear to others that what you were or might have been was not otherwise than what you had been would have appeared to them to be otherwise."

Ben_S

Quote from: motumbo on September 26, 2007, 07:11:51 PM
This is also the way Wordpress does its templates.  As a result, Wordpress is much easier to customize than SMF.  A child could figure it out.  It's that simple. 

Frankly, I'd expect a simple blog to be easier to customise.
Liverpool FC Forum with 14 million+ posts.

Kindred

and your for each example has now doubled the character needed to do that code and IMO made it much MORE complicated that the original, with more chances to make a mistake and mis a closing tag somewhere.
Слaва
Украинi

Please do not PM, IM or Email me with support questions.  You will get better and faster responses in the support boards.  Thank you.

"Loki is not evil, although he is certainly not a force for good. Loki is... complicated."

Nordoelum


Dragooon

Because WordPress is not SMF. You really wont be able to do this due to the mind blowing foreach loops.

Grudge

I have to say that I have a general leaning towards changing (Or at least co-supporting) a different means of templating for SMF - but my only desire for this is to simplify template creation for authors such that they can edit them properly with HTML editors and also not have to worry about escaping characters etc. My intention would be an XML type markup like:


<smf:for subject="boards" value="board">
<div>
  {%board_name}: {$board}
</div>
</smf:for>


But this would effectively just be an abstracted layer which would then get "compiled" into the PHP templates we already use as they are by far the fastest way to produce a template.

I don't see that going into and out of the PHP parsar would provide any benefit at all for SMF. The problem is SMF uses *lots* of PHP - every text string (etc) requires it and as such for the average page you'll be going into and out of the parsar every line or two. Going into and out of the parsar is slower than a joined echo statement and hence I don't see the benefit myself. Don't get me wrong - I'm all up for simplifying the authoring process - but I don't think the suggestion here achieves that goal.
I'm only a half geek really...

Rudolf

Quote from: Grudge on September 27, 2007, 09:14:30 AM

<smf:for subject="boards" value="board">
<div>
  {%board_name}: {$board}
</div>
</smf:for>


But this would effectively just be an abstracted layer which would then get "compiled" into the PHP templates we already use as they are by far the fastest way to produce a template.

Another take on the Smarty model. So you think that people who are not willing to learn a few simple rules will learn this new syntax? And how would this fair with the text editor and highlighting.  Being an XML-like syntax maybe some would color it, but how?

Some editors can highlight html text in php strings. :/ Why people shouldn't use those?
I will update all my mods in the next few weeks. Thanks for your patience.

SVG-Collapse (you need an SVG compliant browser)

motumbo

Quote from: Rudolf on September 26, 2007, 07:42:17 PM
Much easier for whom? Frankly, I do not really care to make things understandable for "children". You see, many times to do something so even a child can understand requires that I limit myself to the basic things. I stopped a long time caring, I just do what needs to be done without worrying that people can't understand it and/or customize it.

This is the exact type of attitude that people cannot stand about SMF.  It's pure ego-driven arrogance and condescension. 

This type of arrogance has actually done more to hold SMF back than anything.  Take a look at the posts of [Unknown] from 2003 or 2004 when he was ARGUING with people that SMF had absolutely no need for a visual verification system.  People were clamoring for it, he was denying it, then eventually, it came to be--but only in a partial manner.  It took years to overcome that stubborn arrogance.  But it was overcome.


What you fail to realize, Rudolf, is that a huge portion of the users of SMF are NOT proficient in PHP.  I have prior programming knowledge in VB and C++, so learning PHP was mainly a matter of learning the syntax.  But EVERYTHING I've ever learned about programming is pretty much the opposite of what I find in SMF and what I see in your attitude.  For example, code should be:

* Well commented so that any coder can read the comments and know what is happening right away.

* Broken up into discrete parts, each of which does one simple thing.

* Designed for readability over terseness. 


'collapse_href' => isset($row_board['canCollapse']) ? $scripturl . '?action=collapse;c=' . $row_board['ID_CAT'] . ';sa=' . ($row_board['isCollapsed'] > 0 ? 'expand' : 'collapse;') . '#' . $row_board['ID_CAT'] : '',


There's no commenting on the above.  Is that easy for people to understand?

Quote from: Rudolf on September 26, 2007, 07:42:17 PM
As I already said, anyone who can't understand those small concepts or take the time to learn some basic rules then it doesn't deserves to have a website.

Uh, OK...   :-\

Quote from: Rudolf on September 26, 2007, 07:42:17 PM
I'm tired of seeing people who made a website using some tool and pretend to be web designers, web programmers and whatnot.

If you are talking about a source code so messy that it's impossible to understand it, that's another story. I've seen projects (and have to work now on some) that are a complete mess code-wise.
SMF is quite linear and easy to modify to your needs. You just need to do your homework. is that too much to ask?

Yes.  Doing homework involves MANY hours of intense code study.  As I've said before, I can look at Wordpress and know exactly what's going on and how stuff works.  With SMF?  Hell, SMF still uses ancient tables for layout.  My goodness, you make one little change and you screwed up the layout.  Then you've got to trackback through the Source file to find out where that mystery <tr> is coming from.  Then there's javascript embedded in the template.  Just an ugly mess!

It's a good thing SMF is free.  Because if SMF were a business, it wouldn't last long by ignoring its user base the way SMF currently does.  Look at how many people have trouble "integrating" SMF into their existing website themes and the resistance to making it easier.

I used to work for a company that was run by total and complete idiots.  I would always argue that if the time was spent to do things right on the front end, it would mean less time and money fixing things on the back end.  No matter how much I argued or how many actual, real changes I made to reduce labor costs, my pleas fell on deaf ears.  Since I stopped working for that company they've seen revenue decrease by over $300 million dollars and lost many productive employees.  I'm not claiming that me leaving is the reason for that.  But I know I was fighting to push things in the right direction--which is what the people who generated the revenue for the company wanted.

Reading your post, Rudolf, made me feel like I was back working for that company.
 

Advertisement: