perl-embperl mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Angus Lees <...@inodes.org>
Subject Re: Migrating from regular mod perl scripts
Date Sun, 22 Jun 2003 13:28:11 GMT
At Mon, 16 Jun 2003 13:55:53 -0700 (PDT), Patrick Galbraith wrote:
> I'm in the process of trying to see if Embperl is a good templating 
> solution, but am not sure where to start.

read the Intro document and Embperl manpage that come with Embperl.

> Also, I'm wondering one simple thing. How would you use Embperl to
> do what regular mod perl script does... something like:

taking it piece by piece:

> #!/usr/bin/perl
> 
> use MyLib::Util;
> use MyLib::DB;

[!
 use MyLib::Util;
 use MyLib::DB;
!]

> my $util = new MyLib::Util;
> my $db = new MyLib::DB; # this is imaginary...
> my $form = $util->getForm();

[-
 $util = new MyLib::Util;
 $db = new MyLib::DB;
-]
 
> my $op = $form->{op} # form parameter from a post...
> 
> my $ops = {
> 	'foo1' => \&fooOp1,
> 	'foo2' => \&fooOp2
> };
> 
> $opts->{$op}->($db,$form);

you could do this using coderefs (as you have), but i'd do it using a
simple if/elsif block, like:

[$ if $fdat{op} eq 'foo1' $]
 do fooOp1 steps
[$ elsif $fdat{op} eq 'foo2' $]
 do fooOp2 steps
[$ else $]
 some sort of error handling
[$ endif $]

or you could do as you were doing by defining fooOp1 and fooOp2 subs
using either normal perl subroutine declarations (in [! !]) or using
embperl [$ sub $] declarations if the functions were going to print
lots of HTML. then call $opts->{$fdat{op}}->($db, \%fdat) as before.

> sub fooOp1 {
>  my ($db,$form) = @_;
> 
>  print <<EOT;
> lots of html tags... blah blah $form->{valuewhatever}
> EOT
> }

[$ sub fooOp1 $]
 <some html>
 that mentions [+ $fdat{valuewhatever} +]
 </some html>
[$ endsub $]

you can now invoke fooOp1() as a normal perl function - although
recursion gets a little more complicated.

> sub fooOp2 {
>  my ($db,$form) = @_;
> 
>  $recordsorwhatever = $db->getData();
> 
>  if($recordsorwhatever) {
> 	print "html stuff... ";
> 	... loop through records...
>  } else {
> 	print "print that record isn't there... blah...";
>  }
> }

you could do it as a perl function, but then you lose a lot of the
advantages embperl might provide.  I tend to go for embperl subs
whenever the function will output html.

[$ sub fooOp2 $]
[-
 my ($db, $form) = @_;
 $recordsorwhatever = $db->getData();
-]

[$ if $recordsorwhatever $]

 html stuff

 [$ foreach $record (@$recordsorwhatever) $]
  loop through records
 [$ endforeach $]

[$ else $]
 record isn't there.. blah
[$ endif $]

[$ endsub $]

> So, this is code that display html vs. something like embperl that has 
> logic in the content. Do you have a template that has mostly code in it 
> and then the logic calls more content laden templates?

i do whatever fits the problem at hand most simply ;)

i usually find it works out better in complicated cases if you
restrict the "top level" html file to logic, form processing and
trivial html - and use separate (reusable) .epl files for displaying
particular "components".  that lets you reuse the component on
multiple pages, while still having separate logic in each page.

in this case, that would probably mean putting fooOp1 and fooOp2 in
separate files and calling Execute() in the parent page to include the
right one.  of course, unless you do actually reuse fooOp1 or fooOp2,
you haven't really gained anything.


for some "real world" examples, see the embperl website itself or
http://slug.org.au/ and click the "source" link at the bottom of the
page.

-- 
 - Gus

---------------------------------------------------------------------
To unsubscribe, e-mail: embperl-unsubscribe@perl.apache.org
For additional commands, e-mail: embperl-help@perl.apache.org


Mime
View raw message