» Utilizator
Salut, vizitatorule!

SkullBox este o comunitate formata din programatori si administratori de sisteme sau retele care iti sta la dispozitie cand ai o problema legata de calculatoare. Daca esti un utilizator existent, autentifica-te.

Daca nu te-ai inregistrat inca pe forum, alatura-te noua astfel marind comunitatea si ajutandu-i pe cei care au nevoie de informatii.

Daca te-ai inregistrat dar inca nu ai primit codul de activare, il poti cere aici.




Autentifica-te cu numele de utilizator si parola pentru a putea posta pe forum sau pentru a accesa ariile disponibile doar utilizatorilor inregistrati.
» Promovam
» Parteneri » Statistici
  • 59752 de mesaje.
  • 7151 de topicuri.
  • 1016 de utilizatori.
  •  
  • Miller0297 e ultimul utilizator inregistrat.
[Detalii]

 
Pagini: [1]
Print
Template engine(v1.9[bugs fixed]) [552 afisari]
AnDrEwBoY
*


Mesaje: 45
OfflineOffline

WWW

Un template engine facut de mine!Nu i`am mai facut update`uri de cateva saptamani dar cred ca ar putea fi folositor!Este foarte bine explicat si nu cred ca veti intampina probleme!Dar va stau la dispozitie!


# FILE :  [COLOR="DarkOrange"]template.tpl[/COLOR]
Code:
<html>
<head>{Head}</head>
<body {Body_Argv}>
{Content}
</body>
</html>
# FILE :  [COLOR="DarkOrange"]engine.php[/COLOR]

Code:
<?php

class Template {


// basic stuff

var $file = ''; //filename template
    var $tmp_vars = array(); //all vars goes here
    var $content = ''; //all compiled template
var $ext = '.tpl'; //template extension

     //construct function
function __construct($file = '') {

   $this->file = $file.$this->ext;   
   
}//end construct
       
//function setting vars
    function set_vars($argvs = array()) {

foreach($argvs as $key => $row)
   $this->tmp_vars[$key] = $row;
   
}//end function

//function for set var
    function set_var($key,$data) {
   
$this->tmp_vars[$key] = $data;

    }//end function

//function compile
    function compile() {
     
     $content = file_get_contents($this->file);
     foreach($this->tmp_vars as $key => $row)
     $content = str_replace("{".$key."}",$row,$content);
$this->content = $content;

}//end function

    //function show_content()
    function show_content() {

      return $this->content;
   
}//end function

}//end class


class engine extends Template {
 
   
   
/****************************************************************************************************** function process_tags()
*                Argvs :
*                       => $tags = array of tags with array of every tags
*                Example: $this->process_tags(array("b"=> array(arguments),"p" => array(arguments));
******************************************************************************************************************************/
function process_tags($tags = array()) {
   
//begin stuff 
      $str = '';
  $str_end = '';

//start processing
  foreach($tags as $key => $tag) //take every row from array and work with it
        {
             //take all arguments from curent tag and save them in an array 
   $_argv = $tag;
//add curent tag to string without end tag (</tag>)
   $str .= $this->new_tag($key,$_argv,false);
//now save end tag
   if($key != "input")   
    $str_end = "</{$key}".$str_end; //save all end tags (first tag in array will become last in our string)       
    } //end foreach
$str .= $str_end; //save tags and end tags in the same string

return $str; //return all data
} //end function



/************************************************************************************************************** function new_tag()
*                Argvs :
*                        => $tag = name of the tag [example: b ==>> <b></b>]
*                        => $argvs = all arguments of tag[example: onclick => alert(1) ==>> <b onclick="alert(1)"></b>
*                                                         if argv is value then will become <b onclick="alert(1)">value</b>]
*                        => $end_tag = state of return tag[example if tag is false and tag name is `b` ==>> <b arguments>value]
*                       
*                 Concret example : $this->new_tag("b",array("value" => "test","onmouseover" => "alert(1)","onclick"=>"alert(2"),true);
*                                returns => <b onmouseover="alert(1)" onclick="alert(2")>test</b>
*                     if last argument is false
*                                returns =>  <b onmouseover="alert(1)" onclick="alert(2")>test                     
*****************************************************************************************************************************************/

function new_tag($tag = '',$argvs = array(),$end_tag = true) {
   
//begin stuff 
  $value = "";
      $str = "<{$tag}";
 
    //working with tag arguments
   foreach($argvs as $key => $_argv) {
if($key == "value" && $tag != "input") {//if curent key is value and is not an input save value for later     
$value = $_argv;
}
else { //else save argument
$str .= " {$key}=\"{$_argv}\"";
}    
   }//end foreach
   
//now save the value of this tag   
   if($tag == "input") {//if tag is input
$str .= " />"; //close tag
   }
   else { //else handling value
  $str .= ">{$value}";
  if($end_tag) $str .= "</{$tag}>";
   } //end if
     return $str;
} //end function




/**************************************************************************************************** function create_table()
* Create table automatic : example
*
*
*    $arr = array("argvs"        => array("argument"  => "value",
        *                                         "argument2" => "value2"
        *                                         ),
*               "contents"     =>  array("tr1"   => array("argvs" => array("align"  => "left",
*                                                         "id"     => "test"
* ),
*                                                         "td1"    => array("argvs"     => array("argument" => "value"),
*                                                                           "contents"  => "data from tag"
*                                                                           ),
*                                                         "td2"     => array("argvs"     => array("argument" => "value"),
*                                        "contents"  => "data from tag"
*                                          ),                
*     )
* )
        *    );
*
*******************************************************************************************************************************/

function create_table($table_arr = array()) {

  //define table main string(this will be main code)
$table_string = "<table";
 
  //get table arguments and save them   
   $tbl_argvs = $table_arr["argvs"];
 
   foreach($tbl_argvs as $key => $arg)
$table_string .= " {$key}=\"{$arg}\""; $table_string .= ">";
 
$tbl_contents = $table_arr["contents"]; //main var for content and working with it

foreach($tbl_contents as $line => $cur_stf) { //handling main lines from table
//get tr arguments and set them
  $line = ereg_replace("[0-9]{0,100}",null,$line);
  $table_string .= "<{$line}"; //open line
 
  if(isset($cur_stf["argvs"])) {
  $tr_argvs = $cur_stf["argvs"];
//save arguments
  foreach($tr_argvs as $key => $arg)
   $table_string .= " {$key}=\"{$arg}\""; $table_string .= ">"; unset($cur_stf["argvs"]);//end line arguments stuff
//working with row
  foreach($cur_stf as $td_name => $td) {
   $td_name = ereg_replace("[0-9]{0,100}",null,$td_name);
   $table_string .= "<{$td_name}";//open tag
   
   if(isset($td["argvs"])) {
   foreach($td["argvs"] as $key => $arg)
   $table_string .= " {$key}=\"{$arg}\"";
   }
   $table_string .= ">{$td['contents']}"; //end row arguments stuff
   $table_string .= "</{$td_name}>"; //close tag
   }
}
else
{
   $table_string .= ">";
}
  $table_string .= "</$line>"; //close line

  return $table_string."</table>"; //return string table+close table
 
}//end function


     
}//end class
?>
# FILE :  [COLOR="DarkOrange"]table.php(POC)[create automatic tables][/COLOR]
Code:
<?php
require("engine.php");


$engine = new engine("template");



    $arr = array("argvs"         => array("align"  => "center",
                                      "border" => "2"
                                      ),
"contents"     =>  array("tr1"   => array("argvs" => array("align"  => "left",
                                                            "id"     => "test"
    ),
                                          "td1"    => array("argvs"     => array("align" => "center"),
                                                            "contents"  => "test1"
                                                            ),
                                          "td2"     => array("argvs"     => array("align" => "left"),
                         "contents"  => "test1"
                          )                
    ),
                          "tr2"  => array()
)
             );


$argvs = array("Head"      => $engine->new_tag("title",array("value" => "table example")),
       "Content"   => $engine->create_table($arr)
                  );
 
$engine->set_var("Body_Argv",'bgcolor="#FFFF00"');//now set simple var
$engine->set_vars($argvs);// now set more than one var
$engine->compile(); //compile template
$engine->show_content(); // show compiled template


?>
# FILE :  [COLOR="DarkOrange"]tags.php(POC)[create tags][/COLOR]
Code:
<?php
require("engine.php");


$engine = new engine("template");

/*         This 2 arrays are for creating tags and arguments of them      */


$argvs = array("p"   => array("align"   => "center", "id"     => "test"),
           "i"   => array("class"   => "test1"),
   "b"   => array("onclick" => "", "value" => "AnDrEwBoY"),
   "input" => array("type"  => "text","value" => "test")
               );
   
//end tags
//now begin engine stuff (first initiate array of all...
$argvs = array("Head"      => $engine->new_tag("title",array("value" => "create tags")),
               "Body_Argv" => 'bgcolor="#FFFF00"',
   "Content"   => $engine->process_tags($argvs)
              );
 
//end initiate => now set vars
$engine->set_vars($argvs);
$engine->compile(); //compile template
echo $engine->show_content(); // show compiled template


?>
Logged

Worldit.info - Blog de tutoriale si articole de programare web,soft si photoshop,securitate,probleme de algoritmica si viata.
04-07-2008, 20:18 Twitt ::
Reclama
VIP

Hosting

Mesaje: 25.90
OnlineOnline

WWW
 

   Pe ABCDomenii: 250MB spatiu + 20GB trafic + 5 subdomenii = 0.95 €
 
 

The problem with troubleshooting is that trouble shoots back.
Azi 
Pages: [1]
Print
SkullBox Forum  |  Development  |  Web Development  |  PHP Snippets (Moderator: payne)  |  Topic: Template engine(v1.9[bugs fixed])
Jump to: