here is the simple PHP template system i have found
The Class (template.class.php)
<?
class Template {
public $template;
function load($filepath) {
$this->template = file_get_contents($filepath);
}
function replace($var, $content) {
$this->template = str_replace("#$var#", $content, $this->template);
}
function publish() {
eval("?>".$this->template."<?");
}
}
?>
The Template File (design.html)
This file will contain the design of your web site and the blank fields that will be merged with content data.
<html>
<head>
<title>#title#</title>
</head>
<body>
<h3>Hello #name#!</h3>
<p>The time is: #datetime#</p>
<? echo "<p>Embedded PHP works too!</p>"; ?>
</body>
</html>
Usage (index.php)
Now we will create a script that load the template and use the class to merge the data.
<?
include "template.class.php";
$template = new Template;
$template->load("design.html");
$template->replace("title", "My Template Class");
$template->replace("name", "William");
$template->replace("datetime", date("m/d/y"));
$template->publish();
?>
Now for a simple wordpress post loop index.php
<?php
get_header();
if (have_posts()) :
while (have_posts()) :
the_post();
the_content();
endwhile;
endif;
?>
Question is, how to use above wordpress loop and the top PHP template system together,
this is to convert an html template into a wordpress theme.
My first answer to your question is another question: Why?
PHP is a template language. You need this separation only in cases where the Designer and the programmer have two very different jobs. But someone writing WordPress themes has to know PHP anyway: Just look at the custom header and background set ups, at
add_theme_support
,wp_enqueue_style
and similar cases where PHP, HTML and CSS are tied together.To answer the how ⦠take a look at TwentyElevenâs
single.php
. There is a part where you could load a template engine:You could instead use:
Some template functions arenât build to be used like this, for example
the_content()
: It offers no parameter to just return a string and it changes the output ofget_the_content()
. So you would have to rewrite some functions just use your extra template engine.For loops, nav menus or widgets it is even harder to separate logic from markup. Yes, it would be nice if there were some separation ⦠but I donât see a way to do this in WordPress.
here is a quick modification of your class to handle a basic loop:
Now just make sure your html has this tokens:
#Loop_Start#
– place before your loop template.#Loop_End#
– place after your loop template.#loop_title#
– place where you want the item title inside your loop template.#loop_content#
– place where you want the item content inside your loop template.for example:
and you should call it like this: