Home > Archive > PHP Smarty Templates > July 2004 > Help understand Smarty - arrays, variables, general
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Help understand Smarty - arrays, variables, general
|
|
| Scott Phelps 2004-07-01, 4:02 pm |
| Please forgive me if I sound clueless here. I have been trying out
Smarty for three days now, and I have the following question:
If I have a PHP array in a separate include file that stores form data
like this:
---------------------------------------------
$formdata = array(
'name'=>$_POST['name'],
'company'=>$_POST['company'],
);
---------------------------------------------
And I want to copy this array to another form using the template syntax
like this:
---------------------------------------------
<p>Contact Name:
<input name='form2_name'
type='text' size='25' value={$name}></input></p>
.....etc
---------------------------------------------
Then must I do this in my main .php file?
---------------------------------------------
$smarty->assign('WORKORDER',array(
"name"=>$name,
.....etc
));
$smarty->display('index.tpl');
---------------------------------------------
This just seems soooo redundant. To assign form-data to one array, then
re-assign the array variables to the Smarty->array so the template can
see it!
I really don't get how Smarty is supposed to increase development time.
I seem to spend more time looking up how to do things the "Smarty" way,
and less time writing my real PHP code.
For example. In pure PHP I would code:
----------------------------
<?php
$var=$someClass->someMethod($ID);
?>
<html>
<?php print($var["valueName"]) ?>
</html>
----------------------------.
##### Now, Using Smarty ####
-------- index.php ---------
<?php
$var=$someClass->someMethod($ID);
$new_var=$var["valueName"];
$smarty->assign('var',$new_var);
?>
-------- index.tpl ---------
<html>
{$var}
</html
----------------------------
That just seems like a lot of extra steps just to hide PHP code from the
template.
Really, I am trying to find the advantages of why I should continue to
learn to use Smarty, and I would like to get some hints from you who use
it and have experience. Everyone says it makes them more productive so
maybe I am doing something _very_ wrong here. Please let me know!
{ I want to increase my productivity too :-) }
Thanks for the patience,
Scott Phelps
| |
| Pete M 2004-07-02, 8:57 am |
| Scott Phelps wrote:
> Please forgive me if I sound clueless here. I have been trying out
> Smarty for three days now, and I have the following question:
>
> If I have a PHP array in a separate include file that stores form data
> like this:
> ---------------------------------------------
> $formdata = array(
> 'name'=>$_POST['name'],
> 'company'=>$_POST['company'],
> );
$smarty-> assign('mydata',$formdata);
> ---------------------------------------------
>
> And I want to copy this array to another form using the template syntax
> like this:
> ---------------------------------------------
> <p>Contact Name:
> <input name='form2_name'
> type='text' size='25' value={$name}></input></p>
> ....etc
<input name="myname" type="text" value="{$mydata.name}">
also the post vars directly
<input name="myname" type="text" value="{$smarty.post.name}">
> ---------------------------------------------
>
> Then must I do this in my main .php file?
> ---------------------------------------------
> $smarty->assign('WORKORDER',array(
> "name"=>$name,
>
> ....etc
> ));
>
>
> $smarty->display('index.tpl');
> ---------------------------------------------
>
>
> This just seems soooo redundant. To assign form-data to one array, then
> re-assign the array variables to the Smarty->array so the template can
> see it!
work great with a databasae ..eg Pear example
$sql ="select id, name, address, email from members where name like'a%'";
$smarty->assign('members',$db->getAll($sql) );
>
> I really don't get how Smarty is supposed to increase development time.
> I seem to spend more time looking up how to do things the "Smarty" way,
> and less time writing my real PHP code.
>
http://smarty.php.net/whyuse.php
YOu need to have 2 heads - one as a pure php coder and one as the
html/designer. That's the way I use it at work.
the template/graphic designer does a mock up of the site/ forms etc
I then build the database and the php logic.
then the designer puts the finishing touches
The main thing is that whatever the html designer does he ddoen'st touch
or mess up the hp code and vice versa..
That's just the main advantage for me as well as all the modifiers,
include files etc which make managing and maintaining a site so much easier.
> For example. In pure PHP I would code:
> ----------------------------
> <?php
> $var=$someClass->someMethod($ID);
> ?>
>
> <html>
> <?php print($var["valueName"]) ?>
>
> </html>
> ----------------------------.
>
> ##### Now, Using Smarty ####
>
> -------- index.php ---------
> <?php
> $var=$someClass->someMethod($ID);
> $new_var=$var["valueName"];
> $smarty->assign('var',$new_var);
> ?>
> -------- index.tpl ---------
> <html>
> {$var}
> </html
> ----------------------------
>
> That just seems like a lot of extra steps just to hide PHP code from the
> template.
that's the point ;-)
>
> Really, I am trying to find the advantages of why I should continue to
> learn to use Smarty, and I would like to get some hints from you who use
> it and have experience. Everyone says it makes them more productive so
> maybe I am doing something _very_ wrong here. Please let me know!
>
Good emaple recently - we wanted to add another link to the nav top n
bot bar - I file change and site wide update DONE ! imagine dreamweaver
templates !!!
> { I want to increase my productivity too :-) }
eg todays data
Today is {$smarty.now|date_format:"d-m-y"}
>
> Thanks for the patience,
>
> Scott Phelps
Its a bit of a learning curve I must admit, took me a while to "convert"
my team but certainly there's no other way we'd do a site nowadays, exen
simple 4 page ones!
Keep at it. It'll be worth it , promise ;-)
Pete
big smarty fan
|
|
|
|
|