Link to home
Start Free TrialLog in
Avatar of dreamshockDesign
dreamshockDesign

asked on

Converting variables from an html include...

Hi there,

As a common way of working, I tend to code a lot of sites to include html files, simply to make editing the site by others a lot easier, using ordinaly development tools etc.

I include it using the following syntax...

$memberhomepage="$fullpath/memberpages/membershome.shtml";
   open (DATA5, $memberhomepage)|| die("Could not open file!");
     while(<DATA5>)
                                                 {
       print $_;
      }


However, if I want to use variables in the included page, it will just output $variable rather than converting it to an actual variable.

I have done a find a replace for specific words sometimes for variables, but its quite a long winded way round. Is it possible to say kind of, replace all the instances of any $variables with the respective $variables content?

Thanks for any help in advance
Avatar of gripe
gripe

It looks like you're using Perl for your CGI, so I'll write from that assumption.

Firstly, you should consider using one of Perl's many templating modules. Text::Template, Template::Toolkit, HTML::Template... These modules are designed to do the very thing you're asking about.

Secondly, if you want to roll your own as above, you should consider using placeholders in your templates that can later be replaced by a regex. (Such as ##MY_VARIABLE##) This is usually what I do when I have small scale templates to work with. Instead of printing the file as you read it, just dump the contents into a variable and then apply whatever regexes to it. Continuing with the example above:

{ local $/; $file_contents = <DATA5> }

$file_contents =~ s/##MY_VARIABLE##/My actual value/g;

print $file_contents;

I still suggest Text::Template for lightweight templating or Template::Toolkit for the kitchen sink.
Note: You could also apply your regex to the incoming lines from your file line by line (as in your original example) as long as you don't expect your incoming data to be multiline. This is but one of the many caveats using a templating module will eliminate.
ASKER CERTIFIED SOLUTION
Avatar of godspropy
godspropy

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial