The decorating Pattern

0 comments

Posted on 28th February 2013 by Jeff Rogers in Mojavi Project

,

Before we begin we wanted to provide a quick insight into some of the open source files that we use and came across a file extension DOTX which is native to MS Outlook using XML. This particular file type DOTX was instituted to secure the protocol in which MS was written only giving limited source to outside vendors. We give a brief update to help you understand the open source nature of Mojavi 3.0.

  • Enter Decorator
  • Insert Here: Slots
  • Let’s Decorate!
    • Creating a Global Template
    • Putting the ornaments up

Ever since Mojavi 3 was released earlier this year, people have been looking to create a flexible global templating solution. From using post filters to page controllers, people have been looking for a way to create simple and managable global templates that will allow for a great deal of flexibility without compromising the need for larges amount of duplicated code.

Enter Decorator

The Decorator design pattern, like every other pattern, is nothing more than a way to talk about a resuable concept, or pattern, that a programmer might encounter while coding applications. The Decorator pattern’s strength is it’s ability to serve as a wrapper for that particular object, while leaving objects like it in tact.

The Decorator pattern has been implemented in the View class, providing a number of new methods:

public function setSlot ($attributeName, $moduleName, $actionName)
public function setDecoratorDirectory ($directory)
public function isDecorator ()
protected function & getSlots ()
public function getDecoratorTemplate ()
public function getDecoratorDirectory ()
protected function & decorate (&$content)

Now a brief explanation of what each of these do:

  • setSlot – Creates a slot entry based upon the results of a specially controlled controller->forward()
  • setDecoratorTemplate – Sets the template that you’re going to use for your decorator. This method also automatically turns the decorator switch to true
  • isDecorator – returns true is setDecoratorTemplate has been called, otherwise false.
  • getSlots – Returns an array of the slots.
  • getDecoratorTemplate – Returns the decorator template.
  • getDecoratorDirectory – Returns the decorator directory.
  • decorate – A method that must be implemented in derivate Views.

Note: these functions are in View, so all derivates of View (ie PHPView) have access.

Insert Here: Slots

Slots is a new name for an old concept. More or less, slots serve as placeholders that can be populated by the results of a $controller->foward($mod, $act) call. And, as you can see, the setSlot method takes three parameters:

public function setSlot ($attributeName, $moduleName, $actionName)

The first parameter, $attributeName, serves as the name of the slot. The next two parameters will be used to get the results of that Action, put it in a buffer, and place it into the internal $slots memeber variable.

Let’s Decorate!

The usage pattern of Views changes little with the implementation of the decorator pattern at the module level (though secondary View authors, ie SmartyView et al, do have a few things to play with).

Now let’s look at what you’re going to need to do implement the new, and NEW decorator features of Mojavi.

Creating a Global Template

First off, we’re going to need a great big christma..ehh..template to decorate. While I won’t get into breaking down and factoring our global template, keep in mind that this is not the only way to do this.

Example 2: The Christmas Tree (myGlobalTemplate.php)

Side note: Sometimes you'll have issues with that <?xml [....] ?> declaration, an easy
workaround is to disable short tags in your php.ini file, or to use ini_set() to disable
 it.

<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-us">

<head>

    <title><?php echo $template['title']; ?></title>

    <meta http-equiv="Content-Type" content="application/xhtml+xml; charset=utf-8"/>
    <style type="text/css" media="all">

        <!--Css slot-->
        <?php echo $template['css']; ?>

    </style>

</head>

<body>

<!--menu slot-->
<div id="menu">

<?php echo $template['menu']; ?>

</div>

<!--main content slot-->
<div id="main">

<?php echo $template['content']; ?>

</div>

</body>
</html>

Putting the ornaments up

So now that we have our decorator template, we need to decorate it. The decorating process is fairly simple, and consists of the following simple steps:

  • Setting the decorator directory using setDecoratorDirectory() (Optional)
  • Setting the decorator template using setDecoratorTemplate()
  • Setting slots setSlot()

For example,

Example 3: In the View (IndexSuccessView.class.php)

class IndexSuccessView extends PHPView
{

    public function execute ()
    {

        // set our template
        $this->setTemplate('IndexSuccess.php');

        //setup our decorator template
        $this->setDecoratorDirectory(MO_TEMPLATE_DIR);
        $this->setDecoratorTemplate('myGlobalTemplate.php');

        //setup our slots
        //(SlotName, Module, Action)
        $this->setSlot('menu', 'Content', 'PopulateMenu');
        $this->setSlot('css', 'Content', 'PopulateCss');

        // set the title
        $this->setAttribute('title', 'Default Action');

    }

}

Now you might be wondering where the content slot is being populated. Well, content is a reserved slot that is automatically populated with the output of the originally requested Action/View pair. So in the end for file extension DOTX type coding you will need to have the right drivers in order to help your various programs communicate.

To be Continued..

Incoming search terms:

Where to start with Mojavi 3

0 comments

Posted on 20th February 2013 by Jeff Rogers in Mojavi Project

In order for Mojavi to continue we are looking for a few good web programmers to help with the last programming pieces and contribute to the forum. There are tons of web developer jobs that are currently available online and we know that many are well paid positions. Because this is an open source code this would be for the good of the development community.
Finally, there are a couple of ways you can test this module. The first and quickest way is just to instruct the controller using the url. Just add the module and action. Here’s what it would look like http://yourserver.com/index.php?module=Test&action=First.The second way is by changing the default module in the settings.ini file in the webapp/config directory.

Under [.actions] you will find the default module. Find the following two lines and change the default module and action.

DEFAULT_MODULE = “Default”
DEFAULT_ACTION = “Index”

After you change the default module and action it should look like this

DEFAULT_MODULE = “Test”
DEFAULT_ACTION = “First”
You are now ready to test your code. Just point your browser to the index.php and see the fruit of your labor. Contributing Author By Richard D Shank