//in the app controller
public function action_index()
{
$this->theme = \Theme::instance();
$this->theme->set_template( 'templates/default' );
$this->tpl = $this->theme->get_template( 'templates/default' );
$this->tpl->set( 'title', 'Test Title' );
$this->tpl->set( 'content', Request::forge( 'pages/pages/index', false )->execute() );
}
//in the module controller
public function action_index()
{
$this->theme = \Theme::instance();
$this->tpl = $this->theme->view( 'pages/test' );
$this->tpl->set( 'name', 'Brian Jeffries' );
}
//app controller
public function action_index()
{
$this->theme = \Theme::instance();
$this->theme->set_template( 'templates/default' );
$this->tpl = $this->theme->get_template( 'templates/default' );
$this->tpl->set( 'title', 'Test Title' );
Fuel::add_module( 'test' );
try
{
$widget = Request::forge( 'test/page/test' )->execute();
}
catch ( HttpNotFoundException $e )
{
$widget = 'Module Not Found';
}
$this->tpl->set( 'content', $widget );
}
//module controller
public function action_test()
{
return 'hello';
}
Notice! ErrorException [ Notice ]: Undefined index: content APPPATH/tmp/Smarty/templates_c/f6e533553ae37904936623049a99dca5a798682a.file.default.html.php @ line 35: 34: <body> 35: <?php echo $_smarty_tpl->tpl_vars['content']->value;?> 36: Notice! ErrorException [ Notice ]: Trying to get property of non-object APPPATH/tmp/Smarty/templates_c/f6e533553ae37904936623049a99dca5a798682a.file.default.html.php @ line 35: 34: <body> 35: <?php echo $_smarty_tpl->tpl_vars['content']->value;?>
<?php echo $content; ?>
$widget = Request::forge( 'test/page/test' )->execute(); $this->tpl->set( 'content', (string) $widget );
Now I have everything working nicely so far, including Smarty, and multiple modules per area in the template.
Here is what I ended up with:
//
//This is the app controller that calls modules into the main layout template.
//
class Controller_Page extends Controller
{
public function before()
{
// call the parent controller
parent::before();
}
/**
* The index page.
*
* @access public
* @return Response
*/
public function action_index()
{
//name the theme instance here so you can pass it into the after() method for rendering
$this->page_name = 'index';
$data[ 'title' ] = 'Test Page';
$data[ 'content_area' ][ 'module_1' ] = static::set_module( 'test/page/test' );
$data[ 'content_area' ][ 'module_2' ] = static::set_module( 'test/page/test2' );
//set main layout template here, since this is the app controller.
\Theme::instance( $this->page_name )->set_template( 'templates/default' )->set( $data );
}
public static function set_module( $uri )
{
try
{
return Request::forge( $uri, false )->execute();
}
catch ( HttpNotFoundException $e )
{
return 'Module Not Found';
}
}
/**
* After controller method has run, render the theme template
*
* @param Response $response
*/
public function after( $response )
{
// If nothing was returned render the defined template
if ( empty( $response ) )
{
$response = \Theme::instance( $this->page_name )->render();
}
return parent::after( $response );
}
}
//
//This is the module controller that sets its own template views, and variables. In this controller I create two separate views using the same template partial view.
//
namespace Test;
class Controller_Page extends \Controller
{
/**
* The home page.
*
* @access public
* @return Response
*/
public function before()
{
// call the parent controller
parent::before();
}
public function action_test()
{
$this->view_name = 'test';
$data['title'] = 'Test Page';
$data[ 'name' ] = 'Brian';
//set view partial here.
\Theme::instance( $this->view_name )->set_template( 'pages/test' )->set( $data );
}
public function action_test2()
{
$this->view_name = 'test2';
$data[ 'title' ] = 'Test Page 2';
$data[ 'name' ] = 'Brian Jeffries';
//set view partial here.
\Theme::instance( $this->view_name )->set_template( 'pages/test' )->set( $data );
}
/**
* After controller method has run, render the theme template
*
* @param Response $response
*/
public function after( $response )
{
// If nothing was returned render the defined template
if ( empty( $response ) )
{
$response = \Theme::instance( $this->view_name )->render();
}
return parent::after( $response );
}
}
// //This is the main template syntax. (default.html) // <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <title>{$title}</title> </head> <body> {foreach $content_area as $module} <br /> {$module} {/foreach} </body> </html>
//
//And this is the syntax in the partial view template (test.html)
//
{$name}
{$title}
It looks like you're new here. If you want to get involved, click one of these buttons!