king.oslo Posted February 28, 2010 Share Posted February 28, 2010 Hello my friends, I have been writing php for about 6 months, and have wondered how I can avoid that php ruins my pretty markup. If for example, I want to print a list of numbers with numbers inside of a document, the php clutteres the mark-up. This is not so bad if the example is a simple as that this one, it is much worse for complicated documents, there the php badly clutters my mark up and makes it difficult for me to read the code later: <?php $numbers = array(1,2,3,4,5,6,7,8,9); ?> <html> <head> <title>List of numbers</title> </head> <body> This is a list of numbers: <ul> <?php foreach ($numbers as $value) { print "<li>$value</li>"; } ?> </ul </body> </html> Is there a better way to separate the xhtml elements and the code to make the markup easier to read? Sorry if my terminology is wrong in places, but I hope you understand the question. Thank you for your time. Kind regards, Marius Link to comment https://forums.phpfreaks.com/topic/193619-php-ruins-my-nice-markup/ Share on other sites More sharing options...
seventheyejosh Posted February 28, 2010 Share Posted February 28, 2010 Why is the markup coming out wrong? As long as your loops aren't forgetting closing tags and such why would you code ever randomly create invalid html. As for the XHtml part, I hope you mean html, because if your pages are being served as XHTML, no one using Internet Explorer can view them. Link to comment https://forums.phpfreaks.com/topic/193619-php-ruins-my-nice-markup/#findComment-1019206 Share on other sites More sharing options...
seventheyejosh Posted February 28, 2010 Share Posted February 28, 2010 <ul> <?php foreach ($numbers as $value) { print "<li>$value</li>"; } ?> </ul // <-- Programmer error.... not php Link to comment https://forums.phpfreaks.com/topic/193619-php-ruins-my-nice-markup/#findComment-1019208 Share on other sites More sharing options...
king.oslo Posted February 28, 2010 Author Share Posted February 28, 2010 Sorry, it must be my terminology that is wrong. What I am trying to say is that this is easier to read: <ul> <li>1</li> <li>2</li> <li>3</li> </ul> than <ul> <?php for ($i = 1; $i < 4; $i++) { print "<li>$i</li>\n"; } ?> </ul> especially if there are many loops and variables to be inserted from code, the resulting file starts to look messy and difficult to read. Imagine if I was to go back to it and do changes to the mark-up after -- say 3 years, it would be nice if the documents were clean, and php- and html-code was separate. I hope this makes sense to somebody Thanks Link to comment https://forums.phpfreaks.com/topic/193619-php-ruins-my-nice-markup/#findComment-1019212 Share on other sites More sharing options...
seventheyejosh Posted February 28, 2010 Share Posted February 28, 2010 You can use some sort of templating engine, such as smarty, or write your own. This will keep your code separate from your display file. You could also just comment stuff properly, or keep a folder of notes on each project. Like if someone pays you to mod a login script, make a folder called Projects/Josh's login Mod DEC-12/ and have a file called notes.txt. Inside just be like: 1. Added form validation (validate.php:21) 2. Added email links (email.php:34 / validate.php:45) etc, etc Smarty works kinda like this: //preload.php: $res=mysql_query($query); $row=mysql_fetch_assoc($res); $title=$row['title']; $content=$row['content']; $smarty->assign('pageTitle',$title); $smarty->assign('pageContent',$content); //someView.tpl <html> <head> <title>{$pageTitle}</title> </head> <body> <div> {$pageContent} </div> </body> </html> If you want to write your own, there are plenty of tutorials out there. Link to comment https://forums.phpfreaks.com/topic/193619-php-ruins-my-nice-markup/#findComment-1019215 Share on other sites More sharing options...
king.oslo Posted February 28, 2010 Author Share Posted February 28, 2010 You can use some sort of templating engine, such as smarty, or write your own. This will keep your code separate from your display file. You could also just comment stuff properly, or keep a folder of notes on each project. Like if someone pays you to mod a login script, make a folder called Projects/Josh's login Mod DEC-12/ and have a file called notes.txt. Inside just be like: 1. Added form validation (validate.php:21) 2. Added email links (email.php:34 / validate.php:45) etc, etc Smarty works kinda like this: //preload.php: $res=mysql_query($query); $row=mysql_fetch_assoc($res); $title=$row['title']; $content=$row['content']; $smarty->assign('pageTitle',$title); $smarty->assign('pageContent',$content); //someView.tpl <html> <head> <title>{$pageTitle}</title> </head> <body> <div> {$pageContent} </div> </body> </html> If you want to write your own, there are plenty of tutorials out there. Thanks! Marius Link to comment https://forums.phpfreaks.com/topic/193619-php-ruins-my-nice-markup/#findComment-1019223 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.