Smarty Forum Index Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon.

Useful Smarty wrapper

 
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> Smarty Development
View previous topic :: View next topic  
Author Message
tsigo
Smarty Rookie


Joined: 14 Jun 2003
Posts: 7

PostPosted: Sun Jun 15, 2003 10:29 pm    Post subject: Useful Smarty wrapper Reply with quote

Nevermind.

Last edited by tsigo on Sun Jun 22, 2003 12:37 am; edited 1 time in total
Back to top
View user's profile Send private message AIM Address
boots
Administrator


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

PostPosted: Sun Jun 15, 2003 11:12 pm    Post subject: Reply with quote

hmmm. I don't get it--or at least, I don't see why you take the pains to do something like this. For one thing, it requires extra parsing (in the wrapper) and in the end, you still have to assign arrays.

Then again, there are a lot of ways to build an array, so why not, I suppose--if it floats your boat and all.

FYI, I usually end up making one single assignment before calling Smarty, something like:

$smarty->assign('BLOCK', $myblock);

and sometimes:

$smarty->assign_by_ref('BLOCK', $myblock);

and $myblock has *all* of the data (usually nested) that the templates will need. ie. I build the complete payload as a single variable and only assign that to Smarty.

Good Luck and happy coding!
Back to top
View user's profile Send private message
idxman01
Smarty Rookie


Joined: 10 Jun 2003
Posts: 14
Location: FL

PostPosted: Mon Jun 16, 2003 6:36 pm    Post subject: Reply with quote

Not sure I get it either..

Here's my typical assign statement:

Code:

      $tpl->assign('results', $obj->searchResults);


$obj being the class I'm retrieving data for and var searchResults being an array of result objects.

In the case of not wanting to use the object syntax in the template:

Code:

      foreach ($Staff->assign_vars as $v) {
         $tpl->assign( 's__' . $v, $Staff->$v );
      }


in the tpl:
Code:

First Name:{$s__fname}
etc...
Back to top
View user's profile Send private message
tsigo
Smarty Rookie


Joined: 14 Jun 2003
Posts: 7

PostPosted: Sat Jun 21, 2003 9:51 pm    Post subject: Reply with quote

I'm trying it without the wrapper, how would you do something like this, just in Smarty?

Code:

  {foreach item=DATE_ITEM from=$DATE_ROW}
  <tr>
    <th align="left">{$DATE_ITEM.DATE}</th>
  </tr>
  {foreach item=NEWS_ITEM from=$DATE_ITEM.NEWS_ROW}
  {cycle values="row1,row2" assign="ROW_CLASS"}}
  <tr>
    <td class="{$ROW_CLASS}">
      <b style="font-size:13px">{$NEWS_ITEM.HEADLINE}</b> <span class="small">(submitted by {$NEWS_ITEM.AUTHOR} at {$NEWS_ITEM.TIME})</span><br /><br />
      {$NEWS_ITEM.MESSAGE}
    </td>
  </tr>
  {/foreach}
  {/foreach}


It shows the date, and then the posts for that date.


Last edited by tsigo on Sun Jun 22, 2003 12:33 am; edited 1 time in total
Back to top
View user's profile Send private message AIM Address
boots
Administrator


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

PostPosted: Sat Jun 21, 2003 11:29 pm    Post subject: Reply with quote

tsigo, it seems like you answered your own question. Is there something in particular you are asking about?
Back to top
View user's profile Send private message
tsigo
Smarty Rookie


Joined: 14 Jun 2003
Posts: 7

PostPosted: Sun Jun 22, 2003 12:04 am    Post subject: Reply with quote

I need it to show the date, and then the posts for that date, but it's not.

This is what I have right now, on the PHP side

