F
F
Fedcomp2011-09-17 18:28:15
Kohana
Fedcomp, 2011-09-17 18:28:15

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>

Further, all controllers that display any pages of the site are inherited from it
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>

Problems arise when trying to implement hmvc as I understand it, i.e.:
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>

The problem arises when it is necessary to get a list of styles / scripts, meta, etc. from the "sub-controller".
What am I doing wrong?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
W
WebSpider, 2011-09-17
@Fedcomp

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

A
aktuba, 2011-09-18
@aktuba

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.

W
WebSpider, 2011-09-18
@WebSpider

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 question

Ask a Question

731 491 924 answers to any question