Re: [Cheetahtemplate-discuss] Re: Having trouble with #include
Brought to you by:
rtyler,
tavis_rudd
From: Mike O. <ms...@oz...> - 2003-08-05 23:52:18
|
On Tue, Aug 05, 2003 at 02:03:50PM -0400, Aaron Buhr wrote: > > searchList variables should be visible automatically. Use '#set global' > > instead of '#set' for other variables that should be visible in the > > included file. > > I preferred to avoid set global because it's not thread-safe and because in > general global vars seem to be a bit sloppy programming practice. However > if that's the simplest way I will do it. #include is a mixed blessing. The Cheetah developers rarely use it. It mainly exists for unforeseen situations where it's necessary, like yours is. The biggest thing we wanted to prevent was people using #include instead of $functions(), using global variables to pass data in and out, and having the include file cause unexpected changes to the parent's environment. Overuse of #include is endemic in PHP and some other languages, and we didn't want Cheetah to go that route. That's the reason '#set global' exists: to make it possible but inconvenient to share non-searchList data, while making it loud and clear what you're doing. Your example shows that our compromises with #include cut both ways: the same construct is good for some ppl but bad for others. But at least the construct allows ppl to do what they need somehow. > I am unclear on why re-loading the variable from session state gave me the > Nonetype error though. My original file (the including file) works fine and > looks more or less like this: > #extends CCGWeb_ServiceApp > #implements respond > #set $ValidPage1 = $IsValidPage1() > #set $serviceAppMsg = $self.session().value('serviceAppMsg', None) > #set $ssn = $self.session().value('ssn', None) > > #if $ValidPage1 > #include svcapp_page2 > #else > There was an error with your data:<br> > $serviceAppMsg<br> > Click <a href="svcapp_page1">here</a> to try again. > <meta http-equiv="Refresh" content="0; URL=svcapp_page1"> > #end if > > The second file was just this: > #set $ssn = $self.session().value('ssn', None) > Testing: $ssn > > Is the file specified at #include supposed to be a Cheetah template file, > or is it supposed to be the compiled Cheetah .py file? I just tried the .py > file and instead of the Nonetype not callable I get "Notfound: ssn". FWIW > if I do use set global then it works when I #include the template file, but > excluding the #set line from the included file. Are Cheetah directives not > allowed in included files? The included file should be Cheetah source. ('#include raw' inserts the content literally without interpretation.) I'm not sure if it's possible to include a compiled template module. The reason you're getting None (if I understand #include correctly) is that the include file is a separate template instance, so it has a different 'self'. Webware transaction info is not passed to the second instance, so it acts like a standalone template. We discovered recently that $methods() also don't have access to the Webware transaction. So your best choice is to '#set global' the data you need. '#set global' is not thread safe for template instances, but each thread can have its own instance. We haven't found a lot of need to reuse template instances since it takes no perceptable time to create a new instance. And with Webware, it's convenient to let Webware handle servlet threading and not worry if the template is thread unsafe, because it makes for much more convenient programming. I usually receommend avoiding #include and instead using methods or functions, especially if you need to pass data in and out. If your application has too many branches to do an 'if' switch, or you have to calculate which method to call at runtime, that could be difficult, but it's probably not insurmountable, and may lead to cleaner code than using #include. -- -Mike (Iron) Orr, ms...@oz... (ir...@se...) http://iron.cx/ English * Esperanto * Russkiy * Deutsch * Espan~ol |