Jump to content

Cleanest Way To Separate PHP and HTML?


BorysSokolov

Recommended Posts

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.

Link to comment
https://forums.phpfreaks.com/topic/275299-cleanest-way-to-separate-php-and-html/
Share on other sites

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?

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.