Jump to content

[SOLVED] How can I reduce the repetition of this code?


Cep

Recommended Posts

Hello,

 

I use a template class to create my html pages from PHP. This class will basically call up template files from a directory and then assign them to a PHP variable. To assemble the page I read in each template using the eval() construct, so that any PHP code in those templates is evaluated as PHP code in my script. These variables then appear in a "super" template and when the script is run I get a nice little html page :)

 

The problem is I need to repeat several lines of code throughout my php scripts and I think it would be better if I created a function to do this but I am a bit unsure how because I am using eval(), which I do not believe returns a value in itself.

 

These are the lines I repeat,

eval("\$header = \"".$tpl->getTemplate("header_guest", "/headers")."\";");
eval("\$body = \"".$tpl->getTemplate("user_login", "/body/user")."\";");
eval("\$footer = \"".$tpl->getTemplate("footer_guest", "/footers")."\";");
// Output main template
eval("\$tpl->outputTemplate(\"".$tpl->getTemplate("main")."\");");

 

The first three lines are sub templates, the variables $header, $body and $footer appear in the main template. The method getTemplate is the method in my template class which will read in a file with some HTML code in them. You don't need to see this class because its very basic and not important to my question so how can I repeat first three lines of code (as the main template is eval'd at the end of the script) using a function call?

Link to comment
Share on other sites

how can I repeat first three lines of code (as the main template is eval'd at the end of the script) using a function call?

 

If the $header, $body and $footer variables are needed within the main template you should pass them in as arguments to the getTemplate(0 method. Obviously your system is not designed to handle such things, but it would make your life alot easier.

 

I would also refrain from using eval as much as possible. I'll try and show a simple example.

 

tpl.class.php

<?php

  class template {
    public function getTemplate($file,$args=array()) {
      if (file_exists($file.'.php')) {
        ob_start();
        if (count($args)) {
          extract($args);
        }
        include $file.'.php';
        return ob_end_clean();
      }
      return false;
    }

    public function outputTemplate($out) {
      echo $out;
    }
  }

?>

 

main.php

<html>
  <head><title><?php echo $title; ?></title></head>
  <body>
    <p><?php echo $content; ?></p>
  </body>
</html>

 

index.php

<?php

  include 'tpl.class.php';

  $title = 'foo';
  $content = 'bar';
  $tpl = new template();
  if ($out = $tpl->getTemplate('main',array($title,$content))) {
    $tpl->outputTemplate($out);
  }

?>

 

I think Ive lost track of your question now, but what I'm getting at is if you pass the variables you need as arguments insstead of trying to have them fall into scope you will be able to wrap alot more of your code in functions, and therefor be able to wrap large pieces of repetative code together.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.