Jump to content
  • 0

What is the recommended way to use templates in the Admin Area when building a custom module?


Chrisfish

Question

3 answers to this question

Recommended Posts

  • 0

You can use smarty template files within the admin module interface:

function module_output ($vars) {
    $params = array ('key1' => 'value1', 'key2' => 'value2');

    $smarty = new Smarty;
    $smarty->assign('params', $params);
    $smarty->caching = false;
    $smarty->compile_dir = $GLOBALS['templates_compiledir'];
    $smarty->display(__DIR__ . '/template.tpl');
}

This code will use the file "template.tpl" and assign the array $params to the template.

Edited by string
Link to comment
Share on other sites

  • 0

Thanks for the feedback, you can indeed use Smarty in the main php file of the module. I want to use different templates for different pages though, and calling Smarty this way doesn't work in the Controller.

I currently worked around this issue this way:

function module_output($vars)
{
  // ...
  $smarty = new Smarty();
  $vars['smarty'] = $smarty;
  $dispatcher = new AdminDispatcher();
  $response = $dispatcher->dispatch($action, $vars);
  echo $response;
}

And then I use it in the controller this way:

class Controller {

    /**
     * An action
     */
    public function index($vars)
    {
		// ...
		$smarty = $vars['smarty'];
        $smarty->assign('version', $vars['version']);
        $smarty->assign('systemUrl', $url);
        $smarty->assign('addonURL', $url . '/admin/addonmodules.php?module=my_addon');
		// ...
        $smarty->caching = false;
        $smarty->compile_dir = $GLOBALS['templates_compiledir'];
        $smarty->display(dirname(__FILE__) . '/../../templates/index.tpl');
	}
}

 

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Answer this question...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use & Guidelines and understand your posts will initially be pre-moderated