Answer the question
In order to leave comments, you need to log in
What is the best way to implement templates in kohana 3.1?
Hi hubr!
I am learning the Kohana 3.1 framework. There was a question as it is better to implement templates through Controller_template.
Read more:
There is a basic controller for templates
class Controller_Design extends Controller_Template {<br><br>
public $template = 'design';<br>
public $auto_render = TRUE;<br><br>
protected $current_page;<br><br>
public function before()<br>
{<br>
parent::before();<br><br>
// Если запрос внутренний или ajax, то не выводим шаблон<br>
if( ! $this->request->is_initial() or $this->request->is_ajax())<br>
{<br>
$this->auto_render = FALSE;<br>
}<br><br>
$this->template->title = '';<br>
$this->template->meta_keywords = '';<br>
...<br>
}<br><br>
public function after()<br>
{<br>
if($this->auto_render)<br>
{<br>
// Default scripts and css's<br>
$styles = array(<br>
'public/skin/style.css' => 'screen',<br>
...<br>
} else {<br>
$this->response->body( $this->template->content );<br>
}<br><br>
parent::after();<br>
}<br>
}<br>
class Controller_Profile extends Controller_Design {<br><br>
protected $current_page = 'profile';<br><br>
public function action_index()<br>
{<br>
$this->template->styles['public/skin/css/profile.css'] = 'screen';<br>
$this->template->content = View::factory('pages/profile');<br>
}<br><br>
}<br>
class Controller_Main extends Controller_Design {<br>
protected $current_page = 'main';<br>
public function action_index()<br>
{<br>
if( ! Auth::instance()->logged_in())<br>
{ // If user not logged, then show the page for guests<br>
$this->template->content = View::factory('pages/main');<br>
}<br>
else<br>
{ // Else show page, like url is /profile<br>
$this->template->content = Controller::factory('profile')->execute();<br>
}<br>
}<br><br>
}<br>
Answer the question
In order to leave comments, you need to log in
Probably still meant Request::factory('profile')
What does it mean to "receive a list of styles from the sub-controller"? You make a normal subquery, it returns a rendered page (or part). Similarly, you can make an external request, such as Request::factory('http://yandex.ru')
. Please clarify your question
What a stupid way to output styles and scripts through controllers ... If you change the template, will you climb through the entire code? And for "$this->template->styles['public/skin/css/profile.css'] = 'screen';" on the hands must be beaten with a bat! Either don't use (H)MVC at all, or follow the rules. V (iew) is fully responsible for the appearance.
2aktuba: I also consider it wrong to set parts of the display logic in the controller, which is why I advised you to use template engines like Twig above. But nevertheless, these links are present in the official documentation of Kohana:
http://kerkness.ca/kowiki/doku.php?id=template-site:basic_page_controller
http://kerkness.ca/kowiki/doku.php?id= template-site:extending_the_template_controller
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question