...

View Full Version : Template Class error, expecting ";"



Jonathon
01-04-2006, 06:50 AM
Okay. A couple things. First off I used to do paid work, but suddenly realizing I had very little real talent in doing certain things on my own (such as making a template class, or file uloading etc...) without a tutorial or something I've decided to take step back and try to make my own fairly simple template class. Now I'm getting an error, and I see nothing line 19 or the lines generally below or above it. I'm wanting to learn theory, and how to code on my own, not how to copy a template Code, so please if you write something it's greatly appreciated, and I WILL NOT copy it, but instead use it too learn! , so any time spent to help me is greatly appreciated!

My Error:
Parse error: parse error, expecting `','' or `';'' in c:\program files\easyphp1-8\www\Libs\template_inc.php on line 19
template_inc.php:


<?php
/*
************************************************************************
** Programmed By: Jonathon M. **
** Programmed On: Jan 03, 2005 to Jan 04, 2005 **
** Program Purpose: **
** Simply put, it's to allow users to select different templates, **
** allow myself to easily change my own templates and to keep my PHP **
** seperate from the HTML **
** COPYRIGHT 2005 **
************************************************************************
*/

//Initiate class
class template {
var $rarr = array();
var $arr = array();
var $n;
var $this->file;

function template( $file, $folder ) {
if( file_exists('./Templates/' . $folder . '/' . $file . '.tpl') ) {
$this->file = include('./Templates/' . $folder . '/' . $file . '.tpl');
} else {
die("ERROR: TEMPLATE FILE DOESN'T EXIST");
}
}

function setVar( $rvar, $var ) {
if( !isset( $this->n ) ) {
$this->n = 0;
}
$this->rarr[$n] = $var;
$this->arr[$n] = $var;
$this->n++;
}

function replaceVar( ) {
$num = count( $this->rarr );
ksort( $this->rarr );
ksort( $this->arr );
foreach( $i = 0; $i = $num; $i++; ) {
str_replace('{' . $this->rarr[$i] . '}', $this->arr[$i], $this->file );
}
}

function dispTemp( ) {
echo( $this->file; );
}
}
?>


EDIT: If I am doing this the hard way and adding in too much in, do not hesitate to tell me! :)

ralph l mayo
01-04-2006, 06:54 AM
change var $this->file; to just var $file;
The $this pointer is used elsewhere to signify the class scope and avoid confusion with a method scope $file.

Jonathon
01-04-2006, 06:57 AM
Doh. Stuipid error. Thanks! :) :)

Jonathon
01-04-2006, 07:02 AM
Okay I'm back with another question. At first I thought the parse error was me using the count function wrong but I'm not. Am I using the for() loop wrong? I'll look it up on php.net to see.

ERROR:
Parse error: parse error in c:\program files\easyphp1-8\www\Libs\template_inc.php on line 42

Here is the modified code:



<?php
/*
************************************************************************
** Programmed By: Jonathon M. **
** Programmed On: Jan 03, 2005 to Jan 04, 2005 **
** Program Purpose: **
** Simply put, it's to allow users to select different templates, **
** allow myself to easily change my own templates and to keep my PHP **
** seperate from the HTML **
** COPYRIGHT 2005 **
************************************************************************
*/

//Initiate class
class template {
var $rarr = array();
var $arr = array();
var $n;
var $file;

function template( $file, $folder ) {
if( file_exists('./Templates/' . $folder . '/' . $file . '.tpl') ) {
$this->file = include('./Templates/' . $folder . '/' . $file . '.tpl');
} else {
die("ERROR: TEMPLATE FILE DOESN'T EXIST");
}
}

function setVar( $rvar, $var ) {
if( !isset( $this->n ) ) {
$this->n = 0;
}
$this->rarr[$n] = $var;
$this->arr[$n] = $var;
$this->n++;
}

function replaceVar( ) {
$num = count( $this->rarr );
ksort( $this->rarr );
ksort( $this->arr );
for( $i = 0; $i = $num; $i++ ) {
str_replace('{' . $this->rarr[$i] . '}', $this->arr[$i], $this->file );
}
}

function dispTemp( ) {
echo( $this->file; );
}
}
?>

ralph l mayo
01-04-2006, 07:06 AM
You're welcome. Templating classes are a very good practice kinda thing to do in PHP, and it can seem like you're taking an unnecessarily long route to the same end as lumping all your display and logic together, but trust me when I say you will save so many headaches in the end. Ask anytime if you need help with any shorter-term headaches. :)

Prikid
01-04-2006, 07:08 AM
for( $i = 0; $i <= $num; $i++ ) {
str_replace('{' . $this->rarr[$i] . '}', $this->arr[$i], $this->file );
}

Jonathon
01-04-2006, 07:08 AM
bleh ignore last post. just me making stupid mistakes again, however I got an error I've never seen:


Notice: Undefined variable: n in c:\program files\easyphp1-8\www\Libs\template_inc.php on line 33

Notice: Undefined variable: n in c:\program files\easyphp1-8\www\Libs\template_inc.php on line 34

Notice: Undefined offset: 1 in c:\program files\easyphp1-8\www\Libs\template_inc.php on line 43





<?php
/*
************************************************************************
** Programmed By: Jonathon M. **
** Programmed On: Jan 03, 2005 to Jan 04, 2005 **
** Program Purpose: **
** Simply put, it's to allow users to select different templates, **
** allow myself to easily change my own templates and to keep my PHP **
** seperate from the HTML **
** COPYRIGHT 2005 **
************************************************************************
*/

