Jump to content

Cleanest Way To Separate PHP and HTML?


BorysSokolov
Go to solution Solved by haku,

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.

Edited by BorysSokolov
Link to comment
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.

Link to comment
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?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.