InkType Libraries
NOTE: These libraries are not necessary for basic InkType functionality. The libraries provide a way to customize the way information is retrieved from the database. For example, maybe you have some space reserved on your sidebar to display the titles of the last three posts. In this case, you could use lib_posts to do that. All of the basic functions required to run InkType (displaying blog posts, searching the blog, etc) are available to you without having to use libraries. For the basic data retrieval functionality, see the Template System documentation.
The InkType libraries provide a way for the views (templates) to communicate with the model (data layer). The main purpose of these libraries is to ease the template development process. The controller automatically provides any available parameters to the view but does not pass any data from the database. The data should be retrieved by the view from the model through these libraries. The libraries are responsible for pulling the data from the database and various helper functions are used to format and display the information.
- Displaying Posts: lib_posts
- Displaying Pages: lib_pages
- Displaying Categories: lib_categories
- Comments and the Comment Form: lib_comments
Displaying Posts: lib_posts
This library enables blog posts to be displayed in the views.
| Function | Parameters | Description |
|---|---|---|
| posts($parameters) | post_id, url_title, url_category, search | Retrieves posts to display; $parameters is an array |
| prep_categories($posts) | Not to be used from a view | Not to be used from a view |
This will produce a loop of all published blog posts - no parameters
<?php foreach ($this->lib_posts->posts() as $post) { ?>
<h2><?php echo inktype_post_title($post); ?></h2>
<?php echo inktype_post_content($post); ?>
<p><?php echo inktype_post_meta($post, $this->lib_settings->allow_comments()); ?></p>
<?php } ?>
This will produce a loop of published blog posts based on the current category
<?php foreach ($this->lib_posts->posts(array('url_category'=>$this->url_category)) as $post) { ?>
<h2><?php echo inktype_post_title($post); ?></h2>
<?php echo inktype_post_content($post); ?>
<p><?php echo inktype_post_meta($post, $this->lib_settings->allow_comments()); ?></p>
<?php } ?>
This will produce a loop of published blog posts based on a search query
<h3>Search Results</h3>
<?php foreach ($this->lib_posts->posts(array("search"=>$this->session->userdata("search_query"))) as $post { ?>
<h2><?php inktype_post_title($post); ?></h2>
<?php inktype_post_content($post);?>
<div class="commentsbox"><?=inktype_post_meta($post, $this->lib_settings->allow_comments());?></div>
<?}?>
As you can see, it's rather easy to pull the posts into the view by using the library. The foreach() loop calls a library function to retrieve the data while the inktype_ functions inside the loop formats and displays the information that the library function retrieved.
This will produce a loop if there are posts to pull; otherwise it will display a "No results found" message
<h3>Search Results</h3>
<?php if ($posts = $this->lib_posts->posts(array("search"=>$this->session->userdata("search_query"))) {
<?php foreach ($posts as $post) { ?>
<h2><?php echo inktype_post_title($post); ?></h2>
<?php echo inktype_post_content($post); ?>
<div class="commentsbox"><?php echo inktype_post_meta($post, $this->lib_settings->allow_comments()); ?></div>
<?php } ?>
<?php } else { ?>
<h2>No results found</h2>
<?php } ?>
This time we assigned the results to $posts and looped through $posts using foreach(). It's just a little more PHP in your template but it allows us to handle the behavior of the output if no records exist.
Displaying Pages: lib_pages
This library is responsible for passing static page data from the model to the view.
| Function | Parameters | Description |
|---|---|---|
| pages($parameters) | url_title | Retrieves pages to display; $parameters is an array |
This will create a list of links to all the published pages in your blog
<? foreach ($this->lib_pages->pages() as $page) {?>
<h2><?=inktype_page_link($page);?></h2>
<?}?>
This will grab the page content for the current page based on the $url_title
<? foreach ($this->lib_pages->pages(array("url_category"=>$this->url_category)) as $page) {?>
<h2><?=inktype_page_content($page);?></h2>
<?}?>
Even though you are only retrieving one page, it still needs to be retrieved through a loop.