You are viewing a plain text version of this content. The canonical link for it is here.
Posted to modperl@perl.apache.org by James Hartling <ha...@doubleprime.com> on 2003/04/24 05:00:43 UTC

internal redirection within templates

Hi everyone,

I have a content handler which uses Text::Template to render some 
simple web pages.  I'd like to be able to do the following from 
within these templates:

(perl and html here)
{
  if ($getmeouttahere) {
    $request->internal_redirect("/whereiwannabe.html");
  }
  # 'return' seems pointless since we're in a template
}
(more perl and html here)

Internal redirects are preferred since I've already populated pnotes 
with all kinds of stuff.  Incidentally, my PerlInitHandler recognizes
the internally redirected request, grabs the previous request, and
copies pnotes to the new request.  This seems to work well so far.

I know that internal_redirect should generally be called only from 
the content handler itself.  Am I asking for trouble by doing the 
above, beyond just having the rest of the template get evaluated?  
If so, are there any recommendations on how to do this kind of 
thing?  Do other templating systems handle this more elegantly?

regards
JimH

Re: internal redirection within templates

Posted by James Hartling <ha...@doubleprime.com>.
thanks Perrin - that all makes a lot of sense.

The basic idea here is to give page developers an easy way to do conditional
redirects - I'll look for a better way to do it.  The ASP method seems like
a good contender...

cheers
JimH

----- Original Message -----
From: "Perrin Harkins" <pe...@elem.com>
To: "James Hartling" <ha...@doubleprime.com>
Cc: <mo...@perl.apache.org>
Sent: Thursday, April 24, 2003 12:01 AM
Subject: Re: internal redirection within templates


> On Wed, 2003-04-23 at 23:00, James Hartling wrote:
> > I have a content handler which uses Text::Template to render some
> > simple web pages.  I'd like to be able to do the following from
> > within these templates:
> >
> > (perl and html here)
> > {
> >   if ($getmeouttahere) {
> >     $request->internal_redirect("/whereiwannabe.html");
> >   }
> >   # 'return' seems pointless since we're in a template
> > }
>
> You're not doing any cleanup.  If you have anything hanging (locked
> files, uncommitted DBI transactions, etc.) it could be a problem.
>
> > I know that internal_redirect should generally be called only from
> > the content handler itself.  Am I asking for trouble by doing the
> > above, beyond just having the rest of the template get evaluated?
>
> Yes, in my opinion.  How is your program structured?  Why don't you know
> whether or not you want to run this template before you start running
> it?  You could easily end up with spaghetti logic here.  You are putting
> control flow logic into a place that ideally would only be worried about
> generating pretty HTML.
>
> > If so, are there any recommendations on how to do this kind of
> > thing?
>
> Make the decisions ahead of time, in your content handler.  However,
> there are different philosophies about this.  If you prefer the PHP/ASP
> page-oriented style over a pipeline or MVC style of processing, then
> what you're doing is necessary sometimes.
>
> Personally, if I were doing this I would try to do it without the
> redirect.  You could write a simple sub that calls a different template
> and passes all of the current context to it.  That would be more
> lightweight.  You would have to make sure to abort the current template
> to avoid getting output from it.
>
> >   Do other templating systems handle this more elegantly?
>
> It varies.  Many have some kind of pre-written version of the sub I
> mentioned above available.  Text::Template is a very roll-your-own
> solution for things like this.  Apache::ASP offers a reasonably nice way
> to do with it with $Server->Transfer().  See
> http://www.apache-asp.org/objects.html#%24Server-%3ETra98c592dc for
> more.
>
> - Perrin
>


Re: internal redirection within templates

Posted by Perrin Harkins <pe...@elem.com>.
On Wed, 2003-04-23 at 23:00, James Hartling wrote:
> I have a content handler which uses Text::Template to render some 
> simple web pages.  I'd like to be able to do the following from 
> within these templates:
> 
> (perl and html here)
> {
>   if ($getmeouttahere) {
>     $request->internal_redirect("/whereiwannabe.html");
>   }
>   # 'return' seems pointless since we're in a template
> }

You're not doing any cleanup.  If you have anything hanging (locked
files, uncommitted DBI transactions, etc.) it could be a problem.

> I know that internal_redirect should generally be called only from 
> the content handler itself.  Am I asking for trouble by doing the 
> above, beyond just having the rest of the template get evaluated?

Yes, in my opinion.  How is your program structured?  Why don't you know
whether or not you want to run this template before you start running
it?  You could easily end up with spaghetti logic here.  You are putting
control flow logic into a place that ideally would only be worried about
generating pretty HTML.

> If so, are there any recommendations on how to do this kind of 
> thing?

Make the decisions ahead of time, in your content handler.  However,
there are different philosophies about this.  If you prefer the PHP/ASP
page-oriented style over a pipeline or MVC style of processing, then
what you're doing is necessary sometimes.

Personally, if I were doing this I would try to do it without the
redirect.  You could write a simple sub that calls a different template
and passes all of the current context to it.  That would be more
lightweight.  You would have to make sure to abort the current template
to avoid getting output from it.

>   Do other templating systems handle this more elegantly?

It varies.  Many have some kind of pre-written version of the sub I
mentioned above available.  Text::Template is a very roll-your-own
solution for things like this.  Apache::ASP offers a reasonably nice way
to do with it with $Server->Transfer().  See
http://www.apache-asp.org/objects.html#%24Server-%3ETra98c592dc for
more.

- Perrin