[php:1:464056cf7f]
$DATE_ROW = array();
$index = 0;
while ( $news = $db->fetch_record($result) )
{
// Show a new date row if it's not the same as the last
if ( date($user->style['date_notime_long'], $news['news_date']) != date($user->style['date_notime_long'], $previous_date) )
{
$DATE_ROW[$index]['DATE'] = date($user->style['date_notime_long'], $news['news_date']);

$previous_date = $news['news_date'];
}

$message = $news['news_message'];

$DATE_ROW[$index]['NEWS_ROW'][] = array(
'HEADLINE' => stripslashes($news['news_headline']),
'AUTHOR' => $news['username'],
'TIME' => date("h:ia T", $news['news_date']),
'MESSAGE' => $message);

$index++;
}
$db->free_result($result);

$tpl->assign(array(
'DATE_ROW' => $DATE_ROW)
);[/php:1:464056cf7f]

And the template is in my previous post. All it's doing is showing the date rows. I must be having a brain fart atm.
Back to top
View user's profile Send private message AIM Address
boots
Administrator


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

PostPosted: Sun Jun 22, 2003 12:14 am    Post subject: Reply with quote

hmmm. I can't see the error either.

Well, you don't need $index (you can use [] like you did elsewhere) but I am curious as to the results of print_r($DATE_ROW) just before it gets assigned to Smarty. (or from the smarty debug console)
Back to top
View user's profile Send private message
tsigo
Smarty Rookie


Joined: 14 Jun 2003
Posts: 7

PostPosted: Sun Jun 22, 2003 12:17 am    Post subject: Reply with quote

Code:

Array
(
    [0] => Array
        (
            [DATE] => June 21, 2003
            [NEWS_ROW] => Array
                (
                    [0] => Array
                        (
                            [HEADLINE] => Testing 2
                            [AUTHOR] => tsigo
                            [TIME] => 05:50pm EDT
                            [MESSAGE] => Testing 2
                        )

                )

        )

    [1] => Array
        (
            [DATE] => May 23, 2003
            [NEWS_ROW] => Array
                (
                    [0] => Array
                        (
                            [HEADLINE] => Testing 1
                            [AUTHOR] => tsigo
                            [TIME] => 10:36pm EDT
                            [MESSAGE] => Testing 1
                        )

                )

        )

)
Back to top
View user's profile Send private message AIM Address
boots
Administrator


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

PostPosted: Sun Jun 22, 2003 12:28 am    Post subject: Reply with quote

This is definately a syntax problem (the double braces):

Code:
   <th align="left">{{$DATE_ITEM.DATE}}</th>
Back to top
View user's profile Send private message
tsigo
Smarty Rookie


Joined: 14 Jun 2003
Posts: 7

PostPosted: Sun Jun 22, 2003 12:31 am    Post subject: Reply with quote

Oh, no - that's how they all are, I just removed them from the example and missed that one.

I did the $tpl->left_delimiter = '{{' and $tpl->right_delimiter = '}}'
Back to top
View user's profile Send private message AIM Address
boots
Administrator


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

PostPosted: Sun Jun 22, 2003 12:32 am    Post subject: Reply with quote

sneaky guy Smile

Now try this:

{{assign var=NR value=$DATE_ITEM.NEWS_ROW}}
{{foreach item=NEWS_ITEM from=$NR}}
Back to top
View user's profile Send private message
tsigo
Smarty Rookie


Joined: 14 Jun 2003
Posts: 7

PostPosted: Sun Jun 22, 2003 12:36 am    Post subject: Reply with quote

Ok that works, thanks.
Back to top
View user's profile Send private message AIM Address
boots
Administrator


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

PostPosted: Sun Jun 22, 2003 12:42 am    Post subject: Reply with quote

d'oh - you removed your original message.

I believe that you are overriding $smarty->assign (since you are passing an array). I assume that you are iterating the array and assigning references?

What you should do is assign the entire $DATE_ROW by reference:

$smarty->assign_by_ref('DATE_ROW', $DATE_ROW);

That should solve it. References are tricky Smile
Back to top
View user's profile Send private message
boots
Administrator


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

PostPosted: Sun Jun 22, 2003 12:49 am    Post subject: Reply with quote

I should mention that it is always a good idea to use a custom wrapper around Smarty, even if it does nothing at all. This way, you can extend Smarty at anytime in the future without having to rewrite your code.
Back to top
View user's profile Send private message
Display posts from previous:   
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> Smarty Development 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