Welcome Guest, Not a member yet? Register   Sign In
Template library for CD3
#1

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

(This post was last modified: 09-04-2015, 11:40 AM by Camo.)

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 ); 
}



Reply
#3

(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
Greetings.
Reply
#4

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.
Reply
#5

(This post was last modified: 09-04-2015, 05:19 PM by rtorralba.)

(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#...x-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);
 
 }

Greetings.
Reply
#6

(This post was last modified: 09-04-2015, 05:27 PM by Camo.)

I looks quite simple. How can I register My_Controller?
Reply
#7

(This post was last modified: 09-04-2015, 05:54 PM by rtorralba.)

(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/ge...asses.html
Greetings.
Reply
#8

(This post was last modified: 09-04-2015, 06:35 PM by Camo.)

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

(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. 
Greetings.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB