templates - Techniques for using native php as a smoothly inline-able templating engine -
goal: best system refactoring complex & ugly code in same file.
i'm in process of refactoring else's bad code. code intermixed, procedural php , want able create templates -in- script. don't want have create external file of different type run template, want able take horrible stuff happening in script , make template in script, , call template right horrible stuff once was. , still able see template outputting in same file.
i rediscovered php's alternative syntax simplify native templating ( http://php.net/manual/en/control-structures.alternative-syntax.php ).
example system
are there other techniques simplifying templating process native php templates? prefer template engine because makes php echoing simpler using php syntax, when refactoring other people's code, templating engine lot of overhead , imposes lot of rules, such having templates in "templates" folder seperate script you're refactoring, makes difficult placement when refactoring.
specifically, i'm trying embark on project create "templating" system relies on native php, templates can inline in source php scripts. reason i'm doing because i've found that, @ least in initial stage, having switch template calling script, , forth, makes harder deal with, , since refactoring complex , ugly code, want able see that's happening possible. i'd like
// inclusion of template library require_once('lib_template.php'); // initialization logic ...lots of ugly non-display code , data-obtaining code here // decide able output , put array pass template $template_output_variables = array('name'=>'no name'); // define template function here, has html , such in native php here. $contact_us = function page_contact_us($template_output_variables=null){ // leave php in template function. ?> <body> hello world </body> <?php } // call & display template via delayed execution, display within pre-set head , footer. display_page($template=$contact_us, $template_output_stuff=$template_output_variables, $options=array('title'=>'contact us'));
looking beneficial techniques inline "template" sections , clean native php templates
so, i'm wondering other useful techniques there out there making cleaner native-php-template code , i'd love know if else out there did inline-in-file templating system/library/script in php, can check out , use inform writing own.
here's github gist of have far: https://gist.github.com/1201969
output buffering handy tool when lacking template engine.
ob_start(); //start buffer echo "some stuff"; include ('sometemplatefile.php'); //etc etc $body=ob_get_contents(); //get junk out of buffer ob_end_clean(); //clear buffer //do search , replace on contents... //etc include ('header.php'); echo $body; include ('footer.php');
maybe heredoc syntax asking for:
http://us.php.net/manual/en/language.types.string.php#language.types.string.syntax.heredoc
also, if have short open tags enabled can come in handy:
some html: <?=$data?> shortcut <? echo $data;?>
i think may deprecated in future version though (i seem remember reading somewhere)
Comments
Post a Comment