Welcome Guest, Not a member yet? Register   Sign In
Hard Searching a easy use of Twig in CI
#1

[eluser]altrano[/eluser]
Hello Comunnity.

i searched hours on the web about a solution, plugin, libraries or something else.

I Worked with symfony on a project earlier its used twig and i love it :-)

but i will learn a bit on other Frameworks actually i am strong interessting on CodeIgniter, but i will use Twig in CodIgniter (-:

anyone have a solution, example, plugin, lib or what ever for me??? Smile)

Thanks
#2

[eluser]wiredesignz[/eluser]
This was a plugin I have used with CI and Twig.

Installed as plugins/twig_pi.php however with CI 2.0 you may need to load it as a package from the third_party directory or even modify it to become a helper. It is not designed to be instantiated as a CI library.

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

include_once 'Twig/Autoloader.php';
Twig_Autoloader::register();

class mxView
{
    private $config, $twig;
    
    function __construct()
    {
        $this->config =& get_instance()->config;
        
        $reload = TRUE;
        $cache = $this->config->item('cache_path');
        $templates = FCPATH.$this->config->item('theme','settings').'views/';        
        
        $environment = array(
            'debug'               => FALSE,
            'trim_blocks'         => FALSE,
            'charset'             => $this->config->item('charset'),
            'base_template_class' => 'Twig_Template',
        );    
        
        $loader = new Twig_Loader_Filesystem($templates, $cache, $reload);
        $this->twig = new Twig_Environment($loader, $environment);    
    }
    
    function load($template, $data = array(), $return = FALSE)
    {        
        $view = $this->twig->loadTemplate($template);
        $data = array_merge($this->config->item('settings'), $data);
        return ($return) ? $view->render($data) : $view->display($data);        
    }
}

Usage:
Code:
$view = new mxView();
$data['widget'] = new mxWidget();
$this->output->final_output = $view->load('layout.tpl', $data, TRUE);
#3

[eluser]altrano[/eluser]
Code:
function load($template, $data = array(), $return = FALSE)
{        
    $view = $this->twig->loadTemplate($template);
    $data = array_merge($this->config->item('settings'), $data);
    return ($return) ? $view->render($data) : $view->display($data);        
}


what is $this->config->item('settings') ?? i can't find this values.. i use CI 2.0

and mxWidget??? dont exist!
#4

[eluser]altrano[/eluser]
Oh men this not works i become only a blank page.

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

include_once 'Twig/Autoloader.php';
Twig_Autoloader::register();
log_message('debug', "Twig Autoloader registered");

/**
* TODO
*
* @author altrano <[email protected]>
*/
class AltraTwig
{
    /**
     * @var array
     * @access private
     */
    private  $config;

    /**
     * Holds the Twig Instance
     *
     * @access private
     * @var object
     */
    private $_twig;

    /**
     * @access private
     * @var string
     */
    private $_template_dir;

    /**
     * @access private
     * @var string
     */
    private $_cache_dir;

    /**
     * Constructor
     *
     * @access public
     */
    public function __construct()
    {
        $this->config =& get_instance();
        
        $this->config->config->load('twig');

        $reload              = true;
        $this->_template_dir = $this->config->item('public_template_dir');
        $this->_cache_dir    = $this->config->item('public_cache_dir');

        $options = array(
            'debug'               => false,
            'trim_blocks'         => false,
            'charset'             => $this->config->item('charset'),
            'base_template_class' => 'Twig_Template,'
        );

        $loader = new Twig_Loader_Filesystem($this->_template_dir, $this->_cache_dir, $reload);

        $this->_twig = new Twig_Environment($loader, $options);
        log_message('debug', "Twig Environment Initiated");
    }

    public function load($template, $data = array(), $return = false)
    {
        $view = $this->_twig->loadTemplate($template);
        $data = array_merge($this->config->item('settings'), $data);
        
        return ($return) ? $view->render($data) : $view->display($data);
    }
}

My Public_Controller:
Code:
class Public_Controller extends MY_Controller
{
    /**
     * Contructor
     *
     * @access public
     */
    public function  __construct()
    {
        parent::__construct();
        $this->twig_view = new AltraTwig();
        $this->output->enable_profiler(false);
    }
}

welcome.php:
Code:
class Welcome extends Public_Controller
{
    /**
     * Contructor
     *
     * @access public
     */
    public function  __construct() {
        parent::__construct();
    }

    /**
     * index action
     *
     * @access public
     */
    public function index()
    {
        $data['test'] = 'Test & Test';

        $this->twig_view->load('welcome_message.html');
    }
}

what i make wrong??
#5

[eluser]drumbassrockroll[/eluser]
Try this...

http://github.com/MaherSaif/codeigniter-twig
#6

[eluser]altrano[/eluser]
[quote author="drumbassrockroll" date="1287785557"]Try this...

http://github.com/MaherSaif/codeigniter-twig[/quote]

ok thanks i will try it tomorow, hope this work... Smile
#7

[eluser]wiredesignz[/eluser]
You need to append the mxView library output to CI's output class. The config settings 'theme' specifies the location for your template files and 'settings' contains default data values to be displayed.

Code:
$config['settings'] = array(
    'base_url'     => $this->config['base_url'],
    'site_url'     => rtrim($this->config['base_url'].$this->config['index_page'], '/').'/',
    'charset'      => $this->config['charset'],
    'default_page' => 'dashboard',
    'theme'        => 'themes/default/',
    'doctype'      => array(
                   'xml_lang'     => $this->config['language_abbr'],
                   'lang'         => $this->config['language_abbr'],
                   ),        
    'site_title'   => 'Project Manager',
    'copyright'    => 'Copyright &copy; '.date('Y').' Group Inc.',
    'cookie_exp'   => 60*60*24*365,
    'ipl'          => '127.0.0.0',
    );

Maybe nothing will work unless you understand how CI works.
#8

[eluser]drumbassrockroll[/eluser]
The link to the library I provided is something that I've NOT tested with CI 2.x. The implementation is sound in 1.7x though.

2.0 is not ready for production, if you manage to get a robust 2.0 implementation with twig then please keep this thread updated in order for the community to write a solid library.
#9

[eluser]altrano[/eluser]
[quote author="drumbassrockroll" date="1287815644"]The link to the library I provided is something that I've NOT tested with CI 2.x. The implementation is sound in 1.7x though.

2.0 is not ready for production, if you manage to get a robust 2.0 implementation with twig then please keep this thread updated in order for the community to write a solid library.[/quote]

Yes this works fine for me in 1.7.2 in 2.0 i have not tested yet, first i make some games with Twig on 1.7.2 and later i will test it in 2.0.

I love this Smile Great thanks the only one i don't understand yet is why works twig->display() perfecty but twig->render() not but this is ok for now i work with display i think this is not significant wheter output the template directly or not, Or?

Ok now let's Play Smile




Theme © iAndrew 2016 - Forum software by © MyBB