-2

I am currently working on a software project for school using PHP, HTML, and CSS. All the examples I find online display the PHP and HTML in the same file. I know that there are advance frameworks that allow me to decouple the PHP and HTML but I am looking for a simple and easy way to focus the responsibilities of the two independently.

How can I separate my HTML and PHP without the use of a framework?

Dov Benyomin Sohacheski
  • 7,133
  • 7
  • 38
  • 64
  • 1
    This has been answered http://stackoverflow.com/questions/23407242/keeping-html-files-separated-from-the-php-files-template-based?rq=1 – Dov Benyomin Sohacheski Apr 18 '16 at 12:01
  • Please tell us what you have tried by yourself. – node_modules Apr 18 '16 at 12:01
  • It sounds like you are talking about a Templating engine rather than a Framework. Be careful about re-inventing this particular Wheel, it may be better to go with one that has already been written rather than write a poor equivalent of the many sizes and shapes of existing Wheels already available. – RiggsFolly Apr 18 '16 at 12:12

2 Answers2

1

I don't really understand why you think this is beneficial to you, but you could separate your HTML and PHP into two files and then require the HTML into your PHP file when neccessary:

In foo.php:

<?php
    // some php code here.
    require('foo.html');
?>

In foo.html:

<!DOCTYPE html>
<html>
...
Luke Joshua Park
  • 9,527
  • 5
  • 27
  • 44
1

It seems you are looking for an MVC patter

M - Model V - View C - Controller.

The model feeds a view with that data it needs and the controller call the business logic to get the right view and data.

With that most php frameworks out there are mvc or can be. Those frameworks include:

laravel, symfony, codeigniter, etc.

If you do not want to tie yourself to a particular framework you could just a few libraries out there to create your own. I use:

Slim - for routing

PHP ActiveRecord - DB access

Twig - Template engine for the view.

nerdlyist
  • 2,842
  • 2
  • 20
  • 32