begin process at 2010 02 10 17:36:41
  Trouver un code source :
 
dans
 
Accueil > 

Code

 > 

Class et Objet ( POO )

 > TEMPLATE ENGINE SIMPLE ET PUISSANT. EXTENTION DE FONCTIONS PAR PLUGINS (FDML PARSER).

TEMPLATE ENGINE SIMPLE ET PUISSANT. EXTENTION DE FONCTIONS PAR PLUGINS (FDML PARSER).


 Information sur la source

Note :
10 / 10 - par 1 personne
10,00 / 10

  • 1

  • 2

  • 3

  • 4

  • 5

  • 6

  • 7

  • 8

  • 9

  • 10
Catégorie :Class et Objet ( POO ) Classé sous :template, engine, fdml, parser, smarty Niveau :Initié Date de création :08/02/2009 Vu / téléchargé :3 644 / 218

Auteur : BlackWizzard

Ecrire un message privé
Site perso
Ce membre participe au partage de revenus publicitaires
Commentaire sur cette source (16)
Ajouter un commentaire et/ou une note


 Description

*** Français: ***
Si vous cherchez un template engine en PHP, voici votre solution!

Un template engine, leger, flexible, mais tres puissant.
Pas de code php dans vos template, mais des tags html avec un namespace <fdml:tagname>.

Des fonctions sont intégrées au template engine: variables (sous la forme %var% coté template), boucles, sections (mettez plusieurs templates dans un fichier, et selectionnez quel partie du fichier vous souhaitez utiliser via useSection() ), affichage conditionnel (utilisez show() pour afficher ou pas une portion de code), ...

Mais l'intérêt de ce template engine, est la flexibilité: Vous pouvez creer vos propres tags sous forme de plugin; Une simple fonction php sous la forme codeparser_tagname().
Le contenu du tag sera remplacé par ce que cette fonction retourne. Pas besoin de declarer la fonction, ou de faire un include. PLacez le fichier qui la contient dans le repertoire /plugins/ (modifiable en runtime) et le parser va la trouver et la charger pour vous.

Le fichier zip contient une série d'exemples divers, simple et complexes.


*** English: ***

If you are searching for a new template parser for your PHP projects, here is your solution!

FDML stands for Flexible Dynamic Markup Language.

