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

I surprised myself - Smarty has a lot of possibilities

 
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 -> Tips and Tricks
View previous topic :: View next topic  
Author Message
bimal
Smarty Elite


Joined: 19 Apr 2007
Posts: 423

PostPosted: Fri Feb 27, 2009 6:29 pm    Post subject: I surprised myself - Smarty has a lot of possibilities Reply with quote

After working several time with Smarty, today, I found something that I was wrongly practicing with {html_options}. Here is how I got enlightened. Because I have very limited time to develop anything, I though about highly reusable things for HTML dropdown lists made with <SELECT> and <OPTION> tags.

I now, must say that following the manual's procedure to assign an associative array into smarty variable and using it later in {html_options} is NOT a nice idea from the point of re-usability. Since, many of my items are stored in the database, I can use some reusable SQL for this operation.

Consider the code below:
Code:
{html_options name="data[category_id]" id="data[category_id]" options=$budget_categories selected=''}

Here, calculating and assigning $budget_categories in normal php file is a poorest idea to me. Because, it is the same way, I will use other variables like $budget_categories drawn for {html_options}.

Look at the slim code below:
Code:
<SELECT name="data[category_id]" id="data[category_id]">
{html_options options='budget_categories'|dropdown selected=''}
</SELECT>


There is the difference in options=... . Earlier one is now, less efficient to me.
For the concepts of the later on, please read the below text and modifier plugin.

It now does exactly similar to the earlier version. Just the difference is that my dorpdown plugin works in a new fasion. For different kinds of the drop downs, I will need different database tables, their primary keys and some name to identify them. For whatever these column names I may have, I now alias them with 'k' for key and 'v' for value, using SQL.
In the later part of the plugin, I build the associative array with the records from the database, and send them immediately to the {html_option options=$ASSOC_ARRY_FROM_PLUGIN}.

This gave me a big surprise: I can now save at least 50% of the time to write and handle these independently. This modifier now takes the table name as the identifier (for which to draw the dropdown data), and uses sql to build the associative array.

Here is a sample modfier that you should write as your need:
Code:
<?php

# For various drop downs

function smarty_modifier_dropdown($identifier='')
{
   $data = array();
   $db = new mysql();
   $sql = '';
   
   # Each block MUST return k / v (key/value) columns only.
   switch($identifier)
   {
   case 'company':
      $sql="SELECT company_id k, `name` v FROM company WHERE is_active='Y' ORDER BY v;";
      break;
   case 'realtor':
      $sql="
SELECT
   r.realtor_id k,
   r.name v
FROM realtor r
INNER JOIN company c ON c.company_id = r.company_id
WHERE
   r.is_active='Y'
   AND c.is_active='Y'
   # Now, look for current company in session
ORDER BY k;";
      break;
   case 'budget_categories':
      $sql="SELECT category_id k, `category` v FROM budget_categories WHERE is_active='Y' ORDER BY v;";
      break;
   case 'states':
   # CONCAT(state_code, ' : ', state_name)
      $sql="SELECT state_code k, state_name v FROM country_states WHERE is_active='Y' ORDER BY v;";
      break;
   case 'filing_status':
      $sql="SELECT status_name k, status_name v FROM filing_status WHERE is_active='Y';";
   default:
   }
   $db->query($sql);
   while($db->next_record())
   {
      $data[$db->row_data['k']] = $db->row_data['v'];
   }

   return($data);
}

?>


Just note that each switch/case block generates a valid and needed sql, based on our application logic, and finally, it allows to generate the associative array data, perfectly suitable for {html_options}. All these SQL results always with same column names.

Thats a trick on saving time with reusable things. Now, I do not have to waste time in writing the variables for states, filing status, budget categories, company names, realtors, etc.
Back to top
View user's profile Send private message Visit poster's website
mohrt
Administrator


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

PostPosted: Fri Feb 27, 2009 8:24 pm    Post subject: Reply with quote

See my comment here for reference:

http://www.phpinsider.com/smarty-forum/viewtopic.php?t=14991
Back to top
View user's profile Send private message Visit poster's website
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 -> Tips and Tricks 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