CodeIgniter Forums
Template library for CD3 - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: External Resources (https://forum.codeigniter.com/forumdisplay.php?fid=7)
+--- Forum: Addins (https://forum.codeigniter.com/forumdisplay.php?fid=13)
+--- Thread: Template library for CD3 (/showthread.php?tid=62872)



Template library for CD3 - Camo - 09-03-2015

Hello,
Can you help me to choose template library for CD3? Which one do you use? It should be well documented.


RE: Template library for CD3 - Camo - 09-04-2015

Meanwhile I wrote my own solution. 
But please can you tell me where I can find some libraries for CD3 ie. template lib? 

I am totally newbie in CD. I go according one very old tutorial and is not actual as you can surmise. 

This is my:


PHP Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');


/**
* Name: Template
*
* Version: 1.0.0
*
* Author: Vladimír Čamaj
[email protected]
* @camo
*
* Requirements: PHP5 or above
*
*/

class template
{
 
/**
 * @desc Array of keys witch will be converted to variables in templates
 * @var array
 */
 
protected $data = array('title' => 'Welcome to Codeigniter');


 
/**
 * @desc Instance of controller
 * @var Object
 */
 
protected $ci;



 
/**
 * @desc Sets the instance of controller in to the ci variable.
 */
 
public function __construct()
 {
 
$this->ci =& get_instance();
 }



 
/**
 * @param string $key
 * @param mixed $value
 */
 
public function set$key$value )
 {
 
$this->data[$key] = $value;
 }


 
/**
 * @param string $layout layout name
 * @param string $view view name
 * @param array $data data array for templates
 */
 
public function load$layout ''$view ''$data = array() )
 {
 
// Merging ensures taht the default data like title will be rewritten or stayed on default values.
 
$this->data array_merge$this->data$data );

 
// Sets the view variable in layout file.
 
$this->data['view'] = $this->ci->load->view$view$this->datatrue );

 
$this->ci->load->view$layout$this->data ); 
}






RE: Template library for CD3 - rtorralba - 09-04-2015

(09-04-2015, 11:38 AM)Camo Wrote: Meanwhile I wrote my own solution. 
But please can you tell me where I can find some libraries for CD3 ie. template lib? 

I am totally newbie in CD. I go according one very old tutorial and is not actual as you can surmise. 

This is my:



PHP Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');


/**
* Name: Template
*
* Version: 1.0.0
*
* Author: Vladimír Čamaj
[email protected]
* @camo
*
* Requirements: PHP5 or above
*
*/

class template
{
 
/**
 * @desc Array of keys witch will be converted to variables in templates
 * @var array
 */
 
protected $data = array('title' => 'Welcome to Codeigniter');


 
/**
 * @desc Instance of controller
 * @var Object
 */
 
protected $ci;



 
/**
 * @desc Sets the instance of controller in to the ci variable.
 */
 
public function __construct()
 {
 
$this->ci =& get_instance();
 }



 
/**
 * @param string $key
 * @param mixed $value
 */
 
public function set$key$value )
 {
 
$this->data[$key] = $value;
 }


 
/**
 * @param string $layout layout name
 * @param string $view view name
 * @param array $data data array for templates
 */
 
public function load$layout ''$view ''$data = array() )
 {
 
// Merging ensures taht the default data like title will be rewritten or stayed on default values.
 
$this->data array_merge$this->data$data );

 
// Sets the view variable in layout file.
 
$this->data['view'] = $this->ci->load->view$view$this->datatrue );

 
$this->ci->load->view$layout$this->data ); 
}




You can use plates php, I like it because has template properties like inheritance but don't have new syntax, is php


RE: Template library for CD3 - Camo - 09-04-2015

Thanks, but I need some examples how implement it to the CI. Syntax is no problem so it would be better to use something robust. But main is the documentation for CI.


RE: Template library for CD3 - rtorralba - 09-04-2015

(09-04-2015, 04:54 PM)Camo Wrote: Thanks, but I need some examples how implement it to the CI. Syntax is no problem so it would be better to use something robust. But main is the documentation for CI.

You simply can use it with composer:
  • You must to have installed composer https://getcomposer.org/doc/00-intro.md#installation-linux-unix-osx
  • Then go to the application folder and execute: 

    Code:
    composer require league/plates


  • Edit ci config.php and set TRUE $config['composer_autoload'].
  • At controller, I recommend at MY_Controller if you use this templates at all controllers, initialize the plates class:
PHP Code:
class MY_Controller extends CI_Controller
{
 
   protected $templates;

    function __construct()
    {
        parent::__construct();
 
       
        
// The parameter is the views path, in the example I put the default
 
       // codeigniter views path
        $this->templates = new League\Plates\Engine(APPPATH.'views');
    }

  • Finally call render method of plates:
PHP Code:
class Example_Controller extends MY_Controller
{
 
 public function index()
 
 {
 
   $data = array('key' => 'value');
 
   $this->templates->render('view_name'$data);
 
 }




RE: Template library for CD3 - Camo - 09-04-2015

I looks quite simple. How can I register My_Controller?


RE: Template library for CD3 - rtorralba - 09-04-2015

(09-04-2015, 05:27 PM)Camo Wrote: I looks quite simple. How can I register My_Controller?

Well, this is a basic functionality of codeigniter, you can declare master classes like MY_Controller, MY_Model, MY_Loader... and after extend from this classes to don't repeat code. You only must to put MY_Controller.php (for example) file at core folder and then extend any controller from MY_Controller instead CI_Controller.

In my example I declare protected $template attribute at MY_Controller class, then if i extend any controller from MY_Controller I cann to access to the attribute with $this->template (This is a basic OO feature)

http://www.codeigniter.com/user_guide/general/core_classes.html


RE: Template library for CD3 - Camo - 09-04-2015

Thank you that's what I was looking for. Also the CodeIgniter-Ion-Auth works very well Wink


RE: Template library for CD3 - rtorralba - 09-05-2015

(09-04-2015, 06:34 PM)Camo Wrote: Thank you that's what I was looking for. Also the CodeIgniter-Ion-Auth works very well Wink

You are welcome, just click on rate button at my post if I helped you.