8 Replies - 1252 Views - Last Post: 07 June 2011 - 07:01 AM Rate Topic: -----

#1 eZACKe  Icon User is offline

  • Garbage Collector

Reputation: 120
  • View blog
  • Posts: 1,278
  • Joined: 01-June 09

Redoing site, with complete code separation

Posted 06 June 2011 - 01:55 PM

And I'm stuck. I just don't understand completely how this all works yet.

Here's my original page without full code separation:
<?php 
session_start();
include_once("appleFunctions.php");
		
?> 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
	"http://www.w3.org/TR/xhtmli/DTD/xhtmli-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
	<head>
		<title>Apple Learning</title>
		<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
		<link href="headerCss.css" rel="stylesheet" type="text/css" />
		<link href="forgotPageCss.css" rel="stylesheet" type="text/css" />
		<link rel="icon" type="image/png" href="favicon.png" />
	</head>
	<body>
	<div id="header">
			<div id="sitebranding">
				<h1>AppleLearning</h1>
			</div>
	</body>
</html>



And here's what my new page with code separation is looking like so far:
header.tpl.html:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
	"http://www.w3.org/TR/xhtmli/DTD/xhtmli-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
	<head>
		<title>TITLE</title>
		<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
		<link href="headerCss.css" rel="stylesheet" type="text/css" />
		<link href="forgotPageCss.css" rel="stylesheet" type="text/css" />
		<link rel="icon" type="image/png" href="favicon.png" />
	</head>
	<body>
	<div id="header">
			<div id="sitebranding">
				<h1>AppleLearning</h1>
			</div>
	</body>
</html>



My problem is I don't understand how to get the top php stuff into my page"
<?php 
session_start();
include_once("appleFunctions.php");
		
?>



I have a createPage function:
function createPage($templateName='index.tpl', $title='My Site', $params=array())
{
	// Load the template providided via the function parameter.
    $pageText = file_get_contents($templateName . '.html');
    
    // Replace each of the optional $params key=>value pairs in
    // the page text.
    foreach ($params as $_key => $_value) {
        $pageText = str_replace($_key, $_value, $pageText);
    }
    
    // And finally replace the title, and return the now
    // filled out template page.
    return str_replace('TITLE', $title, $pageText);
}



And an index page:
<?php
/** index.php */
include_once("appleFunctions.php");



$headerData = array();
$headerPage = createPage('header.tpl.html', 'Header - My Site', $headerData);

?>



How am I to get this stuff into the header.tpl.html page?:
<?php 
session_start();
include_once("appleFunctions.php");
		
?>



Also, once I do that how do I actually display the page? Do I have to just echo something?

Sorry, this is all a little confusing to me right now but I think this is for the best for sure.


EDIT: Now that I look it over I don't really need to include the functions page because it's already included in index.php, correct?

Though I do need to do the start_session() call.
This is what I tried:
$headerData = array('START_SESSION' => 'session_start();');
$header = createPage('header.tpl', 'AppleLearning', $headerData);

echo $header;



With this in my template:
START_SESSION
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"



That doesn't work though. When I load the page, there's just text, session_start(); on the screen.



EDIT: Still nothing working, but this is what I've done:
<?php
/** index.php */
include_once("appleFunctions.php");

// No values provided, just using defaults.
//$index = createPage();

// An "About page" using the default title.
//$about = createPage('about.tpl');

// A user profile page, using a username and and
// a user ID.
$headerData = array('START_SESSION' => '<?php session_start(); ?>');
$header = createPage('header.tpl', 'AppleLearning', $headerData);

//echo $header;

// The login/signup page
$loginData = array('INCLUDE_HEADER' => '<?php echo $header; ?>', 'INCLUDE_SIGNIN' => '<?php include("appleSignIn.php"); ?>',
					'INCLUDE_LOGIN' => '<?php include("appleLogin.php"); ?>');
$login = createPage('loginLayout.tpl', 'AppleLearning', $loginData);

echo $login;

?>



header.tpl.html:
START_SESSION
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
	"http://www.w3.org/TR/xhtmli/DTD/xhtmli-strict.dtd">
<!-- Started 5/22/2011 2:05PM EST -->
<html xmlns="http://www.w3.org/1999/xhtml">
	<head>
		<title>TITLE</title>
		<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
		<link href="headerCss.css" rel="stylesheet" type="text/css" />
		<link href="forgotPageCss.css" rel="stylesheet" type="text/css" />
		<link rel="icon" type="image/png" href="favicon.png" />
	</head>
	<body>
	<div id="header">
			<div id="sitebranding">
				<h1>AppleLearning</h1>
			</div>
	</body>
