View previous topic :: View next topic |
Author |
Message |
sammydafish Smarty Rookie
Joined: 10 Nov 2003 Posts: 13 Location: Upstate, NY
|
Posted: Wed Nov 12, 2003 5:16 pm Post subject: Please help with a good solution |
|
|
ok, I'm guessing that people have run into this before, and come up with some good soultions, so help me out. I'm looking for the most efficient solution to this problem.
Simple content management using Smarty with caching. Content is stored in a database. There are multiple templates, each of which may have multiple pages associated with it. So in order to get a page, I need to pieces of information. The template, and the page. If they all get pulled from a sigular PHP file, then I can pass the info that's needed like so:
index.php?template=twocolumns&page=aboutus
Now using that I can do all the things I ned to do, and then do something like: $smarty->display($template.".tpl", $page);
Now that might work all fine and dandy, but what happens when someone decede to screw with the GET info. Lets say they change the tamplate in the URL for the same page. Then using the above method, the page content would come up in the wrong template. I can avoid this by simply doing :index.php?page=aboutus, then checkign the database to see what template the 'aboutus' page is assigned to, and display the page based on that. The thing there though is that I need to check the database. One of the reasons I'm trying to use Smarty is becasue of the caching feature making it so that I don't need to hit the database.
Most of the content on the site is static. So the pages are only recompiled and re-cached after content changes. So, is there any way I can do this without hitting the database? Please offer any suggestions you can, I'm very open to new ideas
Thanx _________________ - Anthony |
|
Back to top |
|
messju Administrator
Joined: 16 Apr 2003 Posts: 3336 Location: Oldenburg, Germany
|
Posted: Wed Nov 12, 2003 5:42 pm Post subject: |
|
|
maybe the following works for you:
( http://smarty.php.net/manual/en/api.is.cached.php is your friend )
- only do fast simple sanity checking (with preg_match and strlen maybe)
- then do $cached = is_cached($potential_tpl, $potential_id));
- if $cached is true the id's above are good and you are done without the need to bother the db. you can call display safely.
- if $cached is false do more expensive checking of the supplied data (ask the db). you need the db anyways for your content. if the supplied data is resonable do display(). else raise an error.
with this approach you need the more thorough (and computationally more expensive) checks only on cache misses.
HTH
messju |
|
Back to top |
|
sammydafish Smarty Rookie
Joined: 10 Nov 2003 Posts: 13 Location: Upstate, NY
|
Posted: Wed Nov 12, 2003 6:24 pm Post subject: |
|
|
Checking to see if it's cached is not my problem. The problem is that the page depends on the template. I can check if a template is cached, but how do I check if a page is cached, if I don't know what template it used. Is there some way of dynamicaly generateing caches based on something other than the name of the template? The issues being that multiple different pages might use the same template. But each page, only uses one template. _________________ - Anthony |
|
Back to top |
|
boots Administrator
Joined: 16 Apr 2003 Posts: 5611 Location: Toronto, Canada
|
Posted: Wed Nov 12, 2003 7:48 pm Post subject: |
|
|
Sammydafish--what exactly do you mean that you don't know which template was used to generate a page? Sounds completely--insane! Particularly since your app produced the page in the first place.
Recall: a caching call to display (or fetch) caches the entire call under the template name (and the given cache_id and compile_id).
Perhaps you can explain your situation in more detail? It sounds like you need to look into multiple caches per page. |
|
Back to top |
|
sammydafish Smarty Rookie
Joined: 10 Nov 2003 Posts: 13 Location: Upstate, NY
|
Posted: Wed Nov 12, 2003 8:37 pm Post subject: |
|
|
Hmm... maybe I need to better outline my problem.
There are 5 different templates to my site (more may be added later). As per the concept, the templates hold only design information (look of the page). Based on these 5 templates are dozens of pages of content. When a user creates a page, they choose the template that will best display the content they've created. They choose a name for the page, and enter the content. All this information is stored in the database. When a visitor directs their browser to a page, they are looking for the content of the page, therefore the request is based on the content i.e.
index.php?page=products
The variable page is telling my script to get the page that has information about products. The problem is, that in order to display the cached page that has the products content on it, I need to know what template the products content should be displayed on. How I figure that out is my problem. The database holds this information. So I can easily look up in one of my tables the products page (each page identifier is unique) and figure out what template it was based on. Then, after checking to ensure that the page was cached, I could do something like
$smarty->display($template.".tpl", $page);
This though forces me to hit my database, which negates a large portion of the performance gain I get from caching. So what I need to do is be able to call the cached files up by content (page id in my example). How can I do this?
I guess I can do like I stated in my previous example by sending:
index.php?template=twocolumns&page=aboutus
then checking $smarty->display($template.”.tpl",$page); If the template set in the Get parameters was incorrect, it would return false. Since in that case I’m forced to check the database anyway, I could easily see if the template was wrong, and either check again or rebuild the page. This though forces me to use two parameters in the URL, something I’d rather not do, but can if I need to.
Get what I’m sayin _________________ - Anthony |
|
Back to top |
|
boots Administrator
Joined: 16 Apr 2003 Posts: 5611 Location: Toronto, Canada
|
Posted: Wed Nov 12, 2003 9:07 pm Post subject: |
|
|
I think understand your pain, now. I sometimes have a similar situation.
Here's an idea. Create a single template whose only job is to include the proper layout template (I call mine html_page.tpl). Then you only ever have a single template that is called by display, thereby saving you the roundtrip to the database. Its an extra layer of abstraction, but the compiling flattens that structure for you so speed shouldn't be a concern using this idea. Nor does it impact your designers--all it really requires is that you extend your app slightly to deal with the extra template level.
Did I read you right?
HTH |
|
Back to top |
|
messju Administrator
Joined: 16 Apr 2003 Posts: 3336 Location: Oldenburg, Germany
|
Posted: Wed Nov 12, 2003 9:29 pm Post subject: |
|
|
i just read over your post and read "?template=twocolumns" so i thought you already have a fast way to determine the template. maybe i should have looked closer.
i once did the following that worked for me: i set up a meta.tpl. all calls to is_cached() and display() always get passed "meta.tpl" as template. meta.tpl only consists of one line: Code: | {include file=$template} |
so i only needed a valid cache-id and did not have to detemine the displayed template's name to aks if a page is cached. |
|
Back to top |
|
boots Administrator
Joined: 16 Apr 2003 Posts: 5611 Location: Toronto, Canada
|
Posted: Wed Nov 12, 2003 9:34 pm Post subject: |
|
|
yes, messju's example is exactly what I am suggesting |
|
Back to top |
|
messju Administrator
Joined: 16 Apr 2003 Posts: 3336 Location: Oldenburg, Germany
|
Posted: Wed Nov 12, 2003 10:07 pm Post subject: |
|
|
ups, boots. i didn't notice your post, sorry. |
|
Back to top |
|
sammydafish Smarty Rookie
Joined: 10 Nov 2003 Posts: 13 Location: Upstate, NY
|
Posted: Wed Nov 12, 2003 11:38 pm Post subject: |
|
|
awesome! That's exactly the kind of idea that I needed. Thanks a lot guys _________________ - Anthony |
|
Back to top |
|
|