From: Teemu <te...@io...> - 2003-05-29 19:35:11
|
Hi, I spent quite some time today to figure out how to process a template for a return value while calling an OI component. Basically, I try to create a OI component that returns the browser specific CSS for every site. I have the following in base_main: [% OI.comp('ua_select_css') %] With this I will call an action called ua_select_css (in class OpenInteract::Handler::UserAgent), which detects the client browser and feeds the ua_select_css template with params {browser => $browser}. ua_select_css looks something like this: [%- IF browser == 'ie' -%] [% PROCESS stylesheet style = '/link_rel/?link=ie.css' %] [%- ELSIF browser == 'moz' -%] [% PROCESS stylesheet style = '/link_rel/?link=moz.css' %] [%- ELSIF browser == 'opera' -%] [% PROCESS stylesheet style = '/link_rel/?link=opera.css' %] [%- END -%] link_rel is just an action that returns the generated CSS2 stylesheet with correct content-type etc. If my browser is IE, the above TT code will produce the following output: <link rel="stylesheet" type="text/css" href="/link_rel/?link=ie.css" /> Now my problem is how to retrieve ua_select_css (while in OpenInteract::Handler::UserAgent->ua_select_css), process it and return the processed output to base_main. I tried the following: sub ua_select_css { .... return $R->template->handler( {}, $params, { name => 'my_base::ua_select_css' } ); } But it didn't work. Some strange error ended up in error_log and OI ceased to function: --EXITED WITH ERROR from main handler eval block Error: Cannot process template!file error - Template with name [javascript] not found. [javascript] should be a [% BLOCK javascript %] in my base_main, which makes me wonder why it was not found... Ok. I browsed the OpenInteract::Template::Process code for help and tried the following (in place of template->handler): my $output; my $template = $R->template->initialize; $template->process( 'my_base::ua_select_css', $params, \$output ); return $output; It worked! Now I ask, why didn't template->handler work through a component call? Is the above method I used correct or is there a better way to do this? Also, I noticed a problem in Pod documentation of OpenInteract::Template::Process: initialize( \%config ) initialize method doesn't know anything about parameters, so I wonder why \%config is specified in the usage instructions. %config could actually be very nice for initialize if I could pass some parameters to Template->new() through it. Something like: my $template = $R->template->initialize(TRIM => 1); Would enable TT to trim generated output. Is there currently a way to do this? -- Sincerely, Teemu Arina http://www.mimerdesk.org |