Why most theme developers write wrong code like this...
uac_mickset - October 10, 2006 - 18:01
Most people write code as:
<?php if ($sidebar_right) { ?><div id="sidebar-right">
<?php print $sidebar_right ?>
</div><?php } ?>But I think that right code must be written like this:
<?php if ($sidebar_right) print "<div id=\"right_side\">".$sidebar_right."</div>" ?>
I'm explain why at first case code wrong: when user not use left sidebar in genereted page we will see next:
<div id="right_side"></div>
And if all user can write as I say at the second case, then this odd html code not be generated.
So... I'm hope that theme developers read this topic and remember this solution :)

Sure about that?
You might have made a mistake because the only difference between your 2 cases is the choice of syntax which is completely a matter of taste. Case 2 presents the exact same results as Case 1.
-zach
--
harkey design
ups
sorry... I've did error in my second case, this is right:
<?php if ($sidebar_right) print "<div id=\"sidebar-right\">".$sidebar_right."</div>" ?>And you don't understood what I mean! In second case if user disable display for right side bar in generated page you don't see odd html code like:
<div id="sidebar-right"></div>So... I've just place html code in php code in section IF
is that right?
I think you're missing opening and cloing brackets
if condition {do this}Shouldn't it be something like this:
<?php if ($sidebar_right) {print "<div id=\"sidebar-right\">".$sidebar_right."</div>"} ?>or this(more intuitive):
<?php if ($sidebar_right) { ?><div id="sidebar-right"><?print $sidebar_right; ?></div>
<?php } ?>
Phil
Nah. Braces are only
Nah. Braces are only required for a block (multiple) of statements following an if, not for a single statement following an if.
---
www.whatwoulddrupaldo.org
in cycle IF "{" and "}" are
in cycle IF "{" and "}" are not required if used only one operator. In our case we just print values of $sidebar_right, so we can sink {}
PS: I write right code ;)
Not agree
A theme developer may read and remember this but may not agree. The only difference you propose is a different id (and a different way of generating code, but that's not the point, is it?), 'right_side' as opposed to 'sidebar-right'. Imho, 'sidebar-right' is more descriptive.
I don't really see what the possible existence of a left sidebar has to do with it, the div only gets generated when a sidebar-right is used and then it's filled with html.
in my second case if user
in my second case if user don't use right sidebar in generated page you don't see this code:
<div id="sidebar-right"></div>but if developer use firts case - this code be generated. Do you mean what I say? This is odd code that increase page size and possibly do bugs with display (but not sure)
Check your code.
Uhmm.. I'm not much of a coder but even I can tell you that they both produce the same results.
What you posted:
<?php if ($sidebar_right) { ?><div id="sidebar-right"><?php print $sidebar_right ?>
</div><?php } ?>
What your thinking:
<div id="sidebar-right"><?php if ($sidebar_right) { ?>
<?php print $sidebar_right ?>
<?php } ?>
</div>
Look here on coding conventions, especially the last sentence.
http://drupal.org/node/1965
When I first started I worked the other way around. -silly me.
–joon
you just don't understand
you just don't understand what I mean.. Just read my post and test it for examples. If you don't understood it, I've give you 2 examples and you see what be generated html-code in both cases, ok?
Both examples produce the same result
Neither will output anything if there is no sidebar. That's what happens in all the themes I've ever created. And before I used Drupal I used to put large chunks of HTML in PHP if statements like the first example does and it worked fine.
But the first syntax is generally preferred from a templating point of view.
Surely, you mean the right sidebar in that sentence there don't you?
--
Anton
New to Drupal? | Forum posting tips | Troubleshooting FAQ
I understand fully what you
I understand fully what you mean. As it was mentioned already, they both output the same results. You do not get empty div's with the first example since the div is within a conditional statement. Go ahead and check again because your way off.
–joon
He is try to explain that in
He is trying to explain that in the first case an empty div tag is generated, while in the second code it isn't.
First case. if statement is false, a div tag is still generated. Its empty div tag, doesn't do anything. but it still there
Second case, if statement is false, no div tag is generated.
thats what he is saying
although i just tested it. no empty div tag is generated
I understand what you are
I understand what you are trying to explain, but as everone already stated, both code examples produce the exact same result. This is due to the fact that the div's are enclosed in the if-statement in both your examples.
What you where probably trying to say is that most people write code like this:
<div id="sidebar-right"><?php if ($sidebar_right) print $sidebar_right ?>
</div>
thus printing the div's even if there's no sidebar_right defined, which could be avoided by enclosing the div's in the if-statement as well.
Exactly! I think we know
Exactly!
I think we know what you are TRYING to say, but your code doesn't prove your point.
Both of your examples will not print IF there is no sidebar_right = No empty div elements.
You are a coder, you know this ;)
Mike
I have to agree
Mickset,
I have to agree with everyone else. Your examples are pretty much exactly the same. All the HTML is inside the conditional statement, so if the condition is false, nothing will be generated in either case.
I think what you're trying to say is that because the opening of the div isn't sent by a php Print function that it will display regardless of the conditional statement, but that's not true. I use this method all the time when I don't want to bother with making sure any special characters are escaped properly. Remember the PHP code is interpreted by the server, not the browser, the server then sends HTML, not PHP code, to the browser. You could encase an entire page inside a PHP conditional statement like this:
<?phpif ($conditionTest) {
?>
<html>
<body>
my page here
....
blah blah blah
All sorts of HTML all over the place
</body>
</html>
<? } ?>
And if the condition isn't true, your page won't display.
Steel Rat
Drupal Site: RPGMapShare.com