View previous topic :: View next topic |
Author |
Message |
nico654 Smarty Rookie
Joined: 22 Apr 2004 Posts: 5
|
Posted: Mon Apr 26, 2004 7:53 am Post subject: How you would proceed? |
|
|
I need a suggestion.
I'm building an app that lets manage a list of contacts. Now I'm developing the page that lets show/edit each contact, and all his information.
This page should be in two different states: show and edit. The $edit variable, passed via HTTP GET method, sets that state.
The question is: what is it the best manner to manage the two states?
I approached the problem in two ways:
Method 1
card.php:
[php:1:648bfdd662]
$smarty = new Smarty;
$contact['firstname']='Bill';
$contact['lastname']='Smith';
$smarty->assign('contact', $contact);
$smarty->assign('edit', $_GET['edit']);
$smarty->display('card.tpl');
[/php:1:648bfdd662]
card.tpl:
Code: |
{if $edit == 1}
<form action="savecard.php" method="post">
First name: <input type="text" name="firstname" value="{$contact.firstname}"><br>
Last name: <input type="text" name="lastname" value="{$contact.lastname}"><br>
<input type="submit">
</form>
{else}
First name: {$contact.firstname}<br>
Last name: {$contact.lastname}<br>
{/if}
|
Method 2
card.php:
[php:1:648bfdd662]
$smarty = new Smarty;
if ($_GET['edit'] == 1) {
$contact['firstname']='<input type="text" name="firstname" value="Bill">';
$contact['lastname']='<input type="text" name="lastname" value="Smith">';
} else {
$contact['firstname']='Bill';
$contact['lastname']='Smith';
}
$smarty->assign('contact', $contact);
$smarty->assign('edit', $_GET['edit']);
$smarty->display('card.tpl');
[/php:1:648bfdd662]
card.tpl:
Code: |
{if $edit == 1}
<form action="savecard.php" method="post">
{/if}
First name: {$contact.firstname}<br>
Last name: {$contact.lastname}<br>
{if $edit == 1}
<input type="submit">
</form>
{/if}
|
What do you think about them? Any other way to solve the problem?
Thank you,
Nico
ps: creating two tpls, one for editing and one for showing, is not the best solution in my case. |
|
Back to top |
|
niekas Smarty Rookie
Joined: 30 Jan 2004 Posts: 10
|
Posted: Thu Apr 29, 2004 4:23 am Post subject: |
|
|
i like method 1 since its cleaner - no html in php code (both methods are basically the same)
how about for display using a form and disabling <input> fields with "disabled" or "readonly" tag untill user clicked edit button |
|
Back to top |
|
boots Administrator
Joined: 16 Apr 2003 Posts: 5611 Location: Toronto, Canada
|
Posted: Thu Apr 29, 2004 4:32 am Post subject: |
|
|
Agreed they both suffer the same problem: they implement an application decision (action). At least in the first one, all of the HTML is available to the designer. I wonder why multiple files are not an option? Even if you delegated the action to a template (as in the example), IMO it would be preferable to move the separate actions into included templates:
card.tpl:
Code: | {if $edit == 1}
{include file="card_edit.tpl"}
{else}
{include file="card_display.tpl"}
{/if} |
card_display.tpl
Code: | First name: {$contact.firstname}<br>
Last name: {$contact.lastname}<br> |
card_edit.tpl
Code: | <form action="savecard.php" method="post">
First name: <input type="text" name="firstname" value="{$contact.firstname}"><br>
Last name: <input type="text" name="lastname" value="{$contact.lastname}"><br>
<input type="submit">
</form> |
|
|
Back to top |
|
mohrt Administrator
Joined: 16 Apr 2003 Posts: 7368 Location: Lincoln Nebraska, USA
|
Posted: Thu Apr 29, 2004 5:05 am Post subject: |
|
|
I always use separate templates for display and edit, this keeps them clean and simple. Also make it a habit to pipe values through escape when putting them in a form.
<input type="text" name="foo" value="{$foo|escape}">
<textarea name="foo">{$foo|escape}</textarea> |
|
Back to top |
|
nico654 Smarty Rookie
Joined: 22 Apr 2004 Posts: 5
|
Posted: Mon May 03, 2004 1:53 pm Post subject: |
|
|
Using separate templates is a good idea in most of cases.
But what about if you have more than one form in the same page? You should prepare as many templates as forms you have... |
|
Back to top |
|
messju Administrator
Joined: 16 Apr 2003 Posts: 3336 Location: Oldenburg, Germany
|
Posted: Mon May 03, 2004 2:08 pm Post subject: |
|
|
what's the problem?
if the forms are all the same, you can use the same template.
if the forms are all different, it's good to seperate them in seperate files. |
|
Back to top |
|
boots Administrator
Joined: 16 Apr 2003 Posts: 5611 Location: Toronto, Canada
|
Posted: Mon May 03, 2004 10:20 pm Post subject: |
|
|
messju wrote: | if the forms are all different, it's good to seperate them in seperate files. |
Indeed! Even from a mile-high head-in-the-clouds perspective, a first step to re-use is identifying something as unique from something else and then giving it a name that it can be referenced by. |
|
Back to top |
|
|