?
Solved

Converting variables from an html include...

Posted on 2004-11-04
3
Medium Priority
?
189 Views
Last Modified: 2013-12-25
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
0
Comment
Question by:dreamshockDesign
  • 2
3 Comments
 
LVL 13

Expert Comment

by:gripe
ID: 12494666
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
 
LVL 13

Expert Comment

by:gripe
ID: 12494734
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
 
LVL 2

Accepted Solution

by:
godspropy earned 500 total points
ID: 12549457
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

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Shoutout to Emily Plummer (http://www.experts-exchange.com/members/eplummer26.html) for giving me this article! She did most of it, I just finished it up and posted it for her :)    Introduction In a previous article (http://www.experts-exchang…
Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
The viewer will learn how to count occurrences of each item in an array.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses
Course of the Month17 days, 1 hour left to enroll

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question