Smarty Forum Index Smarty
The discussions here are for Smarty, a template engine for the PHP programming language.

How you would proceed?

 
Post new topic   Reply to topic    Smarty Forum Index -> General
View previous topic :: View next topic  
Author Message
nico654
Smarty Rookie


Joined: 22 Apr 2004
Posts: 5

PostPosted: Mon Apr 26, 2004 7:53 am    Post subject: How you would proceed? Reply with quote

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
View user's profile Send private message
niekas
Smarty Rookie


Joined: 30 Jan 2004
Posts: 10

PostPosted: Thu Apr 29, 2004 4:23 am    Post subject: Reply with quote

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
View user's profile Send private message
boots
Administrator


Joined: 16 Apr 2003
Posts: 5611
Location: Toronto, Canada

PostPosted: Thu Apr 29, 2004 4:32 am    Post subject: Reply with quote

Agreed Smile 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
View user's profile Send private message
mohrt
Administrator


Joined: 16 Apr 2003
Posts: 7368
Location: Lincoln Nebraska, USA

PostPosted: Thu Apr 29, 2004 5:05 am    Post subject: Reply with quote

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
View user's profile Send private message Visit poster's website
nico654
Smarty Rookie


Joined: 22 Apr 2004
Posts: 5

PostPosted: Mon May 03, 2004 1:53 pm    Post subject: Reply with quote

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... Rolling Eyes
Back to top
View user's profile Send private message
messju
Administrator


Joined: 16 Apr 2003
Posts: 3336
Location: Oldenburg, Germany

PostPosted: Mon May 03, 2004 2:08 pm    Post subject: Reply with quote

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
View user's profile Send private message Send e-mail Visit poster's website
boots
Administrator


Joined: 16 Apr 2003
Posts: 5611
Location: Toronto, Canada

PostPosted: Mon May 03, 2004 10:20 pm    Post subject: Reply with quote

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
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Smarty Forum Index -> General All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group
Protected by Anti-Spam ACP