ExpressionEngine CMS
Open, Free, Amazing

Thread

This is an archived forum and the content is probably no longer relevant, but is provided here for posterity.

The active forums are here.

Hard Searching a easy use of Twig in CI

October 19, 2010 3:58pm

Subscribe [3]
  • #1 / Oct 19, 2010 3:58pm

    altrano

    20 posts

    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??? 😊)

    Thanks

  • #2 / Oct 20, 2010 12:35am

    wiredesignz

    2882 posts

    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.

    <?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:

    $view = new mxView();
    $data['widget'] = new mxWidget();
    $this->output->final_output = $view->load('layout.tpl', $data, TRUE);
  • #3 / Oct 20, 2010 5:39pm

    altrano

    20 posts

    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 / Oct 20, 2010 6:16pm

    altrano

    20 posts

    Oh men this not works i become only a blank page.

    my class:

    <?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:

    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:

    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 / Oct 22, 2010 2:12pm

  • #6 / Oct 22, 2010 8:48pm

    altrano

    20 posts

    Try this…

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

    ok thanks i will try it tomorow, hope this work… 😊

  • #7 / Oct 22, 2010 9:15pm

    wiredesignz

    2882 posts

    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.

    $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 © '.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 / Oct 22, 2010 10:34pm

    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 / Oct 23, 2010 11:28am

    altrano

    20 posts

    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.

    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 😊 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 😊

.(JavaScript must be enabled to view this email address)

ExpressionEngine News!

#eecms, #events, #releases