function getFile($filename) {

  if (!$filename) {
    return 0;
  }
  $filename = "tmpl/" . $filename . ".tmpl";
  $fp = fopen($filename,r);
    if (!$fp) {
      return 0;
    }
  $data = fpassthru($fp);
  fclose($fp);
  return $data;

}

$name = "My Name";
eval("dooutput(\"welcome\");");

I am currently using this code to call templates from a folder then have eval help parse the vaiables..

I get a few errors.

echo getFile("welcome"); // Outputs:Hello $name11

Now the 11 isnt in the template and I am unsure as to how it gets in there.

$name = "My Name";
eval("dooutput(\"welcome\");"); 

Once I use this code though, it just outputs: Hello $name

I am confused as to why this isnt working, if you can help that would be great.

  • Rayn

    Maybe you should post a copy of the template so we can see what the template looks like...

      ...or just read the manual on the functionality of [man]fpassthru/man

      Th eproblem is that you are using [man]fpassthru/man to pass thru the value to the browser (since this function just reads to the end of the supplied file pointer and dumps the output to the browser. The return value is likely to be how much data was outputted, i.e. 11 bytes - which is the number you are passing back from the function and then echoing out on the screen.

      Additonally, [man]fpassthru/man closes the file pointer for you, so there is no need to perform this action.
      So - a cleaned up version would be: -

      function getFile($filename)
      {
        if (!$filename) {
          return 0;
        }
        $filename = "tmpl/" . $filename . ".tmpl";
        $fp = fopen($filename,r);
          if (!$fp) {
            return 0;
          }
        fpassthru($fp);
      }
      
      getFile("welcome");
      

      Alternatively - if you want to return the value instead, then try using something like: -

      function getFile($filename)
      {
        if (!$filename) {
          return 0;
        }
        $filename = "tmpl/" . $filename . ".tmpl";
      
        if (!file_exists($filename)) {
            return 0;
          }
        return(implode ('', file ($filename)));
      }
      
      echo getFile("welcome");
      

      (I haven't checked the above code, but the general idea should be ok)

        Note that you could also just fopen() and fread() in the second example instead of using file() and implode()

          Write a Reply...