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 /memberpag es/members home.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
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
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
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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.