Site Administration Page 6 - Building a Barebones Content Management System: The Yaapi API |
One final bit before I drawn the curtains on this part of the series: I would like to update my application to display a list of categories in the left hand side menu, which was thus far populated with a bunch of static links. This is what I want it to look like:
Fortunately, yaapi comes to my rescue once again. This time it is the turn of the get_categories() method of the article() class that comes in handy. Since I need to display this menu on every page, it is only logical that I encapsulate the code that lists the categories in a separate function that can be invoked along the lines of my custom number_of_elements() function, listed earlier. This is what my custom function, titled render_lhs_menu() and defined in the "inc.config.php" file, looks like: <?php // snip // function to display the left hand side menu $return_value = ""; for ($count = 0; $count < count($categories); $count++) { $return_value .= "<P><A HREF=\"category.php?id=".$categories[$count]['id']."\" >".$categories[$count]['name']."</A></P>"; return $return_value; // snip ?> There is no rocket science here. The function accepts an instance of the yaapi article() object as an input parameter and retrieves all the categories by calling the get_categories() method. As you may have guessed, this function returns an associative array containing the id and name of each category in the database. It concatenates the required HTML output as it iterates over the array and returns a string value to the calling script (say "article.php"), which in turn echoes it to the browser, as you can seen below. <% // snip <TR HEIGHT="350"> </TD> // snip %> Finally, I have managed to make all components of my web page dynamic -- yes, I have excluded the footer section as it remains constant across most websites. You may argue that I could have defined another function to return the content for this footer section. Good point; I leave that as an exercise for you. Conclusion This brings me to the conclusion of the second part of the "Building a Barebones Content Management System" tutorial. Let me quickly recap the topics that I covered today. Initially, I gave a detailed introduction to the different entities that drive yaapi. Next, I demonstrated how to retrieve a list of articles in a particular category as well as display the content of a selected article using the get_categories() and get_article() methods respectively. After that, I developed a custom function that returns the HTML code required to display the categories defined in yaapi. Finally, I have an application that fetches data from the database and renders it in a logical manner, as one expects from any program worthy of being called a CMS application. In the next part, I shall introduce the patTemplate template engine. After demonstrating a few examples that show the versatility of this software API, I will integrate it with the PHP scripts listed today, thereby bringing me closer to fulfilling my goal of developing my barebones CMS. Till then, enjoy!
blog comments powered by Disqus |
|
|
|
|
|
|
|