Recess Framework

I’ve been playing with the Recess! PHP Framework recently and I like it. Even though it has very little documentation so far, I find the learning curve much lower then other popular frameworks. I think once the dox and examples build up, it will be very easy to learn and implement this framework in production.

There is a great tool included to help build your first application, and that tool creates a lot of scaffolding code that you can build on. The first thing I wanted to do with the generated code was create a submenu for navigation that would be in every view associated with the controller. I’m assuming the default scaffolding has been used to do the initial setup of your app.

The default scaffolding creates a menu that looks something like this:

The root navigation menu is fine. But the regular anchor tags need some sprucing up. Also those submenu navigation items disappear once you move off the index view of your controller. So we want to do two things:

  1. Make our WidgetController submenu navigation items persist through every view in the controller
  2. Make the links look a little nicer

So what we do first is take a look at our Widget index view and see what it’s doing.

<?php      // views/widget/index.html.php

Layout::extend('layouts/widget');     /* Use views/layouts/widget.layout.php to as our layout code for this view
$title = 'Widget Index';
?>

/* Here are the anchor tags that we saw in the screenshot above */
<h3><?php echo Html::anchor(Url::action('WidgetController::newForm'), 'Create New Widget') ?></h3>
<h3><?php echo Html::anchor(Url::action('WidgetController::pushActive'), 'Schedule Production') ?></h3>


Pretty straight forward. This view extends the layouts/widget file, which is literally “{yourApp}/views/layouts/widget.layout.php”. It also manually adds those anchor links(in bold) in the index view. First I want to remove those anchor links, since they only affect the index view and won’t help us elsewhere. In order to have our submenu items show in every view, we should modify the widget layout, and make sure we use that layout for every view in the widget controller. Now let’s look at the Widget layout:
<?php       // views/layouts/widget.layout.php

Layout::extend('layouts/master');        /* Inherit views/layouts/master.layout.php settings
Layout::input($title, 'string');
Layout::input($body, 'Block');

$title .= 'Coupon - ';

/* The layouts/master will display the contents of $navigation in the navigation DIV. So we should
fill this variable with all our navigation information */
$navigation = Part::block('parts/navigation');
?>

Here we see that the Widget layout is getting its navigation part from the root navigation file. That navigation file is used by all our controllers, so we shouldn’t modify it just for our Widget Controller. Instead, we’ll create our own Widget Navigation part that will also show those root navigation options. Change the bold line to read
$navigation = Part::block('parts/widget-navigation');

And then create the widget-navigation.part.php file
<?php         // views/parts/widget-navigation.part.php

echo Part::block('parts/navigation'); ?>
<ul class="subnavigation">
<li><?php echo Html::anchor(Url::action('WidgetController::index'), 'Create New Widget') ?></li>
<li><?php echo Html::anchor(Url::action('WidgetController::index'), 'Schedule Production') ?></li>

</ul>

This Navigation part includes the root navigation items, and then adds our own sub navigation items. Because we edited the widget layout to use this part we will have a nice Widget navigation sub menu.

You may also notice I put the Widget specific items in the “subnavigation” CSS class. Thats not a default class, so we must create it. Edit the style part file, {yourApp}/views/parts/style.part.php and put in this class

.subnavigation {font-size:12}

After we’ve changed these few lines, we get a submenu navigation bar that shows in all the controller’s views, and looks a little more professional.


This was a pretty simple example to introduce you into how a view is formed, and how to modify that view. In order to get this last screenshot all we had to do was:

  1. Create a widget-navigation.part.php file that included the original navigation part, and then added our submenu items.
  2. Tell our widget.layout.php file to use the new parts/widget-navigation.
  3. Add some CSS to make it look a little nicer

If you follow those steps for each controller, your main navigation items will be application wide, and your submenu items will change to fit the controller the user is viewing. This is a very easy procedure to create a professional looking application.

Posted Wednesday, September 16th, 2009 under recess framework.

5 comments

  1. Ryan – Great tutorial on modifying the default scaffolding that Recess generates for you! I'm going to link to this from the Recess blog soon.

    If you wanted to take it another step further you could create a part for each nav item that looked like this (mind the formatting, the comments here don't accept PHP/HTML):

    // PHP parts/subnav
    Part::input($title,'string');
    Part::input($action,'string');
    // /PHP
    // LI
    // PHP
    // echo Html::anchor(Url::action($action), $title);
    // /PHP
    // /LI

    You could then, from your widget-navigation part, just call:

    Part::draw('parts/subnav', 'Create new Widget', 'WidgetController::index');

    Not a super big savings just wanted to demo that parts can have inputs and parts can call other parts.

    Keep up the great work!

  2. Thanks for the comment! I have every intention of putting all this into The Book of Recess as well. I am trying to find a docbook program that isn't smarter then I am(so far I'm losing). I've tried Vex(i think its called) and I seem to be doing ok.

  3. Other variant is possible also

  4. HOI. Ryan,

    Good to read that there are people who share the same experience.

    I totally agree with your impression of Recess and other frameworks.


    I’ve been playing with the Recess! PHP Framework recently and I like it. Even though it has very little documentation so far, I find the learning curve much lower then other popular frameworks.

    Have a nice day!
    Stinie

  5. I stumbled upon your blog by accident. Pretty informative post.