0

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>
1
  • Sounds like you're practically reinventing the Fry templating system: fry.sourceforge.net Commented Jun 9, 2009 at 1:28

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

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?
How are your views being called or referenced?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.