News:

Want to get involved in developing SMF, then why not lend a hand on our github!

Main Menu

Code vs. Templates

Started by ptmuldoon, March 13, 2010, 03:38:09 PM

Previous topic - Next topic

ptmuldoon

I think I may have confused you, as I do have a template_browser function created in the template file.

Here's the Mod file

//RiskMain is called from index.php
function RiskMain() {
global $context, $settings, $options, $scripturl, $modSettings, $txt;

//Add any addtional css we need
$context['html_headers'] = ' <link rel="stylesheet" type="text/css" href="' . $settings["default_theme_url"] .  '/css/riskbrowser.css" />';


    // Load the general template for this action.
    loadTemplate('RiskBrowser');
   
    // Set the Default Subaction
    $_REQUEST['sa'] = (!empty($_REQUEST['sa']) && isset($subactions[$_REQUEST['sa']])) ? $_REQUEST['sa'] : 'browser';
   
    // Set the Default Template
    $context['sub_template'] = (!empty($_REQUEST['sa']) && isset($subactions[$_REQUEST['sa']])) ? $_REQUEST['sa'] : 'browser';
}

//The Default SubAction
function browser(){
global $context, $settings, $options, $scripturl, $modSettings, $txt;

$context['activegames'] = 'This Will be the Game Browser Code';

}


And the template file

//The Default Template
function template_browser() {
global $context, $settings, $options, $scripturl, $modSettings, $txt;

echo '
<div id="container">
<div id="r_menu">', template_rMenu() ,'</div>
<div id="r_menu">', template_ActiveGames() ,'</div>
<div id="footer">', template_rFooter() ,'</div>
<!-- Close Container  -->
</div>
';
}

//List all Active Games
function template_ActiveGames(){
global $context, $settings, $options, $scripturl, $modSettings, $txt;

echo '<pre>';
var_dump($context['activegames']);
echo '</pre>';

echo $context['activegames'];
}


If the default subaction is to 'browser.  I think it should be loading the default browser function in MyMod and the template_browser in the template.

Now, the template_browser loads.  However, should I be able use variables in the browser function as well in Mymod.php file?  Currenlty, I'm getting 'null' for the $context['activegames'] which is defined in the default 'browser' function when the page loads.

Arantor

But you're not actually *calling* anything in your function. You're setting a variable then it goes nowhere. You never actually pass control from the main function to the browser function.

ptmuldoon

Quote from: Arantor on March 21, 2010, 07:05:38 PM
You never actually pass control from the main function to the browser function.

That's were I keep getting myself confused.  How do you than pass control to the 'sub' functions?  I that was done via subaction $_REQUEST['sa'] = 'browser'.  Or is it not possibly to pass control at all to it?

Arantor

Putting it in $_REQUEST['sa'] doesn't do anything, you still have to call it.

Assuming $subactions[$_REQUEST['sa']] contains the name of the function to call, all you need to do is call_user_func($subactions[$_REQUEST['sa']]);

Advertisement: