tags:

views:

330

answers:

1

I have a component that has been happily building and rendering menus for a while now. Now I have to provide for a special case that shares all of the same logic, but requires a little bit of work in front of what already exists. What I'd like to do is create a new component action that will do the necessary preprocessing, punt to shared logic to complete the computational side and then render through the existing template partial (when all is said and done, it's still a menu like any other--it just take a little more work to build it).

Unfortunately, I can't find any way of doing this.

Here's the high level file/code breakdown that I have right now:

# 
# navigation/actions/components.class.php
# 
public function executeMenu() {
  /** 
   * This method runs most of the menus and does most of the work 
   * that's required of the special case.
   * 
   * Once complete, of course, it renders through navigation/templates/_menu.php
   */
}

public function executeSpecialMenu() {
  /** 
   * Do some preparatory work and delegate to executeMenu()
   * to finish up and render the menu. I'd like this action 
   * to render through the _menu.php partial as well.
   */
}

# 
# templates/layout.php
# 
<?php include_component( 'navigation', 'menu', array( 'menu' => 'Entity Type' ) ) ?>

/** SNIP */

<?php include_component( 'navigation', 'SpecialMenu' ) ?>

Any input would be much appreciated.

+2  A: 

A simple if non-optimal way would be to create the _SpecialMenu.php partial and just place an include inside it:

<?php include_partial('navigation/menu', array('menu' => 'Entity Type', 'other_var' => $other_var) ?>

Where each of your variables will need to be passed to the partial as in $other_var. Does this at least solve the problem?

Raise
That solves the problem at a functional level and it was the solution I put in place initially. To me, it feels really ham-fisted so I was hoping that there as a more surgical solution. I did opt for a straight `include()`, though, so that the variables would carry though and I wouldn't have to change 2 files every time a variable was add/removed.
Rob Wilkerson
Yes, `sfComponents` does not provide an easy way to do that, so you're stuck with your `include()` which is the easiest and more future-proof way to do it.
Geoffrey Bachelet