BorysSokolov Posted March 6, 2013 Share Posted March 6, 2013 (edited) I've always found the task difficult. I use includes and functions wherever I can, but sometimes it seems impossible. Take this code for example: <div> <?php $gotContents = printContents($linkDB, 'home'); foreach($gotContents as $k){ echo "<div class = \"catContents\">"; echo "<div class = \"contentTitle\">".contentTitle($linkDB, $k)[0]."</div>"; echo cutContent($k); echo "</div>"; } ?> </div> Here, there's no way (that I can see) to separate the HTML from the PHP; I can't divide the code into different files since I need parts of it to be encased in DIVs. So, is this type of code acceptable, or is there a better way to handle it? If so, how? Thanks in advance for any replies. EDIT: Also, I'm not interested in using any third-party software/add-on- just raw PHP and HTML please. Edited March 6, 2013 by BorysSokolov Quote Link to comment https://forums.phpfreaks.com/topic/275299-cleanest-way-to-separate-php-and-html/ Share on other sites More sharing options...
Jessica Posted March 6, 2013 Share Posted March 6, 2013 I like to use a templating system like Smarty, but for some reason you don't want to... So... You could use a HEREDOC to use one echo statement, and get the returned value of cutContent() before the echo's. Quote Link to comment https://forums.phpfreaks.com/topic/275299-cleanest-way-to-separate-php-and-html/#findComment-1416868 Share on other sites More sharing options...
Solution haku Posted March 6, 2013 Solution Share Posted March 6, 2013 Templating systems are the way to go. You then generate the data in one file, then pass it through a template which wraps it in HTML (or XML or JSON or whatever you want). Quote Link to comment https://forums.phpfreaks.com/topic/275299-cleanest-way-to-separate-php-and-html/#findComment-1416883 Share on other sites More sharing options...
BorysSokolov Posted March 6, 2013 Author Share Posted March 6, 2013 I like to use a templating system like Smarty, but for some reason you don't want to... So... You could use a HEREDOC to use one echo statement, and get the returned value of cutContent() before the echo's. I've just done some research, and changed my mind. I guess using a template engine really would be the best solution. So, this leads me to another question: which template should I use, as a beginner? Smarty seems to be the standard, I gather? Quote Link to comment https://forums.phpfreaks.com/topic/275299-cleanest-way-to-separate-php-and-html/#findComment-1417027 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.