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

Parse a template & look for {$vars} ?

 
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
Brian
Smarty Rookie


Joined: 29 Apr 2003
Posts: 21

PostPosted: Tue May 06, 2003 6:30 pm    Post subject: Parse a template & look for {$vars} ? Reply with quote

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
View user's profile Send private message Send e-mail
mohrt
Administrator


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

PostPosted: Tue May 06, 2003 9:03 pm    Post subject: Reply with quote

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
View user's profile Send private message Visit poster's website
boots
Administrator


Joined: 16 Apr 2003
Posts: 5611
Location: Toronto, Canada

PostPosted: Wed May 07, 2003 6:39 am    Post subject: Reply with quote

Isn't this what plugins are for? Smile

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. Wink

{"your custom query"|get:"var"}
Back to top
View user's profile Send private message
mohrt
Administrator


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

PostPosted: Wed May 07, 2003 2:21 pm    Post subject: Reply with quote

boots wrote:
Isn't this what plugins are for? Smile


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. Wink

{"your custom query"|get:"var"}


I would not advise mixing application logic into the templates.

Monte
Back to top
View user's profile Send private message Visit poster's website
Brian
Smarty Rookie


Joined: 29 Apr 2003
Posts: 21

PostPosted: Wed May 07, 2003 4:43 pm    Post subject: Reply with quote

Thanks for the help. Seems like the objects will work great.
Back to top
View user's profile Send private message Send e-mail
boots
Administrator


Joined: 16 Apr 2003
Posts: 5611
Location: Toronto, Canada

PostPosted: Wed May 07, 2003 11:47 pm    Post subject: Reply with quote

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 Smile 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
View user's profile Send private message
mohrt
Administrator


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

PostPosted: Thu May 08, 2003 4:25 am    Post subject: Reply with quote

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
View user's profile Send private message Visit poster's website
boots
Administrator


Joined: 16 Apr 2003
Posts: 5611
Location: Toronto, Canada

PostPosted: Thu May 08, 2003 5:04 am    Post subject: Reply with quote

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. Wink

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
View user's profile Send private message
mohrt
Administrator


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

PostPosted: Thu May 08, 2003 7:09 pm    Post subject: Reply with quote

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. Wink 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
View user's profile Send private message Visit poster's website
boots
Administrator


Joined: 16 Apr 2003
Posts: 5611
Location: Toronto, Canada

PostPosted: Thu May 08, 2003 7:26 pm    Post subject: Reply with quote

Yes, I get it Wink 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 Wink
Back to top
View user's profile Send private message
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