views:

329

answers:

4

I'm using CodeIgniter, and will likely use their template library, as I want to keep things extremely simple and easy to use. The content for the template variables will come from the database, but I want the business admins to know what content areas are available (basically the names of the variables) when they choose a specific template. For instance, Joomla uses an extra XML file that defines each area, whereas Wordpress uses comments within a page template to inform the system that the PHP file is a template. I like the Joomla approach because you don't have to parse the PHP file to find the areas, but I like the Wordpress approach because you don't have an extra XML file associated with every template. Are there other approaches that I'm missing?

+1  A: 

You could do this directly in the controller:

// in the controller
print_r($data);
$this->load->view("main", $data);

Or a little more rudimentary, but you could pass to the template a PHP array of variables (or an object):

// in the controller
$data = array();
$data["namespace"] = array(
    "title" => "My website",
    "posts" => array("hi", "something else")
);
$this->load->view("main", $data);

And then in the view, have a flag to print_r the namespace to show all the vars available, so that business admins know exactly what to use.

// in the view
if(isset($namespace["showAllVars"])) print_r($namespace);
Luca Matteis
I think I would want them to know at the time they create a template in the admin, instead of having to use the view as a reference, but +1 for a good thought.
hal10001
A: 

One option would be to call token_get_all on the PHP file (only when your business admins are loading it up), and parse the output of that.

gms8994
A: 

The best approach, in my opinion, is to keep the variable definitions in another place (such as a database table, or a separate file). This will help with testing (i.e., a programmer can't just remove a tag and it's gone) and making sure things are still working as you move on with the application development in time.

Another advantage is that your application logic will be independent from the templating engine.

On a side note, if you expect a lot of traffic, you may consider using smarty instead. We have done extensive testing with most of the templating engines around and smarty is the fastest.

SorinV
Other than, I agree with you. I think storing template meta-data somewhere is appropriate, and in this case the database is likely the best option.
Toby Hede
+1  A: 

I think the nicest way would be to add a small hack to the template parser class. The code looks quite readable and clean in system/libraries/Parser.php. You could insert a hook in that class that can be used to keep track of the variables. I don't know, if it works, but here's a snippet:

class CI_Parser {
    var $varCallback;
    function setVarCallback($callbackFunction) {
        $this->varCallback = $callbackFunction;
    }
    ...
    function _parse_single(...) {
        $callback = $this->varCallback;
        $callback($key);
    }
...

//Somewhere in your code
function storeVarName($variableName) {
    // Persist the variable name wherever you want here
}
$this->parser->setVarCallback('storeVarName');
soulmerge
I like the approach. Since I will probably use the template engine, and it has to be called to parse the template, it makes sense that I would hook it to pull in all the template variables.
hal10001