Like many others, you can use MVC, or if you do not want to implement a strict MVC structure, you should still use a template system.
This does not mean that you need to learn smarter, you can write your own template system using only the function that you really need.
If you are not working with designers and performance is not the first thing, you can create an html file with simple placeholders where the dynamic content should go, and then replace it with php (str_replace, preg_replace) .. but it will be slower your application.
Example:
and your template file:
$title = 'Hello world'; if($id == 2){ $content = get_content(); }else{ $content = get_another_content(); } //really, this is just as example ;) ob_start(); include('template.html'); $output = ob_get_clean(); echo str_replace( array('@[title]', '@[content]'), array($title, $content), $output );
This is a really basic example and has 2 problems:
- Performance
- Designers should be instructed NOT to touch the placeholders.
A simpler solution could be:
//html template <title><?php echo $title; ?></title> <body> <?php echo $content; ?> </body>
and php:
$title = 'Hello world'; if($id == 2){ $content = get_content(); }else{ $content = get_another_content(); } include('template.php');
But echo-html should be reduced as much as possible, it is not good practice and combines logic with content Logic is logic, data is data, and life is good
Strae
source share