bradcathey has asked for the wisdom of the Perl Monks concerning the following question:

Fellow Monasterians:

This is probably more of a fundamental question about paths than pure Perl, but bear with me.

I'm trying to move common code used by several Web sites on the same host to a common area and share it. I've got most of the paths correct, but the links in the html (HTML::Template .tmpl files) aren't working. If I were using CGI::Application it's easier for me to understand from where the instance was called and trace the paths back that way. But I'm not using it on this (too much legacy code to convert at the moment). Here's the deal:

var/www/-----+ | /admin-------+ | | | login.pl | /templates-----+ | | | admin.tmpl | | | /images-----+ | | | save_btn.gif | /domain1-----+ | | | admin -> /var/www/admin (symlink) | /domain2-----+ | | | admin -> /var/www/admin | etc.

Setup:

Problem:

/admin-------+ | | | login.pl | | | /images-----+ | | | save_btn.gif
/domain1------+ | | | /images-----+ | | | save_btn.gif

Question: where do I put those common images and what do my .tmpl image paths need to look like to share the image and still have them appear? Thanks in advance.

—Brad
"The important work of moving the world forward does not wait to be done by perfect men." George Eliot

Replies are listed 'Best First'.
Re: Question about web trees, html paths, and HTML::Template
by Cody Pendant (Prior) on Apr 06, 2008 at 02:40 UTC
    I'm not sure what's going on in your second case, i.e. "If I move the /images under /admin (see below), no joy", but the first case the missing image is entirely to be expected.

    If your template contains

    <img src="images/save_btn.gif" />
    and you use that template to render a page at the URL
    http://www.domain1.com/admin/
    then, no, the image isn't "there", because "there", in that case, is the admin folder, and the path you're telling the browser to look at is
    admin/images/
    and it isn't there.

    You need to stop thinking about the location of the HTML::Template files. It makes no difference to the output. It doesn't matter where they are. They could be 47 folder levels down, or on another part of the filesystem altogether for all the browser cares.

    Instead, think about it from the browser's point of view. All the browser knows is that it's been given some HTML, at an admin folder URL, and told there's an image one folder down. And there isn't.

    What I would do is always use root-relative paths.

    If your images are in domain.name.com/foo/images/, then make all references to the begin with a leading slash:

    <img src="/foo/images/save_btn.gif" />
    it's easy to remember, gives you a consistent policy for both software and people to work with, and solves problems moving your code from one server to another.


    Nobody says perl looks like line-noise any more
    kids today don't know what line-noise IS ...

      So, the way I think about where an instance script in CGI::App is called from applies here as well. Makes sense now. Thanks.

      —Brad
      "The important work of moving the world forward does not wait to be done by perfect men." George Eliot
Re: Question about web trees, html paths, and HTML::Template
by dragonchild (Archbishop) on Apr 06, 2008 at 02:33 UTC
    Learn how to use Apache. Specifically, you need to understand the difference between Location (what the browser sees) and Directory (what the filesystem sees).

    My criteria for good software:
    1. Does it work?
    2. Can someone else come in, make a change, and be reasonably certain no bugs were introduced?
Re: Question about web trees, html paths, and HTML::Template
by nedals (Deacon) on Apr 06, 2008 at 03:02 UTC

    Only the files in domain1 and domain2 directories (and sub-directories) are web-accessible.
    Your template files are reached via a full server path and can be accessed from multiple domains (on the same server).

    One solution is to create a 3rd domain and put your common images, css, javascript, etc,. in that domain.
    You would then need to use 'http://www.domain3/......' to access these files
    (or you could put them all in something like 'http://www.domain1/common/....)

      Only the files in domain1 and domain2 directories (and sub-directories) are web-accessible.

      Sheesh, I can't believe I didn't know that. Okay, that's a great concept to keep in mind as I foray into the webhosting realm.

      I don't lose that much by keeping common images in each domain, as they never change, or if they do it's only for that domain. What still works is to have all domains using the same Perl and templates so, for example, instead of upgrading a Perl script in each of the domains, I only change it in one place.

      Perfect. Thanks all!

      —Brad
      "The important work of moving the world forward does not wait to be done by perfect men." George Eliot
        I don't lose that much by keeping common images in each domain, as they never change, or if they do it's only for that domain.

        You can use vhosts and still do the "shared image directory" thing by symlinking your image directory into each vhost's document tree, assuming this is on a unix-type system. (See man ln if you're not familiar with it already.)

        What still works is to have all domains using the same Perl and templates so, for example, instead of upgrading a Perl script in each of the domains, I only change it in one place.

        Think about using modules to control your script processing.

        use strict; use CGI; use CommonModule; use HTML::Template; my $q = CGI->new(); my $module = CommonModule->new( form_data => $q; ); my %templ_hash = $module->process_data(); my $template = HTML::Template->new(filename="$path/file.tmpl"); $template->param(%templ_hash); print $q->header(); print $template->output(); exit;

        This would be the basic form of your .cgi scripts in each domain which are unlikely to change. Now any edits you do in the module or template will be applied to each domain.