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

What vars are referenced in templates

 
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 -> Tips and Tricks
View previous topic :: View next topic  
Author Message
jamgold
Smarty n00b


Joined: 07 Oct 2004
Posts: 3

PostPosted: Thu Oct 07, 2004 4:22 pm    Post subject: What vars are referenced in templates Reply with quote

Hi

I am "relatively" new to smarty, but have worked with PHP for quite some time.

My question is simple: is there a way to find out which variables are referenced in templates?

My situation is this: I have nested templates (include) with arbitrary variables in it. The backend needs to find out what variables are needed in the templates to retrieve the proper values.

Is there a way to register some filter or function that is called when variables are accessed in a template?

thanks

Jan
Back to top
View user's profile Send private message
mohrt
Administrator


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

PostPosted: Thu Oct 07, 2004 4:33 pm    Post subject: Reply with quote

There isn't a good way outside of scraping your templates for variables mentioned then assigning those. There are a few alternate ways to accomplish this though.

You could use an assigned object in your template to fetch the variables on-demand.

So instead of:

{$foobar}

You would use:

{$obj->var('foobar')}

You could also use a (less flexible) custom template function:

{getvar name="foobar"}

Or use a custom modifier on a dummy var:

{$dummy|getvar:"foobar"}

All of these options make the template vars a bit longer, but cut out the need to pre-assign them. I like the object, you can use it much like any assigned template var, and you can easily tweak the methods to do whatever you want.
Back to top
View user's profile Send private message Visit poster's website
jamgold
Smarty n00b


Joined: 07 Oct 2004
Posts: 3

PostPosted: Thu Oct 07, 2004 9:05 pm    Post subject: Reply with quote

mohrt wrote:

You could use an assigned object in your template to fetch the variables on-demand.

So instead of:

{$foobar}

You would use:

{$obj->var('foobar')}
.


Thats actually what I came up with, so I am happy not to be too dumb after all. I just wondered if there was a "better" way, like some sort of filter, or sub-classing the Smarty.compiler.class.

It turns out that my biggest problem with what I am trying to accomplish is the way PHP4 handles references. I develop on PHP5 and then copy to the production server, which has PHP4 on it. Why make it easy on myself.

Thanks for your reply

Jan
Back to top
View user's profile Send private message
mohrt
Administrator


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

PostPosted: Thu Oct 07, 2004 9:30 pm    Post subject: Reply with quote

Be sure to use $smarty->assign_by_ref() for the object assignments. It should work fine under PHP5 too.
Back to top
View user's profile Send private message 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 -> Tips and Tricks 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