GetSimple Support Forum

Full Version: How to Convert Component to a Function?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I'm rather new to PHP and not sure how to do the following:

I want to include some code in my themes "functions.php" rather than add it as a component in GS. This code currently enables Zurb Foundation's "top bar" multi-level drop downs to work with GS.

Im not sure how to convert or whether its even possible to convert the code into a function that can be called in my themes template to render the menu correctly?

Currently the code below is is included in a component named "nav":

PHP Code:
<?php

$s 
= array('current''activepath''currentpath''open');
$r = array('active''active''active''has-dropdown');
$c str_replace($s$r$item->classes);
?>
  <li class="<?php echo $c?>"><?php if ($item->isOpen) { ?><a href="<?php echo htmlspecialchars($item->link); ?>"><?php echo htmlspecialchars($item->text); ?></a>
      <ul class="dropdown">
      <?php $item->outputChildren(); ?>
      </ul><?php } else { ?>
<a href="<?php echo htmlspecialchars($item->link); ?>"><?php echo htmlspecialchars($item->text); ?></a><?php ?></li><?php echo "\n"?>

and this component is then called in my template using:

PHP Code:
<?php get_i18n_navigation(get_page_slug(FALSE),0,2,I18N_SHOW_MENU'nav'); ?>

Can anybody guide me on whether this is possible, and how to do it correctly?

Thanks in advance

Boneye
Of course it is possible. In your functions.php file, simply wrap all of that code in a custom function & call it in your template. However, I don't know of any I18n parameter in that function to use custom markup (not a component), so you might need to use the return_i18n_menu_data function, like this:

PHP Code:
<?php
 
  
function build_i18n_nav($data$loop 0) {
    
$activeClass 'active';
    
$subMenuClass 'dropdown';
    
$output '<ul' . ($loop !== ' class="' $subMenuClass '"' '') . '>';
    for (
$i 0$i count($data); $i++) {
      
$item $data[$i];
      
$output .= '<li' . ($item['current'] ? ' class="' $activeClass '"' '' ) . '><a href="' htmlspecialchars(find_i18n_url($item['url'], $item['parent'])) . '">' $item['menu'] . '</a>';
      if (
$item['haschildren']) 
         
$output .= build_i18n_nav($item['children'], ++$loop);
      
$output .= '</li>';
    }
    
$output .= '</ul>';
    return 
$output;
  }
  
  function 
output_i18n_nav($url$minlevel$maxlevel$showtype) {
    
$menu_data return_i18n_menu_data($url$minlevel$maxlevel$showtype);
    echo 
build_i18n_nav($menu_data);
  }
?>

Then in your template, do eg.

PHP Code:
<div id="site-nav"><?php output_i18n_nav(return_page_slug(), 02I18N_SHOW_MENU?></div> 

For documentation on the return_i18n_menu_data function, refer to the website docs or this documentation page I built to provide extra-detailed info.
@Tyblitz

Thanks for your reply.

Originally the code I am using as a component. Originally it was posted in this thread http://get-simple.info/forums/showthread...9#pid36859, created to be used with Bootstrap, however I slightly adjusted the code so it worked with the Zurb Foundation Framework.

I already have this working by implimenting my code "as a component" in my theme. However, as I am creating a theme that I will use on several websites that will use Zurbs Foundation Framework, I would prefer to have this as a standard function in my themes "functions.php" file, so i dont have to keep making the same component everytime I creat a new website.

I tried your solution but I cant get it to work. Like I say, i'm a php newby with lots to learn... and im a bit slow! and I simply have not figured out how to create fuctions yet...

Regards
Boneye
Sorry, I didn't test the code and there were a few typos in the first codeblock; try again (re-copy paste), it should work now.
I also added the 'active' class to active item & 'dropdown' class to submenu's now. As for currentpath, it's the same as doing li.active a in CSS I guess. I don't know what 'activepath' is for.