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

Look-up data using the Smarty template

 
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 -> General
View previous topic :: View next topic  
Author Message
AbuAnas
Smarty Rookie


Joined: 05 Apr 2004
Posts: 9

PostPosted: Mon Apr 05, 2004 11:14 am    Post subject: Look-up data using the Smarty template Reply with quote

Hi,


I am using a Smarty template in the following manner:

STEP 1: Make an SQL query and retrieve a result set
STEP 2: Assign each record to an object and insert it itno an array
STEP 3: Pass the objects array to a smarty template..
STEP 4: Use template section to print the array contents (each object values)

Now, each object of those has one property (variable) which its value is a number that represents a look-up forign key that reference another table..

I would like the template to print out a value (i.e. a name) from the another table insted of the id of the record without altering the structure of my objects..

To make things clearer let me give an example:

Let me assume that I have those different tables:

Department
----------------
ID
NAME


Employee
---------------
ID
NAME
EMAIL
DEPT_ID --> ref_to_department_table


and assume that I have two objects one for each table and having the same field name vars.. If I passed an array of the employee objects to the template it will print out the dept id's where I want it to list the employee's with their dept's name..


Any idea how can I do it in the most proper way??
Back to top
View user's profile Send private message
messju
Administrator


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

PostPosted: Mon Apr 05, 2004 11:47 am    Post subject: Reply with quote

I'd join the two tables in in the sql-query and assign one array of objects where each object contains the necessary information about employer and department.
Back to top
View user's profile Send private message Send e-mail Visit poster's website
AbuAnas
Smarty Rookie


Joined: 05 Apr 2004
Posts: 9

PostPosted: Mon Apr 05, 2004 12:00 pm    Post subject: Reply with quote

My design is object-driven, and I am using OOP approches, every access to the database is done by DataAccessObjects which populate my business logic objects.. So to have a join value for each look up data fileds means I have to change ALL my design to suit the presentation and this is a major violation of logic and presentation separation rule..
Back to top
View user's profile Send private message
messju
Administrator


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

PostPosted: Mon Apr 05, 2004 1:21 pm    Post subject: Reply with quote

If you do an OO approach, then $employee->DEPT should be a reference to a $dept (an instance of class Department).

Is that what you want?
you can construct this easily in php and assign an array of objects with subobjects to smarty and access things like {$employee->dept->name}

Of course you can also:
- assign a array of $employees
- assign a (hash-array) of $department, with the keys being the DEPT_IDs and the values being the department-objects.

then you can lookup the department inside the template:
{foreach from=$employees item=$employer}
{assign var=dept_id value=$employer->dept_id}
{assign var=dep value=$department.$dept_id}
{* access $employer and his $dept here *}
{/foreach}

but then you do the lookup of the department in the template, and not in the php. this is a (minor) violation of separating business-logic and display-logic, IMHO.
Back to top
View user's profile Send private message Send e-mail 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 -> 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