//Initiate class
class template {
var $rarr = array();
var $arr = array();
var $n;
var $file;

function template( $file, $folder ) {
if( file_exists('./Templates/' . $folder . '/' . $file . '.tpl') ) {
$this->file = include('./Templates/' . $folder . '/' . $file . '.tpl');
} else {
die("ERROR: TEMPLATE FILE DOESN'T EXIST");
}
}

function setVar( $rvar, $var ) {
if( !isset( $this->n ) ) {
$this->n = 0;
}
$this->rarr[$n] = $var;
$this->arr[$n] = $var;
$this->n++;
}

function replaceVar( ) {
$num = count( $this->rarr ) or die("ERROR");
ksort( $this->rarr );
ksort( $this->arr );
for( $i = 0; $i = $num; $i++ ) {
str_replace('{' . $this->rarr[$i] . '}', $this->arr[$i], $this->file );
}
}

function dispTemp( ) {
echo( $this->file );
}
}
?>

ralph l mayo
01-04-2006, 07:09 AM
I pasted the code here and got a parse error from the duplicate semicolon in this line (48):

echo( $this->file; );

echo is not a function, btw, and it's not standard to use parentheses, although it won't hurt anything.

edit: late :/

Jonathon
01-04-2006, 07:11 AM
Okay i've fixed the variable problem...but the offset, never seen it before don't know what it is?



<?php
/*
************************************************************************
** Programmed By: Jonathon M. **
** Programmed On: Jan 03, 2005 to Jan 04, 2005 **
** Program Purpose: **
** Simply put, it's to allow users to select different templates, **
** allow myself to easily change my own templates and to keep my PHP **
** seperate from the HTML **
** COPYRIGHT 2005 **
************************************************************************
*/

//Initiate class
class template {
var $rarr = array();
var $arr = array();
var $n;
var $file;

function template( $file, $folder ) {
if( file_exists('./Templates/' . $folder . '/' . $file . '.tpl') ) {
$this->file = include('./Templates/' . $folder . '/' . $file . '.tpl');
} else {
die("ERROR: TEMPLATE FILE DOESN'T EXIST");
}
}

function setVar( $rvar, $var ) {
if( !isset( $this->n ) ) {
$this->n = 0;
}
$this->rarr[$this->n] = $var;
$this->arr[$this->n] = $var;
$this->n++;
}

function replaceVar( ) {
$num = count( $this->rarr ) or die("ERROR");
ksort( $this->rarr );
ksort( $this->arr );
for( $i = 0; $i = $num; $i++ ) {
str_replace('{' . $this->rarr[$i] . '}', $this->arr[$i], $this->file );
}
}

function dispTemp( ) {
echo( $this->file );
}
}
?>

ralph l mayo
01-04-2006, 07:17 AM
the problem is that count($array) returns one more key than is actually in $array, because the first key is 0. Change your for loop to loop to $num - 1.

Jonathon
01-04-2006, 07:33 AM
Well I've got to thank you for your help so far! You've been great, and I feel like I'm learning a lot (even if the script isn't anything advanced for you guru coders).

But... {Hi} isn't being replaced....

In other words the script returns no errors or anything, yet it just says {Hi}. It doesn't replace "{Hi}" and make it "Hey".

Here is all the code:
index.php


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<?php
require_once("./Libs/template_inc.php");
//let's go

$template = new template( "index", "index" );
$template->setVar( "Hi", "Hey");
$template->replaceVar( );
$template->dispTemp( );

?>
</td>
</tr>
</table>
</body>
</html>

(Don't worry about the HTML lol i know it's screwed up right now...)


index.tpl


{Hi}

lol really simple ^^

template_inc.php


<?php
/*
************************************************************************
** Programmed By: Jonathon M. **
** Programmed On: Jan 03, 2005 to Jan 04, 2005 **
** Program Purpose: **
** Simply put, it's to allow users to select different templates, **
** allow myself to easily change my own templates and to keep my PHP **
** seperate from the HTML **
** COPYRIGHT 2005 **
************************************************************************
*/

//Initiate class
class template {
var $rarr = array();
var $arr = array();
var $n;
var $file;

function template( $file, $folder ) {
if( file_exists('./Templates/' . $folder . '/' . $file . '.tpl') ) {
$this->file = include('./Templates/' . $folder . '/' . $file . '.tpl');
} else {
die("ERROR: TEMPLATE FILE DOESN'T EXIST");
}
}

function setVar( $rvar, $var ) {
if( !isset( $this->n ) ) {
$this->n = 0;
}
$this->rarr[$this->n] = $var;
$this->arr[$this->n] = $var;
$this->n++;
}

function replaceVar( ) {
$num = count( $this->rarr ) or die("ERROR");
ksort( $this->rarr );
ksort( $this->arr );
for( $i = 0; $i = $num - 1; $i++ ) {
str_replace('{' . $this->rarr[$i] . '}', $this->arr[$i], $this->file );
}
}

function dispTemp( ) {
echo( $this->file );
}
}
?>

Velox Letum
01-04-2006, 07:46 AM
$this->file = include('./Templates/' . $folder . '/' . $file . '.tpl');
You can't set the value of a variable with an include...all include returns is a status, the rest is sent directly to the output. To do so you'd have to capture the output with an output buffer, but that's not worth it. You can get the contents of the file and set it to a variable like so:


$this->file = file_get_contents('./Templates/' . $folder . '/' . $file . '.tpl');

ralph l mayo
01-04-2006, 08:01 AM
What Velox said, also a few minor notes:




//...
function setVar( $rvar, $var ) {
//...
$this->rarr[$this->n] = $var; // this should be assigned to $rvar I think
//...
function replaceVar( ) {
//...
for( $i = 0; $i = $num - 1; $i++ ) { // the second argument needs to be a comparison, you have assignment. Use $i < $num; or $i <= $num - 1;



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum