Skip to content

Instantly share code, notes, and snippets.

@judasane
Created September 13, 2013 15:38
Show Gist options
  • Save judasane/6552314 to your computer and use it in GitHub Desktop.
Save judasane/6552314 to your computer and use it in GitHub Desktop.
A class to manage basic template for php.
<?php
/**
* Templater class.
*
* @author Juan
*/
class Sanemplater {
/* @var $basePath string */
/* @var $basePath String */
private $basePath = "";
/**
* @param String $basePath ruta de la carpeta
*/
public function __construct($basePath = "") {
if ($basePath != "") {
$this->basePath = "$basePath/";
}
}
/**
* Generates the page from a previously marked template and the values to
* do it.
* @param String $templateName Path to the template
* @param String $folder Folder when the template is
* @param array $dictionary Associative array with pairs mark=>velue to replace
* the marked html code
* @return String Html ready to use
*/
public function makePage($templateName, $dictionary = "", $folder = "") {
//Gets the template file
if ($folder == "") {
$folder = $this->basePath;
}
$template = file_get_contents($folder . $templateName);
return $this->makePageFromTemplate($template, $dictionary);
}
/**
* Generates a page from a given template and an optional dictionary
* @param String $template String to be scanned and filled with data
* @param array $dictionary Data passed to the template in the way data=>value
* @return String String ready to use with the template filled.
*/
public function makePageFromTemplate($template, $dictionary = "") {
if ($dictionary != "") {
foreach ($dictionary as $clave => $valor) {
$template = str_replace('<!--' . $clave . '-->', $valor, $template);
}
}
return $template;
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment