Templating

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

Post Reply
danf_1979
Forum Commoner
Posts: 72
Joined: Sun Feb 20, 2005 9:46 pm

Templating

Post by danf_1979 »

Is this templating style good enough for you?

Code: Select all

INITIAL PHP CODE

$CONTENT = "
<table>
</table>
";

PHP CODE

$CONTENT .= "
<table>
</table>
";

PARSE_FINAL_CONTENT
To me, it feels natural for small or medium proyects, but is it really a good design?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Not particularly my style. If there's anything beyond presentation logic in the "PHP CODE" bits, I would say not a good design. However, I wouldn't be a stickler if building it real quick for a proof of concept or something. Anything else, it'd likely get a frown.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Re: Templating

Post by Chris Corbyn »

danf_1979 wrote:Is this templating style good enough for you?

Code: Select all

INITIAL PHP CODE

$CONTENT = "
<table>
</table>
";

PHP CODE

$CONTENT .= "
<table>
</table>
";

PARSE_FINAL_CONTENT
To me, it feels natural for small or medium proyects, but is it really a good design?
In my opinion it's a spaghetti since I'm guessing "PHP CODE" includes logic. If the "PHP CODE" is something else then it's still a bit messy since you've got to escape all backslashes. If you were going to use vagualey that schema I'd at least use heredoc ;)

Code: Select all

$CONTENT .= <<<EOD

sdjkgbasod
dsvgsdhb
fhsd

EOD;
My idea of a template would not include any logic at all and would look like HTML. The logic can be done externally (in a controller?) and the syntax can easily be based around HTML ;)

That said, templating really is one of those areas lots of people disagree on so I just say use what feels right to you ;)
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post by Christopher »

I really prefer to keep the code and the HTML separated into different scripts -- except in PHP templates. I just find it much clearer to see what the code is doing. And I find that mixed PHP/HTML hides bad design. I really think of PHP as two languages, a programming language and a template language. When used as a templating language it is usually just conditional and looping constructs and read-only access to objects and vars.
(#10850)
danf_1979
Forum Commoner
Posts: 72
Joined: Sun Feb 20, 2005 9:46 pm

Post by danf_1979 »

I do separate php from html, but well, not 100% because I cant. I have to construct tables in the php files. I store them in $CONTENT, so the tables are then passed to the template.

This is an example:

Code: Select all

// Header

$eplug_admin = TRUE;
require_once("../../class2.php");
$lan_file = e_PLUGIN."catalog/languages/".e_LANGUAGE.".php";
if (file_exists($lan_file))	require_once($lan_file);
else require_once(e_PLUGIN."catalog/languages/English.php");
if (!getperms("4")) {
	header("location:".e_BASE."index.php");
	 exit;
}
require_once(e_ADMIN."auth.php");

// End of header


require_once(e_PLUGIN."catalog/classes/db_class.php");
require_once(e_PLUGIN."catalog/lib/functions.php");

if ( isset($_POST["do_save"]) ) {

	$mysql = array();

	$mysql["company_name"] = clean_input($_POST['company_name']);
	$mysql["catalog_title"] = clean_input($_POST["catalog_title"]);
	$mysql["front_message"] = clean_input($_POST["front_message"]);

	if (isset($_POST["use_cat_images"]) && is_numeric($_POST["use_cat_images"])) {
		$mysql["use_cat_images"] = $_POST["use_cat_images"];
	}
	else {
		e107_fast_error(ERROR_00, "config.php");
	}
	if (isset($_POST["use_last_box"]) && is_numeric($_POST["use_last_box"])) {
		$mysql["use_last_box"] = $_POST["use_last_box"];
	}
	else {
		e107_fast_error(ERROR_00, "config.php");
	}
	if (isset($_POST["limit_per_page"]) && is_numeric($_POST["limit_per_page"]) && $_POST["limit_per_page"] >= 0) {
		$mysql["limit_per_page"] = $_POST["limit_per_page"];
	}
	else {
		e107_fast_error(ERROR_01, "config.php");
	}
	if(isset($_POST["categories_img_dir"]) && preg_match("/^[a-zA-Z0-9\-\.\/]{0,255}$/",$_POST["categories_img_dir"]) && strlen($_POST["categories_img_dir"]) <= 50) {
		$mysql["categories_img_dir"] = $_POST["categories_img_dir"];
	}
	else {
		e107_fast_error(ERROR_02, "config.php");
	}
	if(isset($_POST["products_img_dir"]) && preg_match("/^[a-zA-Z0-9\-\.\/]{0,255}$/",$_POST["products_img_dir"]) && strlen($_POST["products_img_dir"]) <= 50) {
		$mysql["products_img_dir"] = $_POST["products_img_dir"];
	}
	else {
		e107_fast_error(ERROR_02,"config.php");
	}
	if (isset($_POST["contact_email"]) && is_email($_POST["contact_email"]) && strlen($_POST["contact_email"]) <= 50) {
		$mysql["contact_email"] = $_POST["contact_email"];
	}
	else {
		e107_fast_error(ERROR_03, "config.php");
	}

	$config = array();

	$config = array(
				"company_name"=>$mysql["company_name"],
				"catalog_title"=>$mysql["catalog_title"],
				"contact_email"=>$mysql["contact_email"],
				"use_cat_images"=>$mysql["use_cat_images"],
				"categories_img_dir"=>$mysql["categories_img_dir"],
				"products_img_dir"=>$mysql["products_img_dir"],
				"use_last_box"=>$mysql["use_last_box"],
				"front_message" => $mysql["front_message"],
				"limit_per_page"=>$mysql["limit_per_page"],
				);
	$mydb->update("e107_cart_config", $config, "id=1" );
	$CONTENT .= "
	<form method='post' action='./config.php' id='action_done'>
	<table class='fborder' style='text-align: left; width: 70%; margin-top:20px;margin-left:auto;margin-right:auto;' border='1' cellpadding='0' cellspacing='0'>
	  <tbody>
	    <tr>
	      <td colspan='1' rowspan='1' class='forumheader' style='text-align: center;'>".ADMIN_FRONT_07."</td>
	    </tr>
	    <tr>
	      <td style='text-align: center;' colspan='2' rowspan='1' class='forumheader'><input class='button' type='submit' name='from_error' value='".GENERIC_00."' /></td>
	    </tr>
	  </tbody>
	</table>
	</form>
	";	
}
// End do save result

$ns -> tablerender(ADMIN_FRONT_00, $CONTENT);


// Footer

require_once(e_ADMIN."footer.php");
Though there are some scripts in which there are FAR more $CONTENT varaibles to apprend. Any tips? Maybe my design is completely wrong. I must tell you... I *am* a newbie, so comments are very appreciated...
danf_1979
Forum Commoner
Posts: 72
Joined: Sun Feb 20, 2005 9:46 pm

Post by danf_1979 »

I use this kind of templates (but not for the script that is posted, that uses the template system of e107. I use the template.inc from phplib):

Code: Select all

<head>
<link href="{STYLESHEET}" rel="stylesheet" type="text/css" />
</head>

<div class="mymain">
	<div class="myheader">{TITLE}</div>
	<div class="mymenu">
		<ul>

			{CATEGORIES}

		</ul>
	</div>
	<div class="mycontent">{CONTENT}</div>
	<div class="myfooter">{FOOTER}</div>
</div>
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post by pickle »

You asked for opinions & boy are you getting them ;).

