Documentation
Working with Addons/Modules/Views
Some examples of addon usage
Let's assume we have the following files:
/var/www/application/addons/articles/lib/articles.addon.php
/var/www/application/addons/articles/modules/db.module.php
/var/www/application/addons/articles/view/show_article.view.php
In articles.addon.php, we could have:
...
public function action_show_articles() {
// Get article id from url
$id = $this->lib->url->params('id_user');
// check id ...
// Load database module for aticles
$mdb = $this->load_module('db');
// Get article data
$article_row = $mdb->load_article($id);
// Load view to show article
$view = $this->load_view('show_article');
// Set article values
$view->subject = $article_row['subject']
$view->content = $article_row['content'];
// Render and show article view
echo $view->run();
}
...
In show_article.view.php (assuming html5):
...
<article id="main">
<header><h1>{.subject}</h1></header>
<p>{.content}</p>
</article>
...
This snippet could also be written like this:
...
<article id="main">
<header><h1><?=subject;?></h1></header>
<p><?=content;?></p>
</article>
...
Since a view never directly outputs anything but rather returns the a buffer with its contents after rendering, we are not limited to producing web pages. Actually, anytime we need to produce some text according to some template, using a view is appropriate.
For example, we can use views as email templates in some application.
Assuming we have a view file containing:
<p>
Welcome to {.website_name} {.user}!<br />
</p>
and located at:
.../addons/users/views/email_templates/welcome.view.php
then we could generate the contents of an email message as follows:
public function send_welcome_mail($username) {
$view = $this->load_view(
'email_templates/welcome.view.php',
array('website_name' => 'My Portal', 'user' => $username);
);
$email_message = $view->run();
/* Send email ... */
}