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
dreamshockDesignAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

gripeCommented:
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.
0
gripeCommented:
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.
0
godspropyCommented:
Probably the easiest transition from your existing script would be:

$memberhomepage="$fullpath/memberpages/membershome.shtml";
   open (DATA5, $memberhomepage)|| die("Could not open file!");
    while(<DATA5>)
    {
      $_=~s/\$(\w+)/$$1/g;
      print $_;
     }
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Scripting Languages

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.