I'd second what ~danf_1979 showed. That's what I use.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
fastfingertips
Forum Contributor
Posts: 242
Joined: Sun Dec 28, 2003 1:40 am
Contact:

Post by fastfingertips »

Now i'm working at a parser that will help me to use following kind of templates

Code: Select all

<html>
	<head>
		<title><<:title:>></title>
	</head>
	<body>
		<table cellpaddding="0" cellspacing="0" border="1">	
		<tr>
			<td colspan="3"><<:title:>></td>
		</tr>
		<<:if <<:userid:>>==<<:user2:>>:>>
		<tr>
			<td colspan="3">Indeed</td>
		</tr>
		<<:endif:>>	
		<<:foreach <<:user:>>:>>			
			<tr>
				<td><<:username:>></td>
				<td><<:password:>></td>
				<td><<:email:>></td>
			</tr>
		<<:endforeach:>>			
		<tr>
			<td colspan="3"><<:footer:>></td>
		</tr>
		</table>				
	</body>
</html>
I'm making a different approache then Smarty for example and i got an overall speed improvement also.
If you are planning to make a template system is better to allow some cycling functions instead of adding fragment of templates.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post by Christopher »

I'd like to see a really small, simple set of template classes, the first that just does "{myvar}" replacement like danf_1979 showed. Then a second class that adds, say blocks. And a third that adds if() conditionals and while() loops (and I guess for() and foreach()). Then you could just include what you need in your View, but could change up or down because they share the same interface. And it should be customizable so that people could use "{" or "<<:" or whatever prefix/suffix they wanted.
(#10850)
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

If your going to do simple logic in your template files, why not simply use PHP? This is just a bunch of extra overhead.

Code: Select all

?><html>
	<head>
		<title><?php echo $template['title']; ?></title>
	</head>
	<body>
		<table cellpaddding="0" cellspacing="0" border="1">	
		<tr>
			<td colspan="3"><<:title:>></td>
		</tr>
		<?php if ($template['user'] == 'user2') { ?>
		<tr>
			<td colspan="3">Indeed</td>

                ...
fastfingertips
Forum Contributor
Posts: 242
Joined: Sun Dec 28, 2003 1:40 am
Contact:

Post by fastfingertips »

The separator should is customisable but what are other special things that an templating system should handle, i talking about the presentation.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Templating is one of the major PHP (actually, many server side scripting language) debates. Take a look at phpBB. They developed a templating system built on the phplib platform. For Olympus they have adapted their teplate class to use a Smarty type syntax. Then look at WordPress. They don't use templates at all (in the way that most think of templates). They use an html page with PHP function calls placed strategically throughout the page, which is parsed at the end of the page.

I suppose, as with the debate on OOP vs. Procedural coding or the debate on coding standards, use the system that feels best to you while at the same time offering you the best performance for your scripts tasks.

<puttingmytwocentsin>I use the phpBB template class for many apps, though I am testing an app right now that uses a system like WordPress. I like them both.</puttingmytwocentsin>
User avatar
jayshields
DevNet Resident
Posts: 1912
Joined: Mon Aug 22, 2005 12:11 pm
Location: Leeds/Manchester, England

Post by jayshields »

Well, I'm developing a CMS at the moment which uses entirely CSS based templating. I made a templating class which I call and use throughout. The script passes which style sheet to use to the templating class.

The HTML is all written inside the templating class, which does all the logic too.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Dude, that sounds sweet. Do you plan on publicizing this class :wink: ?
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Post by Ambush Commander »

Erm... do you mean that you've got a PHP class generating HTML, and then applying different stylesheets (client side)?
Post Reply