Instead of having nasty php code in your templates, or weird template tags (like smarty's tags for example), the FDML language is based on HTML. That mean you will have tags like <fdml:tagname arg="value" /> in your template.

To control your template from your php code, you have a few functions like: Loop() -> Loop the portion of code located between the <fdml:loop name=""> </fdml:loop> tags. Show() -> Display the portion of code located between the <fdml:show name=""> </fdml:show> tags. If the function is not called, the portion of code will not be displayed. UseSection?() -> Use only a section of the template as the main template. Allow you to put many templates in a single file. AddVar?() -> allow you to define and replace a variable in the template. Variable name are enclosed in % sign in the template. Example: %variable% will be replaced by addVar("variable","value");

But this parser is more interesting than other solution because it allows you to create your own tags; If you need for example a new function in your template, let say, you want a piece of code to be displayed only is the current user is an admin. Instead of putting in your template a lot of code based on conditions, simply create the <fdml:is_admin> tag! When the parser will find this unknown tag, it will automatically search for a function named fdml_is_admin() in the /plugins/ directory, and simply replace the content of the tag by the return of this function. The content of the tag and its attributes will be send to the function as an array.

Oh, and did I mentioned the parser is composed of just one single file with no dependency?

Source

  • <?php
  • /*
  • * @Created: v2 - 15 avr. 07 - 14:25:10
  • * v3 - 20 janv. 09 - 13:44:00
  • * @author: blackwizzard <blackwizzard@gmail.com>
  • * @project: FDML - Flexible Dynamic Markup Language
  • * @filename: fdml.parser.php
  • */
  • class codeparser {
  • // declarations
  • var $_instance;
  • var $_vars;
  • // Constructor
  • function codeparser($file, $pluginDir=null) {
  • /** vars **/
  • $this->_vars = array();
  • $this->_vars["buffer"] = "";
  • $this->_vars["log"] = array();
  • $this->_vars["log"]["file"] = $file;
  • $this->_vars["log"]["tags"] = array();
  • $this->_vars["vars"] = array();
  • $this->_vars["functionbuffer"] = array();
  • $this->_vars["namespace"] = "fdml"; // you can change the namespace here
  • $this->pluginfolder = "plugins/"; // You can change the plugin directory here
  • if ($pluginDir!=null) $this->pluginfolder = $pluginDir;
  • // functions buffers
  • $this->_vars["functionbuffer"]["loop"] = array();
  • $this->_vars["functionbuffer"]["loopbuffer"] = array();
  • $this->_vars["functionbuffer"]["onemptyloop"] = array();
  • $this->_vars["functionbuffer"]["onemptyloopbuffer"] = array();
  • $this->_vars["functionbuffer"]["show"] = array();
  • $this->_vars["functionbuffer"]["showbuffer"] = array();
  • $this->_vars["functionbuffer"]["section"] = array();
  • $this->_vars["functionbuffer"]["sectionbuffer"] = array();
  • $this->_vars["functionbuffer"]["savedloop"] = array();
  • $this->_vars["functionbuffer"]["savedloopbuffer"] = array();
  • /** read the template file **/
  • $this->_vars["buffer"] = $this->freadFile($file);
  • /** plugin loader **/
  • $this->loadPlugins("preparser");
  • $this->loadPlugins("dynamics");
  • $this->_vars["buffer"] = $this->searchtags($this->_vars["buffer"]);
  • }
  • /**
  • * Output the parsed template
  • *
  • * @return Template buffer
  • *
  • */
  • function output() {
  • $this->applyLoops();
  • return $this->_vars["buffer"];
  • }
  • function applyLoops() {
  • // apply loops
  • foreach ($this->_vars["functionbuffer"]["loop"] as $loopName=>$loopOutput) {
  • if ($loopOutput == "") {
  • $loopOutput = $this->_vars["functionbuffer"]["onemptyloopbuffer"][$loopName];
  • }
  • // required...
  • $loopOutput = $this->searchtags($loopOutput);
  • $this->_vars["buffer"] = str_replace("<loop_$loopName/>",$loopOutput,$this->_vars["buffer"]);
  • }
  • }
  • function applySavedTags() {
  • $this->applyLoops();
  • $regex = '#<savedloop id\=[\"]([\w-]+)[\"] name\=[\"]([\w-]+)[\"] \/>#mis';
  • while(preg_match($regex, $this->_vars["buffer"], $match)) {
  • $loop_id=$match[1];
  • $loop_name=$match[2];
  • $this->_vars["buffer"] = preg_replace("#{$match[0]}#", "<".$this->_vars["namespace"].":loop name=\"$loop_name\">".$this->_vars["functionbuffer"]["savedloopbuffer"][$loop_id]."</".$this->_vars["namespace"].":loop>", $this->_vars["buffer"]);
  • }
  • $this->parseAgain();
  • }
  • function parseAgain() {
  • $this->_vars["buffer"] = $this->searchtags($this->_vars["buffer"]);
  • }
  • /**
  • * Search and parse all FDML tags
  • *
  • * @return pre-parsed template
  • *
  • */
  • function searchtags($in) {
  • // match all tags in the form <type:name args>content</type:name> and singleton <type:name args />
  • // old versions:
  • //$reg = "#<([a-z]+):(.*?)\s+(.*?)\s*(>(.*?)</\\1:\\2>|/>)#mis"; // global regex to match all tags with all namespaces.
  • //$reg = "#<".$this->_vars["namespace"].":(.*?)\s+(.*?)\s*(>(.*?)</".$this->_vars["namespace"].":\\1>|/>)#mis";
  • //$reg = "#<".$this->_vars["namespace"].":(.*?)\s+(.*?)\s*(>(.*?)</".$this->_vars["namespace"].":\\1>|/>)#mis";
  • $reg = "#<".$this->_vars["namespace"].":([a-zA-Z0-9_-]+)\s*((?:(?!>).)*)\s*(>((?:(?!".$this->_vars["namespace"].":\\1).)*)</".$this->_vars["namespace"].":\\1>|/>)#mis";
  • if (preg_match($reg, $in)) {
  • $in = preg_replace_callback($reg,array($this,"tagparser"),$in,-1);
  • return $this->searchtags($in);
  • //return $in;
  • } else {
  • return $in;
  • }
  • }
  • /**
  • * Parse a single tag
  • *
  • * @return the parsed tag
  • *
  • */
  • function tagparser($array) {
  • $query = $array[0];
  • //$tagType = $array[1];
  • $tagName = $array[1];
  • $tagArgs = $array[2];
  • $tagContent = $array[4];
  • $argsArray = array();
  • $tagArgs = str_replace("\\\"","[[QUOTE]]",$tagArgs);
  • // attribute parser
  • preg_match_all("#([a-z1-9]+)\s*=(([a-z0-9_-]+)|[\"?]([^\"]*)[\"?])#mis",$tagArgs,$args);
  • foreach ($args[1] as $id=>$var) {
  • $argsArray[$var] = $this->correctValue($args[2][$id]);
  • }
  • array_push($this->_vars["log"]["tags"],array(
  • "Tag name"=>$tagName,
  • "String args"=>$tagArgs,
  • "Parsed args"=>$argsArray,
  • "Complete tag"=>$query,
  • "Content"=>$tagContent
  • ));
  • switch (strtolower($this->superTrim($tagName))) {
  • case "loop":
  • // a loop...
  • if ($argsArray["waitforparsing"] == "true") {
  • // put the content in the function buffer
  • $this->_vars["functionbuffer"]["savedloopbuffer"][$argsArray["id"]] = $tagContent; // saved loop buffer using the loop ID. The localization loop name is saved in the tag itself.
  • // replace by a localization tag
  • return "<savedloop id=\"".$argsArray["id"]."\" name=\"".$argsArray["name"]."\" />";
  • } else {
  • // put the content in the function buffer
  • $this->_vars["functionbuffer"]["loop"][$argsArray["name"]] = ""; // output of the loop is empty
  • $this->_vars["functionbuffer"]["loopbuffer"][$argsArray["name"]] = $tagContent; // loop buffer
  • // replace by a localization tag
  • return "<loop_".$argsArray["name"]."/>";
  • }
  • break;
  • case "onemptyloop":
  • // emptyloop...
  • $tagContent = $this->searchtags($tagContent);
  • // put the content in the function buffer
  • $this->_vars["functionbuffer"]["onemptyloop"][$argsArray["name"]] = ""; // output of the onemptyloop is empty
  • $this->_vars["functionbuffer"]["onemptyloopbuffer"][$argsArray["name"]] = $tagContent; // onemptyloop buffer
  • // strip the tag
  • return "";
  • break;
  • case "section":
  • // a section tag...
  • $tagContent = $this->searchtags($tagContent);
  • // put the content in the function buffer
  • $this->_vars["functionbuffer"]["section"][$argsArray["name"]] = ""; // output of the section is empty
  • $this->_vars["functionbuffer"]["sectionbuffer"][$argsArray["name"]] = $tagContent; // section buffer
  • // replace by a localization tag
  • return "<section_".$argsArray["name"]."/>";
  • break;
  • case "show":
  • // a show tag...
  • $tagContent = $this->searchtags($tagContent);
  • // put the content in the function buffer
  • $this->_vars["functionbuffer"]["show"][$argsArray["name"]] = ""; // output of the section is empty
  • $this->_vars["functionbuffer"]["showbuffer"][$argsArray["name"]] = $tagContent; // section buffer
  • // replace by a localization tag
  • return "<show_".$argsArray["name"]."/>";
  • break;
  • default:
  • // Unknown type. Probably a plugin. Else, strip the tag.
  • if (function_exists("codeparser_$tagName")) {
  • $function = "codeparser_$tagName";
  • $return = $function($tagContent, $argsArray);
  • $return = $this->searchtags($return);
  • return $return;
  • } else {
  • $tagContent = $this->searchtags($tagContent);
  • return $tagContent;
  • }
  • break;
  • }
  • }
  • function parseAndApply($option=null) {
  • $functions = get_defined_functions();
  • $functions = $functions["user"];
  • switch ($option) {
  • case "before":
  • foreach ($functions as $function) {
  • if (substr($function,0,strlen("preparser_")) == "preparser_") {
  • $func = $function;
  • $this->_vars["buffer"] = $func($this->_vars["buffer"], $this);
  • }
  • }
  • break;
  • case "after":
  • foreach ($functions as $function) {
  • if (substr($function,0,strlen("postparser_")) == "postparser_") {
  • $func = $function;
  • $this->_vars["buffer"] = $func($this->_vars["buffer"], $this);
  • }
  • }
  • break;
  • }
  • }
  • /**
  • * Register a template variable
  • *
  • * @param String the variable name without the variable identifier (for variable "%var%", just "var")
  • * String The value of the variable
  • * @return nothing
  • *
  • */
  • function addVar($label, $value) {
  • $this->_vars["vars"][$label] = $value;
  • $this->_vars["buffer"] = str_replace("%".$label."%",$value,$this->_vars["buffer"]);
  • }
  • /**
  • * Register more than one template variable at a time, in an array
  • *
  • * @param Array an array of variables type array("var1"=>"value1","var2"=>"value2")
  • * @return nothing
  • */
  • function addVars($array) {
  • foreach ($array as $varLabel=>$varValue) {
  • $this->addVar($varLabel, $varValue);
  • }
  • }
  • /**
  • * Loop a code section
  • *
  • * @param String the loop name as defined in the template
  • * Array an array of variables type array("var1"=>"value1","var2"=>"value2")
  • * @return Template buffer
  • *
  • */
  • function loop($name, $array) {
  • global $_PARSER;
  • $buffer = $this->_vars["functionbuffer"]["loopbuffer"][$name];
  • foreach ($array as $label=>$value) {
  • $buffer = str_replace("%".$label."%",$value,$buffer);
  • }
  • //$this->debug($name, $buffer);
  • //$buffer = $this->searchtags($buffer);
  • $this->_vars["functionbuffer"]["loop"][$name] .= $buffer;
  • }
  • /**
  • * Function used to handle the <fn:section> tag. Any call to this function will replace the current buffer with the content of the <fn:section> tag.
  • *
  • * @param String the name of the section to load, defined on the tag as name="[name]"
  • * @return true
  • */
  • function useSection($sectionName) {
  • global $_PARSER;
  • $tmpBuffer = $this->_vars["functionbuffer"]["sectionbuffer"][$sectionName];
  • $this->_vars["buffer"] = $tmpBuffer;
  • $this->parseAndApply();
  • return true;
  • }
  • /**
  • * Function used to handle the <fn:show> tag. It will result as the display of the content of the tag.
  • * If a tag is not called, it will be deleted before any output.
  • *
  • * @param String the name of the <fn:show> tag to load, defined on the tag as name="[name]"
  • * @return true
  • */
  • function show($name) {
  • global $_PARSER;
  • $tmpBuffer = $this->_vars["functionbuffer"]["showbuffer"][$name];
  • $this->_vars["buffer"] = str_replace("<show_$name/>",$tmpBuffer,$this->_vars["buffer"]);
  • $this->parseAndApply();
  • return true;
  • }
  • function loadPlugins($type) {
  • $pluginfolder=$this->pluginfolder;
  • if ($handle = opendir($pluginfolder.$type)) {
  • while (false !== ($file = readdir($handle))) {
  • if ($file != "." && $file != "..") {
  • if (!is_dir($pluginfolder.$type."/".$file) && strtolower($this->STRING_get_file_ext($file))=="php") {
  • @include_once($pluginfolder.$type."/".$file);
  • }
  • }
  • }
  • closedir($handle);
  • }
  • $functions = get_defined_functions();
  • $functions = $functions["user"];
  • switch ($type) {
  • case "preparser":
  • foreach ($functions as $function) {
  • if (substr($function,0,strlen("preparser_")) == "preparser_") {
  • $func = $function;
  • $this->_vars["buffer"] = $func($this->_vars["buffer"], $this);
  • }
  • }
  • break;
  • case "postparser":
  • foreach ($functions as $function) {
  • if (substr($function,0,strlen("postparser_")) == "postparser_") {
  • $func = $function;
  • $this->_vars["buffer"] = $func($this->_vars["buffer"], $this);
  • }
  • }
  • break;
  • }
  • }
  • function correctValue($value) {
  • $firstChar = substr($value,0,1);
  • $lastChar = substr($value,strlen($value)-1,1);
  • if ($firstChar == "\"" && $lastChar == "\"") {
  • // remove quotes
  • $value = substr($value,1,strlen($value)-2);
  • }
  • $value = str_replace("[[QUOTE]]","\"",$value);
  • $value = str_replace("\\>",">",$value);
  • return $value;
  • }
  • function freadFile($file) {
  • if (!$handle = fopen ($file, "r")) {
  • exit;
  • }
  • $contents = fread ($handle, filesize($file)+1);
  • fclose($handle);
  • return $contents;
  • }
  • function STRING_get_file_ext($filename) {
  • if (strrpos($filename, '.') >= 1) {
  • return strtolower(substr($filename, strrpos($filename, '.') + 1));
  • } else {
  • return "";
  • }
  • }
  • function debug($label, $value) {
  • if (is_array($value)) {
  • echo "<b>$label</b> :: <div style=\"border:1px dashed #000000;margin-left:20px;\">".nl2br(str_replace(" ","&nbsp;",str_replace("<","&lt;",print_r($value,true))))."</div><br>";
  • } else {
  • echo "<b>$label</b> :: <div style=\"border:1px dashed #000000;margin-left:20px;\">".nl2br(str_replace(" ","&nbsp;",str_replace("\t"," ",$value)))."</div><br>";
  • }
  • }
  • function tagLog() {
  • $this->debug("Tag Log",$this->_vars["log"]["tags"]);
  • }
  • function superTrim($in) {
  • /** remove extra spaces, line breaks, tabs **/
  • $in = str_replace("\t"," ",$in);
  • $in = str_replace("\r"," ",$in);
  • $in = preg_replace('/\s\s+/', ' ', trim($in));
  • return $in;
  • }
  • function encodeAsArg($in) {
  • $in = str_replace("\"","\\\"",$in);
  • $in = str_replace("}","\\}",$in);
  • return $in;
  • }
  • }
  • ?>
<?php
/*
 * @Created:	v2 - 15 avr. 07 - 14:25:10
 *				v3 - 20 janv. 09 - 13:44:00
 * @author:		blackwizzard <blackwizzard@gmail.com>
 * @project:	FDML - Flexible Dynamic Markup Language
 * @filename:	fdml.parser.php
 */

class codeparser {
	
	// declarations
	var $_instance;
	var $_vars;
	
	// Constructor
	function codeparser($file, $pluginDir=null) {
		
		/** vars **/
		$this->_vars						= array();
		$this->_vars["buffer"] 				= "";
		$this->_vars["log"]					= array();
		$this->_vars["log"]["file"]			= $file;
		$this->_vars["log"]["tags"]			= array();
		$this->_vars["vars"]				= array();
		$this->_vars["functionbuffer"]		= array();
		$this->_vars["namespace"]			= "fdml";                   // you can change the namespace here
		$this->pluginfolder 				= "plugins/";               // You can change the plugin directory here
		if ($pluginDir!=null) $this->pluginfolder = $pluginDir;
		
		// functions buffers
		$this->_vars["functionbuffer"]["loop"]				= array();
		$this->_vars["functionbuffer"]["loopbuffer"]		= array();
		$this->_vars["functionbuffer"]["onemptyloop"]		= array();
		$this->_vars["functionbuffer"]["onemptyloopbuffer"]	= array();
		$this->_vars["functionbuffer"]["show"]				= array();
		$this->_vars["functionbuffer"]["showbuffer"]		= array();
		$this->_vars["functionbuffer"]["section"]			= array();
		$this->_vars["functionbuffer"]["sectionbuffer"]		= array();
		$this->_vars["functionbuffer"]["savedloop"]			= array();
		$this->_vars["functionbuffer"]["savedloopbuffer"]	= array();
		
		
		/** read the template file **/
		$this->_vars["buffer"] = $this->freadFile($file);
		
		/** plugin loader **/
		$this->loadPlugins("preparser");
		$this->loadPlugins("dynamics");
		
		$this->_vars["buffer"] = $this->searchtags($this->_vars["buffer"]);
	}
	
	/**
    * Output the parsed template
    * 
    * @return Template buffer
    * 
    */
	function output() {
		$this->applyLoops();
		return $this->_vars["buffer"];
	}
	
	function applyLoops() {
		// apply loops
		foreach ($this->_vars["functionbuffer"]["loop"] as $loopName=>$loopOutput) {
			if ($loopOutput == "") {
				$loopOutput = $this->_vars["functionbuffer"]["onemptyloopbuffer"][$loopName];
			}
			// required...
			$loopOutput = $this->searchtags($loopOutput);
			$this->_vars["buffer"] = str_replace("<loop_$loopName/>",$loopOutput,$this->_vars["buffer"]);
		}
	}
	
	function applySavedTags() {
		$this->applyLoops();
		$regex = '#<savedloop id\=[\"]([\w-]+)[\"] name\=[\"]([\w-]+)[\"] \/>#mis';
		while(preg_match($regex, $this->_vars["buffer"], $match)) {
			$loop_id=$match[1];
			$loop_name=$match[2];
			$this->_vars["buffer"] = preg_replace("#{$match[0]}#", "<".$this->_vars["namespace"].":loop name=\"$loop_name\">".$this->_vars["functionbuffer"]["savedloopbuffer"][$loop_id]."</".$this->_vars["namespace"].":loop>", $this->_vars["buffer"]);
		}
		$this->parseAgain();
	}
	
	function parseAgain() {
		$this->_vars["buffer"] = $this->searchtags($this->_vars["buffer"]);
	}
	
	/**
    * Search and parse all FDML tags
    * 
    * @return pre-parsed template
    * 
    */
	function searchtags($in) {
		// match all tags in the form <type:name args>content</type:name> and singleton <type:name args />
		// old versions:
		//$reg = "#<([a-z]+):(.*?)\s+(.*?)\s*(>(.*?)</\\1:\\2>|/>)#mis";    // global regex to match all tags with all namespaces.
		//$reg = "#<".$this->_vars["namespace"].":(.*?)\s+(.*?)\s*(>(.*?)</".$this->_vars["namespace"].":\\1>|/>)#mis";
		//$reg = "#<".$this->_vars["namespace"].":(.*?)\s+(.*?)\s*(>(.*?)</".$this->_vars["namespace"].":\\1>|/>)#mis";
		$reg = "#<".$this->_vars["namespace"].":([a-zA-Z0-9_-]+)\s*((?:(?!>).)*)\s*(>((?:(?!".$this->_vars["namespace"].":\\1).)*)</".$this->_vars["namespace"].":\\1>|/>)#mis";
		
		if (preg_match($reg, $in)) {
			$in = preg_replace_callback($reg,array($this,"tagparser"),$in,-1);
			return $this->searchtags($in);
			//return $in;
		} else {
			return $in;
		}
	}
	
	/**
    * Parse a single tag
    * 
    * @return the parsed tag
    * 
    */
	function tagparser($array) {
		$query = $array[0];
		//$tagType =  $array[1];
		$tagName =  $array[1];
		$tagArgs =  $array[2];
		$tagContent =  $array[4];
		$argsArray = array();
		
		$tagArgs = str_replace("\\\"","[[QUOTE]]",$tagArgs);
		// attribute parser
		preg_match_all("#([a-z1-9]+)\s*=(([a-z0-9_-]+)|[\"?]([^\"]*)[\"?])#mis",$tagArgs,$args);
		foreach ($args[1] as $id=>$var) {
			$argsArray[$var] = $this->correctValue($args[2][$id]);
		}
		
		array_push($this->_vars["log"]["tags"],array(
			"Tag name"=>$tagName,
			"String args"=>$tagArgs,
			"Parsed args"=>$argsArray,
			"Complete tag"=>$query,
			"Content"=>$tagContent
		));
		
		switch (strtolower($this->superTrim($tagName))) {
			case "loop":
				// a loop...
				if ($argsArray["waitforparsing"] == "true") {
					// put the content in the function buffer
					$this->_vars["functionbuffer"]["savedloopbuffer"][$argsArray["id"]] = $tagContent;  // saved loop buffer using the loop ID. The localization loop name is saved in the tag itself.
					// replace by a localization tag
					return "<savedloop id=\"".$argsArray["id"]."\" name=\"".$argsArray["name"]."\" />";
				} else {
					// put the content in the function buffer
					$this->_vars["functionbuffer"]["loop"][$argsArray["name"]] = "";                 // output of the loop is empty
					$this->_vars["functionbuffer"]["loopbuffer"][$argsArray["name"]] = $tagContent;  // loop buffer
					// replace by a localization tag
					return "<loop_".$argsArray["name"]."/>";
				}
			break;
			case "onemptyloop":
				// emptyloop...
				$tagContent = $this->searchtags($tagContent);
				// put the content in the function buffer
				$this->_vars["functionbuffer"]["onemptyloop"][$argsArray["name"]] = "";                 // output of the onemptyloop is empty
				$this->_vars["functionbuffer"]["onemptyloopbuffer"][$argsArray["name"]] = $tagContent;  // onemptyloop buffer
				// strip the tag
				return "";
			break;
			case "section":
				// a section tag...
				$tagContent = $this->searchtags($tagContent);
				// put the content in the function buffer
				$this->_vars["functionbuffer"]["section"][$argsArray["name"]] = "";                 // output of the section is empty
				$this->_vars["functionbuffer"]["sectionbuffer"][$argsArray["name"]] = $tagContent;  // section buffer
				// replace by a localization tag
				return "<section_".$argsArray["name"]."/>";
			break;
			case "show":
				// a show tag...
				$tagContent = $this->searchtags($tagContent);
				// put the content in the function buffer
				$this->_vars["functionbuffer"]["show"][$argsArray["name"]] = "";                 // output of the section is empty
				$this->_vars["functionbuffer"]["showbuffer"][$argsArray["name"]] = $tagContent;  // section buffer
				// replace by a localization tag
				return "<show_".$argsArray["name"]."/>";
			break;
			default:
				// Unknown type. Probably a plugin. Else, strip the tag.
				if (function_exists("codeparser_$tagName")) {
					$function = "codeparser_$tagName";
					$return = $function($tagContent, $argsArray);
					$return = $this->searchtags($return);
					return $return; 
				} else {
					$tagContent = $this->searchtags($tagContent);
					return $tagContent;
				}
			break;
		}
	}
	
	
	
	
	function parseAndApply($option=null) {
	
		$functions = get_defined_functions();
		$functions = $functions["user"];
				
		switch ($option) {
			case "before":
				foreach ($functions as $function) {
					if (substr($function,0,strlen("preparser_")) == "preparser_") {
						$func = $function;
						$this->_vars["buffer"] = $func($this->_vars["buffer"], $this);
					}
				}
			break;
			case "after":
				foreach ($functions as $function) {
					if (substr($function,0,strlen("postparser_")) == "postparser_") {
						$func = $function;
						$this->_vars["buffer"] = $func($this->_vars["buffer"], $this);
					}
				}
			break;
		}
	}
    
	/**
    * Register a template variable
    * 
    * @param String the variable name without the variable identifier (for variable "%var%", just "var")
    *        String The value of the variable
    * @return nothing
    * 
    */
	function addVar($label, $value) {
		$this->_vars["vars"][$label] = $value;
		$this->_vars["buffer"] = str_replace("%".$label."%",$value,$this->_vars["buffer"]);
	}
	
    /**
    * Register more than one template variable at a time, in an array
    * 
    * @param Array an array of variables type array("var1"=>"value1","var2"=>"value2")
    * @return nothing
    */
	function addVars($array) {
		foreach ($array as $varLabel=>$varValue) {
			$this->addVar($varLabel, $varValue);
		}
	}
	
	/**
    * Loop a code section
    * 
    * @param String the loop name as defined in the template
    * 		 Array an array of variables type array("var1"=>"value1","var2"=>"value2")
    * @return Template buffer
    * 
    */
	function loop($name, $array) {
		global $_PARSER;
		$buffer = $this->_vars["functionbuffer"]["loopbuffer"][$name];
		foreach ($array as $label=>$value) {
			$buffer = str_replace("%".$label."%",$value,$buffer);
		}
		//$this->debug($name, $buffer);
		//$buffer = $this->searchtags($buffer);
		$this->_vars["functionbuffer"]["loop"][$name] .= $buffer;
	}
	
	/**
    * Function used to handle the <fn:section> tag. Any call to this function will replace the current buffer with the content of the <fn:section> tag.
    * 
    * @param String the name of the section to load, defined on the tag as name="[name]"
    * @return true
    */
	function useSection($sectionName) {
		global $_PARSER;
		$tmpBuffer 	= $this->_vars["functionbuffer"]["sectionbuffer"][$sectionName];
		$this->_vars["buffer"] = $tmpBuffer;
		$this->parseAndApply();
		return true;
	}
	
	
    /**
    * Function used to handle the <fn:show> tag. It will result as the display of the content of the tag.
    * If a tag is not called, it will be deleted before any output.
    * 
    * @param String the name of the <fn:show> tag to load, defined on the tag as name="[name]"
    * @return true
    */
	function show($name) {
		global $_PARSER;
		$tmpBuffer 	= $this->_vars["functionbuffer"]["showbuffer"][$name];
		$this->_vars["buffer"] = str_replace("<show_$name/>",$tmpBuffer,$this->_vars["buffer"]);
		$this->parseAndApply();
		return true;
	}
	
	function loadPlugins($type) {
		$pluginfolder=$this->pluginfolder;
		if ($handle = opendir($pluginfolder.$type)) {
			while (false !== ($file = readdir($handle))) {
				if ($file != "." && $file != "..") {
					if (!is_dir($pluginfolder.$type."/".$file) && strtolower($this->STRING_get_file_ext($file))=="php") {
						@include_once($pluginfolder.$type."/".$file);
					}
					
				}
			}
			closedir($handle);
		}
		$functions = get_defined_functions();
		$functions = $functions["user"];
		switch ($type) {
			case "preparser":
				foreach ($functions as $function) {
					if (substr($function,0,strlen("preparser_")) == "preparser_") {
						$func = $function;
						$this->_vars["buffer"] = $func($this->_vars["buffer"], $this);
					}
				}
			break;
			case "postparser":
				foreach ($functions as $function) {
					if (substr($function,0,strlen("postparser_")) == "postparser_") {
						$func = $function;
						$this->_vars["buffer"] = $func($this->_vars["buffer"], $this);
					}
				}
			break;
		}
	}

	function correctValue($value) {
		$firstChar = substr($value,0,1);
		$lastChar = substr($value,strlen($value)-1,1);
		if ($firstChar == "\"" && $lastChar == "\"") {
			// remove quotes
			$value = substr($value,1,strlen($value)-2);
		}
		$value = str_replace("[[QUOTE]]","\"",$value);
		$value = str_replace("\\>",">",$value);
		return $value;
	}
	
	function freadFile($file) {
		if (!$handle = fopen ($file, "r")) {
			exit;
		}
		$contents = fread ($handle, filesize($file)+1);
		fclose($handle);
		return $contents;
	}
	
	function STRING_get_file_ext($filename) {
		if (strrpos($filename, '.') >= 1) {
			return strtolower(substr($filename, strrpos($filename, '.') + 1));
		} else {
			return "";
		}
	}
	
	function debug($label, $value) {
		if (is_array($value)) {
			echo "<b>$label</b> :: <div style=\"border:1px dashed #000000;margin-left:20px;\">".nl2br(str_replace(" ","&nbsp;",str_replace("<","&lt;",print_r($value,true))))."</div><br>";
		} else {
			echo "<b>$label</b> :: <div style=\"border:1px dashed #000000;margin-left:20px;\">".nl2br(str_replace(" ","&nbsp;",str_replace("\t","    ",$value)))."</div><br>";
		}
		
	}
	
	function tagLog() {
		$this->debug("Tag Log",$this->_vars["log"]["tags"]);
	}
	
	function superTrim($in) {
		/** remove extra spaces, line breaks, tabs **/
		$in = str_replace("\t"," ",$in);
		$in = str_replace("\r"," ",$in);
		$in = preg_replace('/\s\s+/', ' ', trim($in));
		return $in;
	}
	
	function encodeAsArg($in) {
		$in = str_replace("\"","\\\"",$in);
		$in = str_replace("}","\\}",$in);
		return $in;
	}
}
?>

 Conclusion

Updates et code: http://code.google.com/p/fdml-php-template/

 Fichier Zip

Les Membres Club peuvent télécharger directement un fichier contenu dans le zip sans télécharger le zip en entier !

Télécharger le zip


 Sources du même auteur

Source avec Zip PARSER FBML; COMMENT UTILISER DU CODE FBML (FACEBOOK) SANS P...
Source avec Zip Source avec une capture FLEXIBLE TEMPLATE LANGUAGE (FTL) – TEMPLATE PARSER EXTENSIBL...
LISTER UN REPERTOIRE (ARBORESCENCE DES DOSSIERS)
Source avec Zip DEVBLOG: BLOG-SYSTEM POUR LES DEVELOPPEURS @VERSION->0.2.2
Source avec Zip PHP\DEVLIB -> COLORATION SYNTAXIQUE 12 LANGAGES, EASYSQL, IP...

 Sources de la même categorie

Source avec Zip POO - LOGGING PACKAGE par Waredan
POO - OBJECT CLASS par Waredan
Source avec Zip POO - FACTORY CLASS par Waredan
POO - SINGLETON CLASS par Waredan
POO - PARAMETERHOLDER CLASS par Waredan

 Sources en rapport avec celle ci

Source avec Zip Source avec une capture PARSER ALLOCINE par cyrhades
Source avec Zip MOTEUR DE TEMPLATE PHPBB3 SIMPLIFIÉ EN PHP5 par gagah1
Source avec Zip GÉNÉRER DES DOCUMENTS OPENOFFICE (OPENDOCUMENT) OU WORD 2007... par oloynet
Source avec Zip Source avec une capture FLEXIBLE TEMPLATE LANGUAGE (FTL) – TEMPLATE PARSER EXTENSIBL... par BlackWizzard
Source avec Zip [PHP4] MOTEUR DE TEMPLATE par jean84

Commentaires et avis

Commentaire de webdeb le 09/02/2009 00:15:30

Pourquoi s'obstiner à écrire encore des classes en syntaxe PHP 4 ???

Commentaire de BlackWizzard le 09/02/2009 01:29:09 administrateur CS

Parsque c'est comme ça que j'ai appris, et que je ne me suis jamais posé la question...
qu'est ce que ça change une syntaxe php5?

Commentaire de Arto_8000 le 09/02/2009 04:53:42

Tu as tous les concept de fonction et variable public, privée et statique qui sont inclus avec PHP5. Il est aussi possible de typer les paramètres de tes méthodes (les seuls types permis sont array et les classes que tu as définis). Il y a aussi les interfaces qui sont inclus à partir de PHP5.

Commentaire de codefalse le 09/02/2009 09:46:00 administrateur CS

@BlackWizzard : Pourquoi mettre deux fois la même source, avec *quasiment* le même code, sous un nom différent ?

Laquelle je supprime ?

Commentaire de BlackWizzard le 09/02/2009 13:39:30 administrateur CS

Codefalse:
Ce n'est pas la même source, du tout.
Le noyaux est le même, mais ils restent très différents.
FBML parser est dédié au parsing FBML, avec une structure un peut différente et des exigences différentes.
Cette source, par contre, est un template engine complet, qui n'est pas fait pour parser du FBML ou un autre markup language.
Elle inclus des fonctions de contrôle du template tel que les boucles, les variables et autre.
Donc aucune n'est à supprimé.
Ceux qui cherchent un template engine ne seront pas satisfait avec la source FBML etant donné qu'elle n'en inclus pas les fonctions, et ceux qui cherchent un moyen de parser du FBML n'utiliseront pas un template engine ayant des fonctions inutiles.
PS: je suis admin CS également. Je connais les règles.

Commentaire de codefalse le 09/02/2009 14:47:38 administrateur CS

Ce qui me perturbe, c'est qu'il y a beaucoup de similitudes entre ces deux éléments, du coup, pourquoi ne pas faire un core qui utilise les mêmes composants, et deux classes filles (filles, ou autre) qui utiliseront le core, et augmenteront ses capacités en fonction de l'usage (parsing et template).

Commentaire de BlackWizzard le 09/02/2009 15:22:59 administrateur CS

Parsque ce sont deux projets différents.
Ce ne sont pas des sources postées pour me faire de la pub, ou pour montrer que je sais faire un hello world.
Ce sont de vrais sources, complètes, testées, avec exemples, de qualité, avec un vrai travail derrière.
Je pense que celui qui cherche une solution pour parser du FBML ne vx pas trouver une source qui fait template engine en même temps, et que celui qui cherche un template engine ne veut pas trouver un source qui fait aussi parser FBML.
Les gens cherchent des sources adaptées à leur besoin, pas des packages qui ont 10 fonctions dont faire le café, parsque le core du projet était le même de toute façon.
Il y a des similitudes, car j'ai posté le core du projet en "source", qui est basiquement le même. Mais dans le détail, ce sont des sources différentes, avec des fonctions différentes, des utilisations différentes, et des publiques différents.

Commentaire de codefalse le 09/02/2009 15:50:48 administrateur CS

T'inquiète, je sais que tu ne voulais pas te faire de la pub ou (même pire), augmenter ton quota de source postée :p

Ce que je voulais dire par le fait d'avoir des classes filles adaptées au projet, c'est que la personne, même si elle veux l'espresso et pas le café, elle n'aura qu'à instancier (inclure avant) que ce dont elle à besoin. C'est comme le framework Zend. Tu as des tonnes de fonctionnalités, mais tu n'est pas obligé d'instancier tout le système en entier.

Après, ce que je proposais, c'était pour te faciliter la vie, car quand tu fera une correction sur un de tes codes, tu sera obligé de corriger l'autre (en supposant que la modification touche au coeur de tes scripts biensur).

Après, libre à toi de juger qu'elle option est la meilleure, c'est ton code, ton travail, ton temps, et je n'ai pas pour motivation de supprimer des sources juste pour le plaisir. J'ai juste cru que tu t'était emmêlé les pinceaux en proposant tes codes, et j'ai ensuite proposé une variante qui pourrait te simplifier la vie.

Comme on le fait tout le temps, je ne fait que conseiller. Après, libre à toi d'accepter ou non mes conseils :)

Commentaire de BlackWizzard le 09/02/2009 16:05:49 administrateur CS

ça marche ;)

Je préfère laisser en tant que deux projets distincts.

Commentaire de aKheNathOn le 25/02/2009 17:40:43 10/10

Salut BW,

Ca fessait un sacré bail qu'on ne t'avais plus vu sur le réseau. Content de te revoir parmi nous :)

Bonne prog et a+,
Akh

Commentaire de BlackWizzard le 25/02/2009 18:01:19 administrateur CS

Ouai, ça fait un moment que je code plus trop à cause des études, et quand je code, c'est rarement open-source...
Les études sont presques terminées, je pense que je vais recommencer à poster de temps en temps ;)

Commentaire de aKheNathOn le 25/02/2009 18:14:16

C'est vraiment dingue, au début tu m'avait même manqué un peu :) - il étais temps que je me socialise un peu plus.

Ca fait vraiment plaisir de te revoir, j'ai toujours en tête les codes que tu fesait y'à 6 ans de ça en VB 6 !!!

Commentaire de BlackWizzard le 25/02/2009 18:21:06 administrateur CS

lol ça fait un moment!
6 ans et meme plus pour  le VB...
Je code presque uniquement en PHP maintenant. Et un peut de flash AS2 pour un projet perso... Mais je suis plus tres actif...

Commentaire de Kimjoa le 06/03/2009 00:27:34

pile poile ce que je recherché , un script simple, léger et évolutif,..... merci !!