</html>



Some of loginLayout.tpl.html:
INCLUDE_HEADER
INCLUDE_SIGNIN
INCLUDE_LOGIN
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
	"http://www.w3.org/TR/xhtmli/DTD/xhtmli-strict.dtd">
<!-- Started 5/22/2011 2:05PM EST -->
<!-- Login/SignUp Page -->
<html xmlns="http://www.w3.org/1999/xhtml">
	<head>
		<link href="indexCss.css" rel="stylesheet" type="text/css" /> 
	</head>
	<body>
	



In the index page I'm not sure if this is working, though probably not:
$headerData = array('START_SESSION' => '<?php session_start(); ?>');



And I know this is not working:
$loginData = array('INCLUDE_HEADER' => '<?php echo $header; ?>', 'INCLUDE_SIGNIN' => '<?php include("appleSignIn.php"); ?>',
					'INCLUDE_LOGIN' => '<?php include("appleLogin.php"); ?>');
$login = createPage('loginLayout.tpl', 'AppleLearning', $loginData);

echo $login;



That's definitely not working because there's no way my createPage function knows what $header is anyway, plus I checked and the signin and login scripts are definitely not being included in the page.

I guess my big question is how do you do things other than layout in this code separation method? Like how do you do includes, session starts, how would I even call a function if I needed to? I'm looking through content on DIC but everything is just basic layout stuff and printing words on the screen, not doing PHP functions and what not.

This post has been edited by eZACKe: 06 June 2011 - 04:04 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Redoing site, with complete code separation

#2 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4405
  • View blog
  • Posts: 12,262
  • Joined: 18-April 07

Re: Redoing site, with complete code separation

Posted 06 June 2011 - 04:24 PM

You wouldn't include things like include statements. You are already in PHP mode when you are filling your template right?

<?php

   // Start your session
   session_start();

   echo $parser->parse("template.tpl.html");
?>



Your parser is going to return pure HTML/Javascript etc. Client side code. If you need to include a file, do that before you parse the template. The way you are trying to do it you would have to parse the HTML, stick in the PHP sections and then repass it back to the PHP process to translate the full page. No need, you are in PHP mode when you are parsing so you can do your includes and such and just pass whatever data you need to to the template and have it fill in the tags. Most templates use a tag like {{SOME_TAG}}.

Hope you get what I am saying. :)
Was This Post Helpful? 3
  • +
  • -

#3 eZACKe  Icon User is offline

  • Garbage Collector

Reputation: 120
  • View blog
  • Posts: 1,278
  • Joined: 01-June 09

Re: Redoing site, with complete code separation

Posted 06 June 2011 - 04:36 PM

I believe I understand what you are saying.

So in my index.php page where I call all the createPage methods, that's where I would do the includes, and that's where I would start my session.

A couple question in regards to this:

index.php:
session_start();
$header = createPage('header.tpl', 'AppleLearning');

echo $header;



$header will have access to all the $_SESSION variables. But what if $header links to another page? That won't have the $_SESSION variables.

I guess that brings me down to linking in general. What if $header had a link to main.php. When a user clicks on that would you have to send him back to index.php and do a createPage call?
Was This Post Helpful? 0
  • +
  • -

#4 eZACKe  Icon User is offline

  • Garbage Collector

Reputation: 120
  • View blog
  • Posts: 1,278
  • Joined: 01-June 09

Re: Redoing site, with complete code separation

Posted 06 June 2011 - 05:40 PM

Got all my problems sorted out for now. Thanks for the help.

This is just a very different way of doing PHP programing than what I'm used to. Have to get used to how the pages flow doing it this way. :bigsmile:
Was This Post Helpful? 0
  • +
  • -

#5 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3079
  • View blog
  • Posts: 10,804
  • Joined: 08-August 08

Re: Redoing site, with complete code separation

Posted 06 June 2011 - 06:04 PM

Just remember the order in which things happen:
  • Browser sends a request to the server.
  • Server runs PHP script and sends HTML back to browser.
  • Browser displays the HTML. (note that there is no PHP that the browser sees)
  • User takes an action of some kind, repeat step 1.

Was This Post Helpful? 2
  • +
  • -

#6 eZACKe  Icon User is offline

  • Garbage Collector

Reputation: 120
  • View blog
  • Posts: 1,278
  • Joined: 01-June 09

