[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [cgiapp] Re: Benefits of using load_tmpl insted of using HTML::Template directly


Saturday, April 19, 2003, 5:55:03 PM, you wrote:
MS> Aleksey --
MS> On Thu, Apr 17, 2003 at 08:22:03PM +0400, Aleksey Serba wrote:
>>     Hi, C::A people!
>> 
>> I prefer to use interface
>> 
>> 1.
>> $content = MyTemplateModule::parse_template($filename, %vars),
>> where
>>      $filename - H::T template,
>>      %vars - hash with H::T parameters ( pass to H::T::param as hash reference )
>> 
>> parse_temp create H::T instance with some options, pass to H::T hash
>> reference with parameters and return H::T::output.
>> 
>> instead of using
>> 
>> 2. H::T direct calls from code generating pages.
>> 
>> Is there some benefits of using load_tmpl in my C::A applications?

MS> On Thu, Apr 17, 2003 at 08:22:03PM +0400, Aleksey Serba wrote:
>>     Hi, C::A people!
>>
>> I prefer to use interface
>>
>> 1.
>> $content = MyTemplateModule::parse_template($filename, %vars),
>> where
>>      $filename - H::T template,
>>      %vars - hash with H::T parameters ( pass to H::T::param as hash reference )
>>
>> parse_temp create H::T instance with some options, pass to H::T hash
>> reference with parameters and return H::T::output.
>>
>> instead of using
>>
>> 2. H::T direct calls from code generating pages.
>>
>> Is there some benefits of using load_tmpl in my C::A applications?

MS> I wanted to use H::T the same way-- every time I used it I wanted to
MS> pass the same arguments to the "new" constructor, and pass a common set
MS> of things to "param". I found that by over-riding load_tmpl(), I could
MS> cleanly do both. Below is an example of some real code that does this.

MS> Watch out though, it uses some evil global variables you may want to
MS> avoid. Here's a breakdown of what my load_tmpl() is doing.

MS> 1. It passes some defaults to "new" from a configuration file
MS> 2. It passes some parameters to the "param" function.

MS> 3. It creates a variable to help integrate with Dreamweaver by setting a
MS> "base href" variable. This has always felt like a hack, but it works
MS> reliably. We store our templates in a "Templates" folder so that they
MS> are available for processing by Dreamweaver along with the rest of the
MS> site. The problem is, relative links will only work after the the file
MS> is loadded through CGI::App if the script is same number of levels down
MS> in the directory tree, and "PATH INFO" isn't used. By creating a "base
MS> href" variable, we force the links to work relative to where ever the
MS> file is stored in the templates directory

MS> # In this case we override the default method so that we can provide our own
MS> # defaults, and stick the config variables into the environment.

MS> sub load_tmpl {

MS>   use File::Basename;

MS>   # It's important to call fileparse_set_fstype() here because if
MS>   # the function was used elsewhere in the same application
MS>   # (such as image uploading), it could be set to a non-unix value
MS>   # We always what UNIX here, which is what calling it with no arguments does.
MS>   # -mls on 11/06/02
MS>   fileparse_set_fstype('Unix');

MS>   my $self = shift;
MS>   my ($tmpl_file, @extra_params) = @_;

MS>   my $fq_tmpl_file = $self->tmpl_path() . $tmpl_file;


MS>   my $base_href_append = dirname($tmpl_file)."/";


MS>   require HTML::Template;
MS>   # set up the templates with some defaults
MS>   my $t = HTML::Template->new_file($fq_tmpl_file, @extra_params, %{ $CFG{HTML_TMPL_DEFAULTS} });

MS>   # One of the config variables is handled differently: it always returns the current instance script
MS>   $CFG{INSTANCE_CGI} = $ENV{SCRIPT_NAME};

MS>   # supply global config variables to the templates. Rather than pollute the name space
MS>   # let's prefix them with cfg_.
MS>   $t->param(
MS>   map {
MS>     my $k = $_;
MS>     'CFG_'.$k =>  $CFG{$k};
MS>   } keys (%CFG)
MS>   );

MS>   $t->param(
MS>     # Update the Base Href with appended directory name for deeper Templates
MS>     'CFG_TMPL_BASE_HREF' => qq^<base href="$CFG{TMPL_BASE_HREF}$base_href_append">^,
MS>   );

MS>   return $t;
MS> }
MS> --
MS> http://mark.stosberg.com/ 

Hello Mark!

MS> I wanted to use H::T the same way-- every time I used it I wanted to
MS> pass the same arguments to the "new" constructor, and pass a common set
MS> of things to "param". I found that by over-riding load_tmpl(), I could
MS> cleanly do both. Below is an example of some real code that does this.
Yes, it is _one_ solution of problem.

But i'd like to separate these two tasks - parsing html templates and C::A
framework. :)

Thank you very much for your detailed response!

    Aleksey


---------------------------------------------------------------------
Web Archive:  http://www.mail-archive.com/suppressed/
To unsubscribe, e-mail: suppressed
For additional commands, e-mail: suppressed


Mail converted by mhonarc 2.6.15
This archive provided courtesy of JSW4.NET, Internet Hosting Services for Small Business.