Newbie Spellweaver
- Joined
- Dec 6, 2011
- Messages
- 11
- Reaction score
- 8
Hello RageZone. As this is one of my firsts posts, I thought that I will start off something that may be useful before I start to ask
1. Create a file named template.class.php (it does not really matter, it just need to be required/included into a document)
2. Put into a file:
Then require a file into your document, so you can use it later
3. Usage: Method 1
To use the method you have 2 choices.
Return the template to a variable:
OR
Echo the template anywhere you want, just do:
4. Usage: Method 2
We can simplify our lives, and do not deal with classes at all, then just define a function called template, under the definition of class in our template.class.php:
And then use it in same two ways as:
OR
5. Summary and example
We have used basics of PHP language, and only. We managed to understand here a little bit of class, and their behaviours.
The way it can be worked with is simply create a index.html in templates folder.
Put content of:
Then create a index.php and fill that in with:
and thats should get it working then.
1. Create a file named template.class.php (it does not really matter, it just need to be required/included into a document)
2. Put into a file:
PHP:
<?php
class template{
private $template_root = 'templates'; // This is the folder where we keep templates
private $template; // This is our template content if we need to return a template into a string or something, instead of echo it
private $template_extension = 'html'; // This is the extension that we are going to use to grab our templates from root
private $pattern = '{c}'; // c is our variable that we will replace it on template assemble
function __construct($filename, $data = array(), $echo_method = false){
$file = $this->template_root.'/'.$filename.'.'.$this->template_extension; // Assign a local variable to initialize the PATH of a file
if(file_exists($file)){ // We checking up if a file exists
$content = file_get_contents($file); // On existence, we grabbing the content from a file, and returning it into a local variable
if(!empty($data) && is_array($data)){ // If $data is not an empty, so we can loop it at least once, and if is an array, to avoid the error message
foreach($data as $variable => $value){
$content = str_replace(str_replace('c', $variable, $this->pattern), $value, $content); // We looping up a whole $data array with given variable patterned name in a html file, and handles a str_replace of that patterned variable to be replaced with value
}
}
if($echo_method) // If we decided to use a echo method, so we need to set a boolean to true
echo $content;
else $this->template = $content; // If we not, the private variable of a class scope getting value of looped template, to can be passed then as a return to a function below. This is because a constructors in PHP cannot return any values
}else{
// You can handle here a exception for not existing template, I chosen to die with an message on missing template, and stop executing the code further
die('You are missing a template file in: '.$file);
}
}
public final function get(){
return $this->template;
}
}
Then require a file into your document, so you can use it later
3. Usage: Method 1
To use the method you have 2 choices.
Return the template to a variable:
PHP:
$template = new template( file*, data*, method* );
$my_template_variable = $template->get();
OR
Echo the template anywhere you want, just do:
PHP:
new template( file* , data*, true);
4. Usage: Method 2
We can simplify our lives, and do not deal with classes at all, then just define a function called template, under the definition of class in our template.class.php:
PHP:
function template($filename, $data = array(), $echo_method = false){
$template = new template($filename, $data, $echo_method);
if(!$echo_method)
return $template->get();
else return null;
new template($filename, $data, $echo_method);
}
PHP:
$my_template_variable = template(file*, data*); // We don't need a method here, because false is our default, in the class and in the function, so we are safe and sure that won't happen dumb any error.
OR
PHP:
template(file*, data*, true);
5. Summary and example
We have used basics of PHP language, and only. We managed to understand here a little bit of class, and their behaviours.
The way it can be worked with is simply create a index.html in templates folder.
Put content of:
Code:
Hello my name is: {name}
Then create a index.php and fill that in with:
PHP:
template('index', array("name"=>"John Smith"), true);
and thats should get it working then.
Attachments
You must be registered for see attachments list
Last edited: