|
Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon. |
|
View previous topic :: View next topic |
Author |
Message |
devnul Smarty Rookie
Joined: 26 Aug 2003 Posts: 32
|
Posted: Mon Sep 08, 2003 10:15 am Post subject: Worried about memory and other things... Is there a solution |
|
|
Hello,
I am a bit concerned abotu one aspect of Smarty and I'm not really sure what to do here...
Basically I am concerned that for large database queries that return hundreds/thousands of results that having to build that array in memory then have Smarty iterate over the array will consume substantially more memory then something like this:
Code: |
$res=db_quer("SELECT SOME BIG RESULTS");
while($rec=db_fetch_assoc($res))
{
print $rec['somefield'];
print $rec['somefield2'];
}
|
Is there any intuitive way around this that does involve writing PHP code in the Smarty template?... I really want as much of my code (particularly database code) to sit outside of the template...
Thanks!
- Greg |
|
Back to top |
|
andre Smarty Pro
Joined: 23 Apr 2003 Posts: 164 Location: Karlsruhe, Germany
|
Posted: Mon Sep 08, 2003 10:45 am Post subject: |
|
|
Perhaps you could write a custom block plugin which is able to iterate over you database result... |
|
Back to top |
|
messju Administrator
Joined: 16 Apr 2003 Posts: 3336 Location: Oldenburg, Germany
|
Posted: Mon Sep 08, 2003 10:50 am Post subject: |
|
|
this is an issue if you really want to show hundreds/thousands of results on one webpage. but this is unlikely, not?
i think in nearly every case you can limit the results of the database to a subset of reasonable size that is needed for the template to display.
for example if you have 1000 elements to be paged over n-per-page with a template you don't need to assign all 1000 elements to the template, but just the number of the "active" page and the n elements for the "active" page. |
|
Back to top |
|
boots Administrator
Joined: 16 Apr 2003 Posts: 5611 Location: Toronto, Canada
|
Posted: Mon Sep 08, 2003 12:49 pm Post subject: |
|
|
Yes, you WILL loop over the data at least twice; once to collect it, once to display it; however, you have additional opportunities now. For example, if the query results are shared by many templates, you can serialize or otherwise cache them.
Also, when assigning to smarty, make sure you use assign_by_ref(), at least for large objects/arrays.
Besides, the fetch and print paradigm is exactly what Smarty is there to defeat/replace since it ties your application's logic to your display logic which is often not what you really want anyhow. |
|
Back to top |
|
boots Administrator
Joined: 16 Apr 2003 Posts: 5611 Location: Toronto, Canada
|
Posted: Mon Sep 08, 2003 5:41 pm Post subject: |
|
|
It occurs to me that you can use an assigned object that is used to retrieve the current record of interest to avoid the double iteration issue. Perhaps in conjunction with the fast recursion technique.
In other words, you application would set the object up, but it would be left to smarty to actually iterate the object.
Last edited by boots on Tue Sep 09, 2003 12:44 am; edited 1 time in total |
|
Back to top |
|
devnul Smarty Rookie
Joined: 26 Aug 2003 Posts: 32
|
Posted: Mon Sep 08, 2003 11:39 pm Post subject: |
|
|
Of all of these i think the "LIMIT" method works best for me on this project (it seems to be the one which keeps the most code out of the template)....
Thanks guys and/or gals!
- Greg |
|
Back to top |
|
|
|
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
|