tags:

views:

145

answers:

6
return'
    <li>
    <a href="nano.com/' . $username . '"><img class="avatar" src="images/' . $picture . '" width="48" height="48" alt="avatar" /></a>
    <div class="tweetTxt">
    <strong><a href="nano.com/' . $username . '">' . $username . '</a></strong> '. autolink($tweet).'
    <div class="date">'.relativeTime($dt).'</div><div class="date">'. $reply_info . '</div> <a class ="reply"  href="home.php?replyto=@'. $username .'&status_id='. $id .'&reply_name=' .$username.'"> reply </a>

    </div>
    <div class="clear"></div>
    </li>';

I was wondering if there is a cleaner way to write this code, and taking in mind processing time, if that really means anything.

p.s. this code is part of a function, this is the return statement!

+3  A: 

Yes. Use double quotes for the PHP string (and single quotes for the HTML attributes), then you can just use PHP variables in the string, like so:

"<a href='nano.com/$username'>";

Is processing time really an issue? I doubt it, but profile to be sure.


Edit: If anyone is unsure about using single quotes in HTML attributes, have a look at this question. It's pretty unanimously agreed that single quotes are fine. If anyone can give a decent counter-argument I'd be happy to hear it.

Skilldrick
Such bad practice! All attribute values should be enclosed in double quotes as is the standard.
Alex
@Alex - If only I could upvote that comment more. We have developers who use single quotes for attribute values, and it makes me lose my mind sometimes.
Joel Etherton
thanks @alex and @joel and @skilldrick, so shall i leave my code the way it is.
getaway
No, more thorough refactoring is needed ;) why would you want to return that blob of html from a method anyway?
thomasmalt
lol @thomasmalt, i dont know it just seemed ideal at the time, basically the function retrieves the users twitter statuses and then echos them in a list hence the "li" list!!!
getaway
what I would do, of the bat, is split the code into a template part, containing just the html and then "decorate" the twitter input with the html.
thomasmalt
@Alex, @Joel, other upvoters: what’s wrong with single quotes? The standard explicitly allows both. Why is it bad practice?
Konrad Rudolph
confused.com, is this way standard or not, it looks pretty neat to me
getaway
I wouldn't say there's anything wrong exactly, but it's just one of those things that should be a consistency for everyone. And as far as my time in the web design industry goes I've never come across any reason to deviate from that standard. It's like using a <i>, <u> or even <b> tag. They're there, but using them is somehow... wrong.
Alex
cheers everyone
getaway
@Alex You should be pragmatic - it's easier to write PHP with double-quoted strings a single-quoted attributes. For a single line heredoc is too much overhead. There's nothing wrong with single-quoted attributes. Nothing whatsoever.
Skilldrick
@Alex - have a read of this: http://stackoverflow.com/questions/273354/html-single-quotes-a-problem
Skilldrick
Can't you just use "<a href=\"nano.com/$username\">"; ?
Dai
@Dai of course you can, but I find it easier to read 'something quoted' than \"something quoted\".
Skilldrick
@Skilldrick - At the end of the day it's down to personal preference. I personally prefer 'something '.$var.' something else' simply because, in my editor, it highlights PHP and fades out HTML allowing me to easily debug/see the PHP snippits mixed in with HTML. However, I'm much more a fan of frameworks over inline PHP anyways
Alex
@Alex Personal preference is fine - "Such bad practice" isn't, when it's not. I'm just doing my best to dispel the FUD about single-quoted HTML attributes.
Skilldrick
@Skilldrick - I am not against the single quoted stuff, I was just pointing out to all the "double quotes or gtfo" people that you can have the best of both worlds here.
Dai
See my question on that: http://stackoverflow.com/questions/2887714/quoting-html-attribute-values
Flavius
+1  A: 

Cleaner template and php code -> use MVC

