Template


The Template plugin helps you creating your own functions.

 

Example 1:

Let's say you have a table for articles, and you want to make the new articles private until you feel they're ready for the public. Let's say want to be able to make it really easy to publish new articles, because you don't want to open the edit view just for this simple task. With the Template plugin, you could add a link into the table of the Article Overview. Take a look at the examples!

 

What you could do than is create a simple function in your controller called publish:

 

Class Article extends codexController{
 
    ...
 
    function publish(){
        if (!is_numeric($this->uri->segment(3))) 
            redirect('article');
         
        $this->load->model('article_model', 'articles');
        if ($this->articles->publish($this->uri->segment(3)))
            $this->codexmessages->add('success', 'The article you selected is published.');
        else  
            $this->codexmessages->add('failure', 'The article you selected is NOT published.');
        redirect('article');  
    }
}

 

Of course you need to create the corresponding model too, but that shouldn't be a problem.

 

Now you can add the template plugin to your yaml file:

publish:
    class: Template
    template: <a href="http://www.yourserver.com/backend.php/article/publish/{id}">Publish</a>

 

Codex will render the given template the substitute {id} with the value of the id value for that row. Of course if you use a primary key different different from 'id', then you'll need to put that between braces.

 

 

Example 2:

You could easily add another template to show a preview of the article:

preview:
    class: Template
    template: <a href="http://www.yourserver.com/{permalink}" target="_blank">Preview of {title}</a>

 

 

Example 3:

So far so good, but what if you want to make sure that you won't see the Publish text when the article is published? Not a problem, because you can create a callback function to get the template and even add the parameters you want to call the function with. Note that you can add any paramaters but you will most likely add values of the database record you're displaying, so the plugin will try to convert field names to actual values.

Add a function to your controller again:

Class Article extends codexController{
 
    ...
 
    function _get_publish_template($published){
        if ($published) return '';
        else return '<a href="http://www.yourserver.com/backend.php/article/publish/{id}">Publish</a>';
    }
}

end modify your yaml file to:

publish:
    class: Template
    func: _get_publish_template
    func_params:
        0: published

 

It's important to understand that _get_publish_template will be called with call_user_func_array if you define any parameters and therefore you will not be able to use the rest of your controller values and functions. To avoid this, either get the required values from $this->save_data inside your function or get the CI controller with CodeIgniter's get_instance() function.

 

 

Note that this plugin does not require a fieldname that corresponds to it in you database and it will not appear in your forms either.