JustinSmall
11-02-2008, 05:35 PM
[PHP] Separating business logic from design!
Ok, this is what I'm trying to accomplish.
this is not the actual code, just a run through
PHP Code:
if(user is logged in){// DONT SHOW THIS FORM} else {// show form }
but I want to do that on the backend, not the template file.
How would I go about that without actually echoing all of the form information into the backend?
__________________• PHP5 / MySQL / CUSTOM TEMPLATING / CLEAN
UNIXy
11-02-2008, 05:44 PM
Well, for one template files are PHP-free (ex: smarty's way of doing things). As an option, you could use xajax to make a call to another request processor that in turn injects the result of the business logic.
Regards
__________________UNIXyhttps://www.unixy.net/?page=aboutCross-Datacenter Highly Available Systems (failover)FULLY managed dedicated servers because we care.
JustinSmall
11-02-2008, 05:47 PM
I'm using a TPL file which allows me to do this… (go down to code)
I just don't want to put that big if statement in there, I will if I really have to! I just don't prefer to.
PHP Code:
<? if($_SESSION['logged'] == 1){
ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*} else {
ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*?>
ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*<form action="<? echo $_SERVER['PHP_SELF']; ?>" method="post" name="login">
ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*<table width="45%" border="0" align="center" cellpadding="5" cellspacing="0">
ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*<tr>
ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*<td width="37%">Username:</td>
ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*<td width="63%"><input name="username" type="text" id="username" /></td>
ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*</tr>
ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*<tr>
ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*<td>Password:</td>
ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*<td><input name="password" type="password" id="password" /></td>
ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*</tr>
ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*<tr>
ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*<td></td>
ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*<td><input type="submit" name="submit" value="submit" /></td>
ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*</tr>
ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*<tr>
ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*<td></td>
ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*<td></td>
ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*</tr>
ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*<tr>
ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*<td colspan="2">Enjoy the Benefits: <a href="register.php">Register Now! </a></td>
ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*</tr>
ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*</table>
ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*</form>
ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*<? } ?>
__________________• PHP5 / MySQL / CUSTOM TEMPLATING / CLEAN
UNIXy
11-02-2008, 05:54 PM
I'm not good at this so hopefully I'm not wasting your time. But the way you can solve this, and the way I know at least, is to set a flag.
So
PHP Code:
<?
$session_flag=0;
if($_SESSION['logged'] == 0){
ÂÂ*ÂÂ*ÂÂ*ÂÂ*Smarty->assign('session_flag', $session_flag);
} else {
ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*$session_flag=1;
ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*Smarty->assign('session_flag', $session_flag)
}
And then in your template file, check on Smarty->session_flag (which is cleanly available to the template) and output your HTML code. You can also pass the POST variables too.
__________________UNIXyhttps://www.unixy.net/?page=aboutCross-Datacenter Highly Available Systems (failover)FULLY managed dedicated servers because we care.
JustinSmall
11-02-2008, 06:00 PM
Heh, I'm not using Smarty It's a custom template engine
I know how to check and set my sessions…
what I want to do is be able to create a function where it will do the check for me
in teh session your not allowed to leave out the } at the end...
so I couldn't do this
PHP Code:
function foo(){if(user is logged in){// DONT SHOW THIS FORM} else {// show form} // << that ends function, doesn't end the IF
because it will give me an error...
so I need a way for it to show the form if logged in, but I dont want to put the form itself in the function... I think I might stuck doing it the way I did 2 posts up
__________________• PHP5 / MySQL / CUSTOM TEMPLATING / CLEAN
JustinSmall
11-02-2008, 06:02 PM
or wait, I could do a function like this
PHP Code:
function foo(){if(user is logged in){// do nothing} else {include('login_form.tpl');}}
correct? and then in the tpl page for login.tpl do foo();
__________________• PHP5 / MySQL / CUSTOM TEMPLATING / CLEAN
UNIXy
11-02-2008, 06:06 PM
Quote:
Originally Posted by JustinSmall
or wait, I could do a function like this
PHP Code:
function foo(){
if(user is logged in){
// do nothing
} else {
include('login_form.tpl');
}
}
correct? and then in the tpl page for login.tpl do foo();
Hey, that's exactly what I meant to say ! Based on little I know on templating it's definitely "cleaner" than putting HTML code in PHP.
__________________UNIXyhttps://www.unixy.net/?page=aboutCross-Datacenter Highly Available Systems (failover)FULLY managed dedicated servers because we care.
etogre
11-02-2008, 07:09 PM
How about creating two template files,
PHP Code:
<?phpif (!logged_in()){ÂÂ*$usercp = $tpl->display('templates/loginbox.tpl', false); // false to not echo the content but return it}else{ÂÂ*ÂÂ*$tpl->assign('username', $session->username);ÂÂ*ÂÂ*$usercp = $tpl->display('templates/userbox.tpl', false);}$tpl->assign('usercp', $usercp);$tpl->display('templates/main.tpl');?>
Code:
<html>
<body>
<div id="usercp">
{{usercp}}
</div>
<div id="content">blah blah</div>
</body></html>
__________________