Re: Redoing site, with complete code separation

Posted 06 June 2011 - 08:34 PM

Okay I have another question about how I should go about doing something:

In my non-code separated site I had this:
<div id="workhistorysection">
			<?php
				$count = 1;
				for($j = 0; $j < $numberWork; ++$j)
				{
					if(${workEndDatebox.$j} == "2200-01-01")
					{
						$tempEndDate = "";
					}
					else
					{
						$tempEndDate = ${workEndDatebox.$j};
					}
					
					if(${workBeginDatebox.$j} == "2200-01-01")
					{
						$tempBeginDate = "";
					}
					else
					{
						$tempBeginDate = ${workBeginDatebox.$j};
					}
					echo "$count: <input type='text' maxlength='50' name='workCompanybox0$count' value=\"${workCompanybox.$j}\" /> 
					<input type='text' maxlength='500' name='workDescriptionbox0$count' value=\"${workDescriptionbox.$j}\" /> 
					<input type='text' name='workBeginDatebox0$count' value=\"$tempBeginDate\" /> 
					<input type='text' name='workEndDatebox0$count' value=\"$tempEndDate\" /> 
					<input type='text' maxlength='100' name='workPositionbox0$count' value=\"${workPositionbox.$j}\" /><br/><br/>";
					++$count;
				}



What's happening here is I have a variable number of variables that fill in a variable number of text boxes.

This was a pretty simple matter with code combined like this, but I'm not sure how to fit it into my template approach.

The template and createPage function don't seem like they'd be able to handle a variable number of elements.

This is why I think that:
array('INCLUDE_HEADER' => $header, '{{EMAIL_TOKEN}}' => $email_token, '{{FNAME_TOKEN}}' => $fName_token);



You have to know how many variables you are giving to the array because that's how many {{TAGS}} you will need in your HTML page.

It just doesn't seem possible to me to do a variable number of for example text boxes. You would need a {{TAG}} for each one. And if you can't use php in the HTML, then you have no way of determining how many tags. How can it be done?

This is the key:
for($j = 0; $j < $numberWork; ++$j)
{
}


Without that for loop I just don't see how to do it =/


I'm having a tough time of thinking how I can do this. Any suggestions? Thanks!

This post has been edited by eZACKe: 06 June 2011 - 10:05 PM

Was This Post Helpful? 0
  • +
  • -

#7 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3572
  • View blog
  • Posts: 10,414
  • Joined: 08-June 10

Re: Redoing site, with complete code separation

Posted 06 June 2011 - 11:05 PM

who says you need exactly one {{TAG}} for every <tag>? since you do string replacement in the end, you can replace one {{TAG}} with whatever you like (e.g. <tag><tag>).
Was This Post Helpful? 0
  • +
  • -

#8 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3079
  • View blog
  • Posts: 10,804
  • Joined: 08-August 08

Re: Redoing site, with complete code separation

Posted 07 June 2011 - 06:01 AM

Here's an example:
PHP:
<?php
$states = explode("|",file_get_contents("statelist.txt"));
$myform = file_get_contents("select.html");
$myoptions =file_get_contents("optionform.html");
$cnt = count($states);
$with = "";
$replace = "PHPNAME";
for($i = 0; $i < $cnt; $i++) {
	$with .= str_replace($replace, $states[$i], $myoptions);
}
$replace = "PHPOPTIONS";
$output = str_replace($replace, $with, $myform);
echo $output;
?>

statelist.txt:
AK|AL|AR|AZ|CA|CO|CT|DC|DE|FL|GA|HI|IA|ID|IL|IN|KS|KY|LA|MA|MD|ME|MI|MN|MO|MS|MT|NC|ND|NE|NH|NJ|NM|NV|NY|OH|OK|OR|PA|RI|SC|SD|TN|TX|UT|VA|VT|WA|WI|WV|WY

select.html:
<form method="post"><select>
PHPOPTIONS
</select>
<input type="submit" value="Submit">
</form>

optionform.html:
<option value="PHPNAME">PHPNAME</option>

Was This Post Helpful? 1
  • +
  • -

#9 eZACKe  Icon User is offline

  • Garbage Collector

Reputation: 120
  • View blog
  • Posts: 1,278
  • Joined: 01-June 09

Re: Redoing site, with complete code separation

Posted 07 June 2011 - 07:01 AM

I used a slight modification of your approach, CTphpnwb. Seems to be working well for me so far. Thanks!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1