Thread: [htmltmpl] Filter Syntax
Brought to you by:
samtregar
From: Chris F. <cf...@do...> - 2007-07-26 16:01:59
|
Folks, I'm having (what I hope is) a syntax problem. I'm trying to use the "filter" option to run a sub on a template at create time, but I want the sub in another package and I need to some other objects with it. >From reading the docs I thought that the HTML::Template object would simply be the first incoming param, but its not. For example, here is how I wanted to create my template object: my $template_header = new HTML::Template(filename => 'CommonHeader.tmpl', filter => { sub => scripts::MyPackage->initialize_template_header($r,$db,$site_settings,$user_s ession_data,\%form_data) }, ); Then in "MyPackage.pm", I would have sub initialize_template_header { my ($self,$template,$r,$db,$site_settings,$user_session_data,$form_data) = @_; .... } $self is correctly defined, but $template becomes the first object I send in ($r in this case). How do I get the HTML::Template object?? TIA!! -Chris |
From: Robert H. <si...@gm...> - 2007-07-27 13:21:33
|
Chris Faust wrote: > Folks, > > > > I'm having (what I hope is) a syntax problem. > > > > I'm trying to use the "filter" option to run a sub on a template at > create time, but I want the sub in another package and I need to some > other objects with it. > > > > From reading the docs I thought that the HTML::Template object would > simply be the first incoming param, but its not. > > > > For example, here is how I wanted to create my template object: > > > > my $template_header = new HTML::Template(filename => 'CommonHeader.tmpl', > filter => { sub => > scripts::MyPackage->initialize_template_header($r,$db,$site_settings,$user_session_data,\%form_data) > }, > ); > > > > Then in "MyPackage.pm", I would have > > > > sub initialize_template_header { > my > ($self,$template,$r,$db,$site_settings,$user_session_data,$form_data) = @_; > .... > > } > > > > $self is correctly defined, but $template becomes the first object I > send in ($r in this case). > > > > How do I get the HTML::Template object?? > This may have an answer for you: http://www.perl.com/pub/a/2006/11/30/html-template-filters.html HTH Robert |
From: Mathew R. <mat...@ne...> - 2007-08-10 04:55:36
|
Hi Chris, I don't think anyone has answered yet... The purpose of a filter is simply to modify the template-text-blob, before being compiled into its internal form, ie: the only thing passed into your filter, is the template. Here is a simple/common filter: my $filter = sub { my $text_ref = shift; my $match = qr/%([-\w\/\.+]+)%/; $$text_ref =~ s/$match/<TMPL_VAR NAME="$1">/g; }; This filter replaces %some_var% with <TMPL_VAR some_var>. Notice that the sub is called with only a text-ref. >From your example below, it looks like you are initialising the sub => ... the result of the function call to initialize_template_header(). What does this function return? for this syntax to work, it will need to return a sub-ref. To answer your last question, you need to make use of "use vars ..." to stash your H::T reference in, eg: { ... use vars qw($HT); $HT = H::T->new(... filter => some_filter ); ... $HT->output(); } sub some_filter { my $textref = shift; use vars qw($HT); ... access $HT ... ... modify $$textref or something... } But I'm not sure why you need to access H::T from within your filter. I suspect that you are trying to something with filters, that it simply wont do -> I might be able to suggest an alternative solution if I know what the problem was. cheers, Mathew Robertson Chris Faust wrote: > > Folks, > > > > I'm having (what I hope is) a syntax problem. > > > > I'm trying to use the "filter" option to run a sub on a template at > create time, but I want the sub in another package and I need to some > other objects with it. > > > > From reading the docs I thought that the HTML::Template object would > simply be the first incoming param, but its not. > > > > For example, here is how I wanted to create my template object: > > > > my $template_header = new HTML::Template(filename => 'CommonHeader.tmpl', > filter => { sub => > scripts::MyPackage->initialize_template_header($r,$db,$site_settings,$user_session_data,\%form_data) > }, > ); > > > > Then in "MyPackage.pm", I would have > > > > sub initialize_template_header { > my > ($self,$template,$r,$db,$site_settings,$user_session_data,$form_data) > = @_; > .... > > } > > > > $self is correctly defined, but $template becomes the first object I > send in ($r in this case). > > > > How do I get the HTML::Template object?? > > > > TIA!! > > -Chris > > > > > > ------------------------------------------------------------------------ > > ------------------------------------------------------------------------- > This SF.net email is sponsored by: Splunk Inc. > Still grepping through log files to find problems? Stop. > Now Search log events and configuration files using AJAX and a browser. > Download your FREE copy of Splunk now >> http://get.splunk.com/ > ------------------------------------------------------------------------ > > _______________________________________________ > Html-template-users mailing list > Htm...@li... > https://lists.sourceforge.net/lists/listinfo/html-template-users > |