JSP include directive (perhaps newby)

C

cartercc

Using Perl/Apache/MySQL on a Linux platform for web development.

What is the Perl equivelent for the <%@ include file="/some/url" %>
directive.

Particularly, I want to be able to include the standard HTML head
section, menu bars, footers, etc., by including html code in CGI
scripts.

Thanks, CC.
 
S

Scott Bryce

Using Perl/Apache/MySQL on a Linux platform for web development.

What is the Perl equivelent for the <%@ include file="/some/url" %>
directive.

Things are done differently in Perl CGI.

Particularly, I want to be able to include the standard HTML head
section, menu bars, footers, etc., by including html code in CGI
scripts.

It is usually better separate the HTML from the Perl script. For one way
to do it, look here:

http://search.cpan.org/~samtregar/HTML-Template-2.7/Template.pm
 
J

J. Gleixner

Using Perl/Apache/MySQL on a Linux platform for web development.

What is the Perl equivelent for the <%@ include file="/some/url" %>
directive.

I'd suggest that if you want to embed perl, instead of JSP, then
investigate one of these:

Embperl http://perl.apache.org/embperl/
HTML::Mason http://masonhq.com/

Particularly, I want to be able to include the standard HTML head
section, menu bars, footers, etc., by including html code in CGI
scripts.

If you're already using CGI, then look at the CGI module on CPAN and a
templating system, like HTML::Template.

These and many more modules are available on CPAN. To search CPAN use:
http://search.cpan.org
 
C

cartercc

Thanks, Scott and J.

I've looked at mason, embperl, Template.pm, and some other things. I
read Randal's perlboot and Tom's perltoot, and other perldocs as well.

What I'm looking for is a nice, easy way to embed HTML, something along
the lines of:

<cfinclude template = "template.cfm"> (for ColdFusion)
<script src="template.js"></script> (for JavaScript)
<%@ include file="template.jsp" %> (for JSP)

I can't believe that Perl doesn't have something that will do this
natively, without using external modules. Is it possible to write a
simple class with a single 'print' method, and call that method in the
appropriate place in the code?

The way I have done things with Perl is to write a function (call it
'&header') and call it where I need an HTML header. However, this
requires that I copy and paste it in every page. I want to put the code
in a separate directory (call it ../includes) and be able to call it
every time I write a new page, without having to import a module.

I *would* use the JavaScript method of importing an external JS file,
but I'm trying to avoid stuff that would show up in the client, and
besides, I'm not too fond of JS anyway.

Thanks very much, CC.
 
J

Jürgen Exner

What I'm looking for is a nice, easy way to embed HTML, something
along the lines of:

<cfinclude template = "template.cfm"> (for ColdFusion)
<script src="template.js"></script> (for JavaScript)
<%@ include file="template.jsp" %> (for JSP)

I can't believe that Perl doesn't have something that will do this
natively, without using external modules. Is it possible to write a
simple class with a single 'print' method, and call that method in the
appropriate place in the code?

Well, I think there is a little bit of confusion here.
Your examples above do a simple _textual_ inclusion of the template file
into your code. In short, they behave as if the include statement would be
replaced with the content of the named file. The prototypical example is
maybe the #include directive from CPP.

However higher-level programming languages typically have other means to
organize programming in the large, often called modules, which provide a
much cleaner and more structured interface than a mere textual inclusion.

Perl supports both.
- perldoc -f do
- perldoc perlmod
If you want to run your own home-grown system, then certainly you are free
to do it. It works reasonable well for small scale project.

However, having said that, those people who are using Perl to write CGI
scripts have come with better ways to tackle large scale, professional
projects and they wrote those modules to help them. It is your choice with
path you want to go.
The way I have done things with Perl is to write a function (call it
'&header') and call it where I need an HTML header. However, this
requires that I copy and paste it in every page. I want to put the
code in a separate directory (call it ../includes) and be able to
call it every time I write a new page, without having to import a
module.

Well, your ./include very much sounds like a module library to me.

jue
 
C

cartercc

Well, your ./include very much sounds like a module library to me.

Yes, I understand that it does. I can write a 'module' that will do
what I want. I just don't know how to *include* it in a CGI script in
the same way that I would use a JSP include directive or an external JS
script.

This is probably a real simple question, and I feel like a real idiot
asking it, but I don't know how to do it, and I can't find where the
issue is addressed in the documentation.

I'll look at the perlmod doc, and thank you for your suggenstion.

CC
 
J

Jürgen Exner

Yes, I understand that it does. I can write a 'module' that will do
what I want. I just don't know how to *include* it in a CGI script in
the same way that I would use a JSP include directive or an external
JS script.

