Question: How can I convert a HTML template into a live php-editable template? I'd like to be able to set the title, description, add images and links. (something like the behance uploading system)

Can someone link me to a tutorial?
Thanks a lot!

Answer: 
To do this in a simple way, you should follow this 3 steps :
1- Add custom tags in your HTML template
2- Create a class to make your HTML template writable
3- Load the class, write your template, display the page

1. First, insert some custom tags in your HTML template (template.html), like this :
2. Then, create a quick class (class.php) to make your custom tags writable :

```
    class Template { var $contents;

    function load($file) {
        if ($fp = fopen($file, "r")) {
            $this->contents = fread($fp, filesize($file));
            fclose($fp);
        }
    }

    function replace($str,$var) {
        $this->contents = str_replace("<".$str.">",$var,$this->contents);
    }

    function show() {
        $search = array(
                '/\t/', //Remove Tabs
                '/<!--[^\[-]+?-->/', //Remove Comments
                '/\n\n/' //Remove empty lines
                );
            $replace = array(
                '',
                '',
                ''
                );
        $this->contents = preg_replace($search, $replace, $this->contents);
        echo $this->contents;
    }
}

```

Once this is done, you must create a function to write inside your tags. In my example, to be able to write your `<page_title>` tag, add the following code to your class.php file :

```
function writetitle($s) {
    $GLOBALS['writes']++;
    $GLOBALS['page_title'] .= $s;
    return;
}

```

1. Finally, all you need is to do is to create your page.php. Load the class, write what you want, and display the result.

Something like : 

```
<?php
    require_once('class.php');    //Load Class
    $template = new Template;    
    $template->load("template.html"); //Load HTML template

    //Some query :
    $query = mysql_query('SELECT...');
    $res = mysql_num_rows($query);

    writetitle('my page title went live!, '.$res.'');  //write your content

    $template->show(); //Generate the page
?>

```

*writetitle* now acts as an *echo*, so you can make queries and everything you want.

In the end you have 3 files :
tempalte.html : your template
class.php : your template engine
page.php : an example page usging your template.

hope it helps. ;)
1. Save the .html file as .php
2. In your PHP, load the data you want to populate the template with (this is a huge topic, and foundational to learning PHP; not something I can answer for you on SO)
3. Echo out your loaded variables into your template. A beginner's approach would be like this. In "example.php":

---

```
<?php
//1. Populate the value from PHP somehow, such as from the GET variables in your HTTP request
$personsNameFromPHP = $_GET['personsNameFromGETVariables'];

//2. Echo the variable out in your markup like this:
?>
<div class="personsName">
<?php echo $personsNameFromPHP; ?>
</div>

```

W3Schools is a decent starting point. If you already know PHP syntax start by learning how MySQL databases work and how PHP accesses them. If you don't know PHP, W3Schools has some links there as well.

<http://www.w3schools.com/PHP/php_mysql_intro.asp>

HTH.