views:

72

answers:

1

Hi,

i have two PHP files:

  • template.php
  • template.html.php

The first is the class definition for Template. The second contains an actual HTML-based template, however with PHP constructs (hence the .PHP extension). I'd call this a hybrid html/php file.

Is it possible to create some function in the Template-class (special_include_parse()) which takes a:

  • $path (to a hybrid html/php file)
  • $model (which is passed to the code in the hybrid html/php file so that it can be referenced using $this->getModel() or $model or whatever...)

?


template.php

class Template {
    function Parse($model) {
        //include('/var/www/template.html.php');
        //fopen('/var/www/template.html.php');
        $return = special_include_parse('/var/www/template.html.php', $model);
    }
}

template.html.php

<html>
    <head>
        <title><? echo $this->getModel()->getTitle(); ?></title>
    </head>
</html>
+2  A: 

Um... why not just set $this (although I wouldn't call it that) and include/require the template.html.php considering it's basically PHP syntax? Basically:

class Template {
  function Parse($model) {
    ob_start();
    require '/var/www/template.html.php'; // I wouldn't use absolute paths
    $return = ob_get_clean();
  }
}

Personally though I think this is a prime example of making something more difficult than it is by introducing an unnecessary (in fact, counterproductive) object abstraction to something that's otherwise pretty straightforward:

$model = new MyModel(...);
require 'template.html.php';

and

<html>...
  <h3><?= $model->getStuff(); ?></h3>

I'm not sure why you're overcomplicating it or rather what you're trying to achieve.

cletus
I have View classes (.php) which can have a linked template file (.html.php). At the end of a script getHtml() is called on the View class. At this point a Model should have been set on the View class. The Viewclass somehow needs to pass that model it's template, so the template can be parsed with values from the model. I think the output buffering is actually what I'm looking for. Do you think there is another, more efficient way of achieving what I want?
Ropstah
How are your views being called or referenced?
cletus
Very nice answer. Problem solved completely :)
Ropstah