perso , j'ai appris comme BlackWizzard la POO en php4, et j 'ai jamais vraiment compris à koi ça servait l'encapsulation des données ... si quelequ'un trouve le temp de me répondre ....

super code a++

Commentaire de aKheNathOn le 06/03/2009 10:16:59

Salut Kim,

L'encapsulation peut être faite en php4 ou php5. Elle sert à vérifier les valeurs de variables affectées à ton instance.

Un exemple d'encapsulation qui sert :
T'as un progressbar dont la valeur minimale est 0 et valeur maximale est 100. Tu encapsules les bornes pour vérifier que l'utilisateur en les configurant ne met pas une valeur maximale inférieure ou égale à la valeur minimale. Tu as une valeur value permettant d'indiquer la valeur de progression. Tu vérifies qu'elle est comprise dans les bornes.

Dans la POO si tu construit des classes étant réutilisées par d'autres, tu part du principe qu'il faut tout sécuriser, d'où l'encapsulation.

Commentaire de Kimjoa le 06/03/2009 12:28:43

salut aKheNathOn, merci d'avoir répondue à ma question :) !!

bon je dois avoué que je suis toujours pas convaincue :( , pk il faut partir du principe de tout sécuriser?? c'est vraiment rare de trouver des fonctions hérité qui serait sensible si appelé hors de l'instance de la classe .... bon c'est vrai, ça peux arriver , et dans ce cas les private et protected sont utile , mais j'y ais jamais été confronté...

en tout cas c'est cool de me répondre ;)
a++

 Ajouter un commentaire


Discussions en rapport avec ce code source dans le forum

Template phplib [ par TuXAveRy ] Bonjour,Voila j'ai un problème de logique assez basic :/J'utilise smarty depuis quelque temps déjà et pour des raisons technique je souhaiterais passe parser xml [ par Nebraska ] bonjour,bon je débute salement en php; et j'ai besoin d'un parser xml. J'ai essayé ça mais bon ça marche pas :(Une bonne âme prèt a me dire ou je me s Parser overture [ par yvain91 ] Bonjour,J'aurai besoin, grace à une fonction php, de récupérer des résultats affichés sur la page d'overture de suggestion de mot clé(http://inventory parser une page web. [ par dezeque ] hello, j'aimerai savoir comment faire pour parser&nbsp;une page de requete google afin de sortir le nombre de resultat.DeZeQuE php dans template [ par BirD ] hello, une tite question par rapport aux templates : peut -on ins&#233;rer du code&nbsp; php dans le fichier de template et faire que ca fonctionne ? Cherche parser RSS 0.9 1.0 et 2.0 [ par olid ] Jour, Je cherche un tit script (ou une classe) ou un debut de source pour parser du RSS... Je veux juste recuperer les titres et URLs, ce qui est dej Au secours : comment parser un fichier xml et inserer a la volé dans une base mysql [ par deblok83 ] Bonjour, &nbsp;&nbsp;&nbsp; c'est mon premier post ici alors je me permet de me presenter je suis le webmaster de deblok83.com . j'ai un petit proble Parser une chaine de caractères [ par Suru_Verbal ] Bonjour &#224; tous,Quelle fonction utiliser s'il en existe une pour parser une chaine de caract&#232;res ?Merci d'avance ! Parser Flux XML afin de trouver une info [ par neoxdragon ] Salut, Ce que je souhaite faire c'est parser un XML de ce type: &lt;item&gt; &lt;id&gt;1&lt;/id&gt; &lt;name&gt;Nom1&lt;/name&gt; &lt;/item&gt; Flux WEBRANKINFO [ par malik7934 ] Hello,Aujourd'hui j'ai fait la douloureuse decouverte que je ne pouvais pas utiliser simple xml sur mon site.... je me suis donc empresse de demander


Nos sponsors


Sondage...

Comparez les prix

CalendriCode

Février 2010
LMMJVSD
1234567
891011121314
15161718192021
22232425262728

Consulter la suite du CalendriCode

 
Développement réalisé par Nicolas SOREL (Nix) avec l'aide de : Cyril DURAND et Emmanuel (EBArtSoft), Merci à Vincent pour ses précieux conseils.
CodeS-SourceS.com© Toute reproduction même partielle est interdite sauf accord écrit du Webmaster
CodeS-SourceS.com© est une marque déposée tous droits réservés

Google Coop CodeS-SourceS Google Coop CodeS-SourceS
Temps d'éxécution de la page : 0,655 sec (3)

Nous contacter | Annoncer sur CodeS-SourceS | Mentions légales