Anpher
i dnt like the MVC approach i just dnt get it!! thanky anway :))
getaway
@getaway Well you'd do a lot better if you pushed yourself abit more with MVC. It is a major 'gateway' to better write code especially with the problem you are facing.
zaf
@zaf i tried using codeigntier, i understood the concept, but it was more confusing to me than just normal coding, im not really having trouble with this code, i was just curious if thier was a better way to write it!!
getaway
@getaway You can do MVC without a framework.
zaf
@zaf MVC is architecture. It has nothing to do with this code snipped
Col. Shrapnel
@Col. Shrapnel "MVC is architecture" <check> "It has nothing to do with this code snipped (sic)" <check> Q.E.D.
zaf
A: 

I would cut in several pieces and use sprintf() to tie it all together.

Dennis Haarbrink
Why the downvote? It's a viable solution that could make the intent more clear.
Dennis Haarbrink
+1 : This is one reasonable way of cleaning it up. Don't hold your breath for the down voter to explain why this is not useful.
zaf
@zaf well, if you insists. 1. no code example to estimate the real value of such an improvement. sprintf is still ugly approach to encapsulate HTML into PHP into string into function... Leave HTML alone! It's a language too. And it deserves formatting/highlighting. And a programmer, who would read this code, would save some headache for more important task.
Col. Shrapnel
Of course there are better ways than this. To correctly solve the problem his design needs to be reconsidered IMHO. My suggested approach might not fit *your* ideal of good code, and I would agree with you, but it could definitely solve/improve this problem in isolation.
Dennis Haarbrink
@Col. Shrapnel Thank you for returning and leaving a comment! Upvote for you but I'll also keep the upvote to this question.
zaf
+1  A: 

Yes, there is one, and you don't need MVC (only a template):

<li>
 <a href="nano.com/<?=$username ?>">
  <img class="avatar" src="images/<?=$picture ?>" width="48" height="48" alt="avatar" />
 </a>
 <div class="tweetTxt">
  <strong><a href="nano.com/<?=$username ?>"><?=$username ?></a></strong>
  <? echo autolink($tweet) ?>
  <div class="date"><?=relativeTime($dt) ?></div>
  <div class="date"><?=$reply_info ?></div>
  <a class="reply" href="home.php?replyto=@<?=$username?>&status_id=<?=$id?>&reply_name=<?=$username?>">
  reply
  </a>
 </div>
 <div class="clear"></div>
</li>

Must read: http://wiki.yet-another-project.com/php/the_one_single_step_for_a_cleaner_code . It describes how you have to use the code above.

Flavius
Formatted it a bit. People are way underestimate code formatting/highlighting.
Col. Shrapnel
+3  A: 

You could use HEREDOC syntax :

$auto = autolink($tweet);
$rel = relativeTime($dt);
return <<<ENDOFRETURN
    <li>
    <a href="nano.com/$username"><img class="avatar" src="images/$picture" width="48" height="48" alt="avatar" /></a>
    <div class="tweetTxt">
    <strong><a href="nano.com/$username">$username</a></strong> $auto
    <div class="date">$rel</div><div class="date">$reply_info</div> <a class ="reply"  href="home.php?replyto=@$username&status_id=$id&reply_name=$username"> reply </a>
    </div>
    <div class="clear"></div>
    </li>
ENDOFRETURN;
M42
now thats looks so clean
getaway
Could the downvoter explain why ?
M42
is it not standard to use heredoc syntax
getaway
I must admit, I quite like this approach. Tend to keep it for such things as E-Mail templates though. But it'd work just fine.
Alex
Yes, it is.[...]
M42
would i be able to do if statements inside the heredoc
getaway
No, you can't, it is just a way to build a string. You can't neither call a function, that's why I assign 2 variables before.
M42
kool kool, its actually much better that way, so you can do all the processing then neatly echo it which is kool, thanks anyway!!
getaway
@getaway this ugly syntax won't let your editor to highlight HTML code. If you have idea of code highlighting, of course :)
Col. Shrapnel
@col do you suggest another idea?
getaway
@getaway Flavius already suggested down here. If you *really* need to return this code, not output it as it ought to be, you can use ob_start() output buffering to grab PHP code output into a variable
Col. Shrapnel
A: 

You can use a template engine i.e. smarty, twig, ...

ipsum