Maybe you are just looking for "perldoc -f use"?

jue
 
A

A. Sinan Unur

I can't believe that Perl doesn't have something that will do this
natively, without using external modules. Is it possible to write a
simple class with a single 'print' method, and call that method in the
appropriate place in the code?

You can use CGI::Application in combination with HTML::Template.
(I haven't used it, but CGI::prototype is also similar in spirit).

Using HTML::Template, you put together an HTML file that has all
the static parts filled in, and has special placeholders for
information to be inserted by the Perl script.

In fact, my first ever CGI script which I still use (and, yes, I
would like to believe, if I did it today, it would be better), uses
different templates to choose between different display styles for
my photo album pages.

<URL: http://www.unur.com/cgi-bin/photobrowser?loc=sinan/usa_02_03;tmpl=cols3;rnd=yes>

versus

<URL: http://www.unur.com/cgi-bin/photobrowser?loc=sinan/usa_02_03;tmpl=plain;rnd=no>

The source code is at:

<URL: http://www.unur.com/comp/photobrowser/photobrowser.txt>

Please do not think I am claiming this is some great programming.

I wanted to have an easy way to separate presentation from logic,
and I wanted to be as lazy as possible in creating my photo album
pages.

The script allows me to upload a bunch of pictures and thumbnails,
along with a single text file containing captions for the pictures.
All of the presentation is in templates, so I may eventually even
correct the HTML so it validates without touching the program ever.

One of these days ...

Sinan
 
S

Scott Bryce

What I'm looking for is a nice, easy way to embed HTML, something along
the lines of:

<cfinclude template = "template.cfm"> (for ColdFusion)
<script src="template.js"></script> (for JavaScript)
<%@ include file="template.jsp" %> (for JSP)

What is usually recommended in Perl CGI is to embed the values of Perl
variables into the HTML. That is what HTML::Template is for.
I can't believe that Perl doesn't have something that will do this
natively, without using external modules.

1) Perl is not first and foremost a CGI language.

2) Perl has a different paradigm for combining static content and
generated content when used as a CGI language.

3) There is nothing wrong with using modules.
Is it possible to write a
simple class with a single 'print' method, and call that method in the
appropriate place in the code?

You could do that. But writing a class would mean writing your own
module rather than using an existing module. It would go something like:

Open the file that contains your headers.
If the open was successful, read and print the contents.
Close the file.

I think you will find the HTML::Template way of doing things easier to
maintain.

Open an HTML template that contains the static HTML.
Replace markers in the HTML template with the generated content.
Print the contents of the combined template/generated content.
The way I have done things with Perl is to write a function (call it
'&header') and call it where I need an HTML header. However, this
requires that I copy and paste it in every page.

Or write it into a module and call it when you need it.
I want to put the code
in a separate directory (call it ../includes) and be able to call it
every time I write a new page, without having to import a module.

You will have to give us a good reason why you don't want to use modules.
 
C

cartercc

You will have to give us a good reason why you don't want to use modules.

Actually, I *do* want to use modules. I guess I was fixated on JSP, and
looking for something resembling the include directive.

I've been using mod_perl with Apache writing database driven websites
for about five years. Also, I've worked in academic institutions
(undergraduate and graduate) and (as you know) the academic community
is head over heels in love with Java, and that is bleeding over into
JSP. I find the whole JSP thing very attractive, with the custom tags,
beans, etc., but am put off by the overhead of Java (write, compile,
test, re-write, re-compile, re-test, etc.)

I know that Perl has a different paradigm, and am used to academic
types telling me that Perl isn't a programming language, but I'm still
looking for a Perl way to do Java -- with the different declarations,
scriptlets, directives, and actions.

On the other side, I find Perl's way much simpler and easier that
Java's way. "if ($a == $b)" rather than "if (a.equals(b))" I honestly
think that, in a head to head competition between CGI/Perl and
Java?JSP, no one can predict the final 'winner' and feel that the
outcome would be fiercely contested.

CC
 
S

Sherm Pendley

Actually, I *do* want to use modules. I guess I was fixated on JSP, and
looking for something resembling the include directive.

I've been using mod_perl with Apache writing database driven websites
for about five years.

If you're using mod_perl, you might have a look at Apache::ASP. It uses
the same page-embedding syntax as ASP on IIS, and similar request,
session, and other objects.

Not exactly my own cup of tea, I'm afraid, but it sounds like you might
be looking for something along those lines.

sherm--
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,540
Members
45,024
Latest member
ARDU_PROgrammER

Latest Threads

Top