0

I have a very simple template class that allows me to set some values and then include a template file. The class stores the variables, and the template file can access using $this->variable.

So my question is how should I go about doing this? Or perhaps it'd be better to just assign some variables and then include the template file - that way they don't need to be passed around?

2 Answers2

3

If you call `extract' like this:

extract( $this -> variables );

on top on your template code, you will be able to refer to $this -> variables[foo] as $foo.

moonwave99
  • 21,957
  • 3
  • 43
  • 64
0

One of the advantages of OO programming is that variables can be bound to a class and used within the context of a class.

So $this->title means: use the title variable bound to this class.

What you are basically asking is: how do I get back from OO programming to procedural programming. My answer would be: do not do that, try to stick with OO programming.

JvdBerg
  • 21,777
  • 8
  • 38
  • 55
  • The point is that's a true pain to write ` title ?>` just to echo a single value out in a template - that's why I'd consider using a templating engine like Twig in order to write just `{{ title }}`. – moonwave99 Sep 22 '12 at 12:13
  • With the short_open_tags on it would be `= $this->title ?>` for me, thats short enough. – JvdBerg Sep 22 '12 at 12:16