
|
View Full Version : Custom Templating
JustinSmall 10-25-2008, 12:46 PM Alright, so say I have a error handler.
I don't want it to display the error right where it is.
my site is setup like this
// FUNCTIONS HERE
// Requires Index.tpl file for Visual
require_once(template_dir($template_current) ."/index.tpl");
How can I make it to where it displays it in a preset place instead of right at the top of the template?
JustinSmall 10-25-2008, 03:09 PM So that it's not misunderstood, something like this.
$checkuser = "SELECT * FROM table WHERE username = '$new_user'";
$checkuserquery = mysql_query($checkuser);
$numcheckuser = mysql_num_rows($checkuserquery);
if($numcheckuser == 1){
// USER DOES EXIST
}
Where it has // USER DOES EXIST, it would make an error there, or something like that.
Then on the template file (index.tpl) somehow bring up that error?
I know there is a way to do it, just don't know exactly how to do it.
jphilipson 10-25-2008, 03:12 PM output it as a variable and display the variable in another location?
JustinSmall 10-25-2008, 05:07 PM That is what I am trying to do lolâ¦
I don't know how to output it...
like I want to run the function, then say in my header run the error...
Jaseeey 10-27-2008, 02:31 AM Collect the errors in an array (or appending variable), and then output them only if they exist. Many template engines will allow the use of IF statements within the TPL file (the one I'm developing does).
Also, add an @ symbol to the start of any PHP function which you don't want the error to output exactly when it occurs. You could also use the try and catch methods to catch any exceptions and append them to a string variable.
JustinSmall 10-27-2008, 09:05 AM See, I want to put that function above my include...
like this...
function.php
function checkuser(){
$checkuser = "SELECT * FROM table WHERE username = '$new_user'";
$checkuserquery = mysql_query($checkuser);
$numcheckuser = mysql_num_rows($checkuserquery);
if($numcheckuser == 1){
print("Username Taken, Try Another!");
}
}
index.php
require_once('function.php');
checkuser();
require_once('index.tpl')
index.tpl
header stuff...
side bar...
----------content---------
display 'Username Taken, Try Again!'
--------------------------
footer
I just don't know how to go about doing that...
I don't want to put the function
checkuser(); in teh content area... I want it to be on the index.php file
Adam-AEC 10-27-2008, 01:23 PM You could probably use Exceptions here. http://ca.php.net/exceptions
You could also create an Error class, throw an error at it from your function, and then check for errors' in your view. You'd want to store the error instance in a singleton Registry though, instead of having to global() it from your function.
Also, I hope your sanitizing $new_user, and not just fetching it from $_POST (or *gasp* Global Variables).
Steve_Arm 10-27-2008, 02:19 PM If your files are included then you have a "global scope"
<?=$g_error;?> will show on the template file (or however you show vars on tpl files), which has been defined in another file which includes the tpl file.
Without knowing how is the whole design of the system this is an assumption.
JustinSmall 10-27-2008, 10:06 PM lol that was just an example, and yes I will sanitize it, to manyi injectors today! rotten people I tell you, well I will check out this exception throwing...
@steve, you know I am into the shorthand <? but never have been into <?= :)
JustinSmall 10-27-2008, 10:24 PM As you can tell I've never done error reporting before lol.
Ok, so I tried this (worked)
function testError($num){
if($num > 1){
throw new Exception("Number Is Greater Than One.");
}
if($num == 0){
throw new Exception("Number Can't Be Greater Than One");
}
}
try {
echo testError(0) . "\n";
} catch (Exception $e) {
echo 'Caught exception: ', $e->getMessage(), "\n";
}
but using this little code, how would I put it on the TPL, I still don't understand.
Adam-AEC 10-27-2008, 11:34 PM I don't know what your existing code base looks like, so the code I am providing is a shot in the dark.
You should really look into using MVC. You should have a template class that you pass variables to, and it renders the necessary output. You need to separate business logic from your views, as it doesn't belong there.
<?php
class Error {
private $errors;
public function __construct() {
$this->errors = array();
}
public function addError($message) {
array_push($this->errors, $message);
}
public function hasErrors() {
return sizeof($this->errors);
}
public function stringOfErrors($delimeter = "\n") {
return join($delimeter, $this->errors);
}
}
$errors = new Error;
function check_if_user_exists($username) {
# Function cleaned up for testing purposes
# Throw exception if the username foo was passed
if ($username == 'foo') throw new Exception("Username '${username}' exists!");
}
try {
# Perform our business logic here!
check_if_user_exists('foo');
} catch (Exception $e) {
# Rescue and add error message
$errors->addError($e->getMessage());
}
?>
<!-- This could be in your template -->
<?php
# Template code here
if ($errors->hasErrors()) {
echo sprintf("Error: %s", $errors->stringOfErrors());
}
?>
You'd want to separate the Error class into it's own file to keep everything organized, and just require at the top of the files that need it. I'll try to explain to you parts of the code you might not understand.
Hope this helps.
JustinSmall 10-28-2008, 09:19 AM I've been programming for quite awhile, but never needed a nice little error reporting system until now!
This is actually pretty nice :) I will definitely build onto this and make it more suitable, but what you did was very helpful.
The only thing I didn't get was the return sizeof... what is the need for the count?
Adam-AEC 10-28-2008, 10:01 AM The only thing I didn't get was the return sizeof... what is the need for the count?
It's used to return true/false. A zero length will return false (no errors), a non-zero length should return true (X number of errors).
JustinSmall 10-28-2008, 11:58 AM oops reposted (I thought the post didn't go through)
Now I see it though, gotcha :) thats useful information aswell... I love learning
|