4

I have a function called load_template()

this function has two parameters

  • $name => the name of the template
  • $vars => array of key => value variables to be replaced in the template.

the way I want this to work is.

in the template ('test') I want to be able to write

<?php echo $title; ?>

then call

load_template('test', array('title' => 'My Title'));

and have it fill it out.

how can I do this?


Output buffering method. I have come up with the code below.
I am sure it can be improved.

public static function template($name, $vars = array()) {
  if (is_file(TEMPLATE_DIR . $name . '.php')) {
    ob_start();
    extract($vars);
    require(TEMPLATE_DIR . $name . '.php');
    $contents = ob_get_contents();
    ob_end_clean();
    return $contents;
  }
  throw new exception('Could not load template file \'' . $name . '\'');
  return false;
}
3
  • Can you show us what you have so far? Commented Jan 20, 2011 at 20:27
  • ob-way is okay if you need to return the contents. However, I see no use for this, save for some rare exceptional cases. Why not to let it go straight to the browser? Commented Jan 20, 2011 at 20:45
  • Whats wrong with standard includes? Are you templating the variable names? why is $title = "My Title"; include "test.templ" insufficient? Commented Jan 20, 2011 at 21:15

2 Answers 2

10
function load_template($name, $vars)
{
  extract($vars);
  include $name;
}

Wrap with ob_start and ob_get_clean if you want to capture the output in a variable.

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

Comments

2

Something like this?

function load_template($name, $vars)
{
  include('template/'.$name.'.tpl'); //.tpl, .inc, .php, whatever floats your boat
}

and in template/whatever.tpl you'd have:

...
<title><?php echo $vars['title'] ?></title>
...

...
<?php if (!empty($vars['content'])): //template still needs to know if the content is empty to display the div ?>
    <div id="content">
<?php echo $vars['content']; ?>
    </div>
<?php endif; ?>
...

Of course, that assumes the output being printed directly. You could have the tpl file print directly, or produce a string, or buffer the output from the tpl file and return it from load_template

3 Comments

not quite. I want to be able to return the loaded template as a string once it it processed. also, I would add in extract($vars) so that the variables do not have to be referenced from an array.
yea, extract was the way to go in this case. I'd completely forgotten about it actually, just make sure you handle the cases of $vars['vars'] and $vars['name'] properly.
Get rid of all that if (!empty crap. it's business logic job. A template should be always supplied with full set of variables

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.