Goal: Best system for refactoring complex & ugly code in exactly the same file.
I'm currently in the process of refactoring someone else's bad code. The code is intermixed, procedural php and I want to be able to create templates -in- the script. I don't want to have to create an external file of a different type to run the template, I want to be able to take the horrible stuff happening in a script and make it into a template in the script, and then call that template right where the horrible stuff once was. And still be able to see what the template is outputting in the same file.
I just recently rediscovered php's alternative syntax to simplify native templating ( http://php.net/manual/en/control-structures.alternative-syntax.php ).
Example System
Are there other techniques for really simplifying the templating process with native php templates? I generally prefer another template engine because it makes the php echoing slightly simpler than using php syntax, but especially when refactoring other people's code, sometimes a templating engine is a lot of overhead and imposes a lot o开发者_如何学运维f rules, such as having your templates in some "templates" folder seperate from the script you're refactoring, which makes it difficult for placement when refactoring.
Specifically, I'm trying to embark on a project to create a "templating" system that relies on native php, with templates that can be inline in source php scripts. The reason that I'm doing this is because I've found that, at least in the initial stage, having to switch from template to calling script, back and forth, makes it slightly harder to deal with, and since I am refactoring really complex and ugly code, I want to be able to see everything that's happening as easily as possible. So I'd like to do something like
// Inclusion of the template library
require_once('lib_template.php');
// Initialization Logic
...lots of ugly non-display code and data-obtaining code here
// Decide what will be able to be output and put it into an array to pass to the template
$template_output_variables = array('name'=>'no name');
// Define the template function here, has html and such in native php here.
$contact_us = function page_contact_us($template_output_variables=null){
// Leave php in the template function.
?>
<body> Hello World
</body>
<?php
}
// call & display the template via delayed execution, display it within a pre-set head and footer.
display_page($template=$contact_us, $template_output_stuff=$template_output_variables, $options=array('title'=>'Contact Us'));
Looking for beneficial techniques for inline "template" sections and clean native php templates
So, I'm wondering what other useful techniques there are out there for making cleaner native-php-template code and I'd also really love to know if someone else out there did a inline-in-file templating system/library/script in php, so I can check it out and use that to inform writing my own.
Here's a github gist of what I have so far: https://gist.github.com/1201969
Output buffering is a handy tool when lacking a template engine.
ob_start(); //start the buffer
echo "some stuff";
include ('sometemplatefile.php');
//etc etc
$body=ob_get_contents(); //get the junk out of the buffer
ob_end_clean(); //clear the buffer
//do search and replace on contents...
//etc
include ('header.php');
echo $body;
include ('footer.php');
Maybe something like heredoc syntax is what you were asking for:
http://us.php.net/manual/en/language.types.string.php#language.types.string.syntax.heredoc
Also, if you have short open tags enabled this can come in handy:
Some HTML:
<?=$data?>
This is a shortcut for
<? echo $data;?>
I think this may be deprecated in a future version though (I seem to remember reading somewhere)
I know this doesn't answer your question, but there are so many good tempting engines available I don't see any reason to create your own. There really is very little overhead.
Your question seems too obscure to me.
What answer you want to get? On what question? I can't get what certain problem you have to solve.
So, I can only comment some of your statements.
another template engine makes the html/css simpler
It is not true.
I'd really love to see it if someone else out there did a non-object oriented templating system in php, so I can use that to inform writing my own.
I am such a person.
But, unfortunately, I disagree with this statement:
having to switch from template to calling script, back and forth, makes it slightly harder to deal with
and I am using separate template for the each page and one main template.
So, only I have to do at the end of script is to assign page template filename and to call main template
$page = 'news.tpl.php';
include 'main.tpl.php';
and then main template would include $page respectively
Anyway, I see no big problem in your current approach. Just include usual header and footer before and after your "inline" template. that's all
'While', 'for', 'foreach', and 'if' all have alternative control structures to make them more template friendly. Really though it's not that hard to close a curly brace with
<?php } ?>
精彩评论