View previous topic :: View next topic |
Author |
Message |
Brian Smarty Rookie
Joined: 29 Apr 2003 Posts: 21
|
Posted: Tue May 06, 2003 6:30 pm Post subject: Parse a template & look for {$vars} ? |
|
|
I could have sworn I saw a function for this at some time in the past, but I can't for the life of me find it now. I'm looking for a way to parse a template (which contains regular vars, a section, and a foreach) and record which {$vars} it's looking to find. Basically, I'm going to allow template designers decide which vars get assigned by simply setting them in the template. But then I have to parse them and get the correct vars from the database.
Any help greatly appreciated.
Brian |
|
Back to top |
|
mohrt Administrator
Joined: 16 Apr 2003 Posts: 7368 Location: Lincoln Nebraska, USA
|
Posted: Tue May 06, 2003 9:03 pm Post subject: |
|
|
Another way to do this is with objects. This way the variable values are looked up on-the-fly as the template is displayed instead of before hand.
so, instead of:
{$var}
register object "foo", then use:
{foo->get val="var"}
or assign object $foo, then use:
{$foo->get("var")}
A bit wordier, but more efficient. To keep things simplified in the template, you could write a prefilter that converts {$var} to {$foo->get("var")}. THen that will only happen at compile time.
Monte |
|
Back to top |
|
boots Administrator
Joined: 16 Apr 2003 Posts: 5611 Location: Toronto, Canada
|
Posted: Wed May 07, 2003 6:39 am Post subject: |
|
|
Isn't this what plugins are for?
The example on the manual page for Template Functions can be readily modified to fetch data from a database.
{get attr=val attr=val ...}
EDIT:
Or you can use my favourite, Modifiers.
{"your custom query"|get:"var"} |
|
Back to top |
|
mohrt Administrator
Joined: 16 Apr 2003 Posts: 7368 Location: Lincoln Nebraska, USA
|
Posted: Wed May 07, 2003 2:21 pm Post subject: |
|
|
boots wrote: | Isn't this what plugins are for? |
That's another way to do it. There is a whole slew of reasons why an object may be more convenient or managable (search the mailing lists), but use what best suits your needs.
Quote: |
Or you can use my favourite, Modifiers.
{"your custom query"|get:"var"} |
I would not advise mixing application logic into the templates.
Monte |
|
Back to top |
|
Brian Smarty Rookie
Joined: 29 Apr 2003 Posts: 21
|
Posted: Wed May 07, 2003 4:43 pm Post subject: |
|
|
Thanks for the help. Seems like the objects will work great. |
|
Back to top |
|
boots Administrator
Joined: 16 Apr 2003 Posts: 5611 Location: Toronto, Canada
|
Posted: Wed May 07, 2003 11:47 pm Post subject: |
|
|
Quote: | I would not advise mixing application logic into the templates. |
yeah yeah yeah. That has got to be the most used line around here It should be in a sticky, for sure!!
Okay, a bit off topic, but how about using a layered approach, with each layer exploiting Smarty's compilation, transformation and caching engines?
YES, the final output layer (what people tend to mean by templates) should no so-called "application" logic.
But how about a higher level that contained only application logic and no output? ie. a level that is used to create the data structures that Smarty will use.
ie.
$smarty = new $Smarty;
$smarty->fetch('Myapp.data.tpl"); // data level
$smarty->display('Myapp.output.tpl"); // output level
You could add other layers. Because the layers are seprated, you have tons of control over caching and compilation issues. It makes your application/page design less monolithic.
There are many nuts like me who like using Smarty's features to shorthand data layers. |
|
Back to top |
|
mohrt Administrator
Joined: 16 Apr 2003 Posts: 7368 Location: Lincoln Nebraska, USA
|
Posted: Thu May 08, 2003 4:25 am Post subject: |
|
|
That is what include_php is for, use it to load the data into your template at runtime.
{include_php file="load_nav.php"}
Monte |
|
Back to top |
|
boots Administrator
Joined: 16 Apr 2003 Posts: 5611 Location: Toronto, Canada
|
Posted: Thu May 08, 2003 5:04 am Post subject: |
|
|
mohrt wrote: | That is what include_php is for, use it to load the data into your template at runtime.
Monte |
I'm surprised by that answer. {include_php} is one of those things usually on the "not-recommended" list and further, it means putting data loading in-template (output template, that is) instead of keeping the mechanics in the PHP file.
Mostly, it doesn't really address the layered template approach.
Still, I love the object support that is now in Smarty.
Ahh, its nice that there are many ways to eat an apple. |
|
Back to top |
|
mohrt Administrator
Joined: 16 Apr 2003 Posts: 7368 Location: Lincoln Nebraska, USA
|
Posted: Thu May 08, 2003 7:09 pm Post subject: |
|
|
What php_include does is makes a template completely self-contained. A good example is a navigation bar.
Let's say your nav bar contains newspaper sections in it, and these sections are dynamically mapped from your news application. This means any time you include navigation.tpl, you must always remember to load the data into the template before hand. That can be a pain to manage... unless you use php_include. With php_include, the data gets loaded right when the template is included. Now you can use this template without worry of what gets loaded first. Very nice. Also with php_include, the actual php code isn't imbedded in the template, and the included file must come from the $trusted_dir so it can't be explioted by including arbitrary files.
Monte |
|
Back to top |
|
boots Administrator
Joined: 16 Apr 2003 Posts: 5611 Location: Toronto, Canada
|
Posted: Thu May 08, 2003 7:26 pm Post subject: |
|
|
Yes, I get it It is something close to what I keep wanting: the ability to register/associate start and end functions with templates.
I'm not saying that I don't like php_include, I'm saying that it means less separation is all. I'm down with that |
|
Back to top |
|
|