• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
phptal & ci integration

#11
[eluser]MaD HamsteR[/eluser]
[updated] 24.11.11

Hi there.

I wrote a little wrapper to help you integrate and use this magnificent tempalte engine with CI.

Instructions

1. Download PHPTAL
2. Extract it to /system/application/libraries/phptal/ folder
3. Create file tal.php in /system/application/libraries/ folder
4. Put this code inside tal.php file

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

//Path to PHPTAL library, i used 1.2.0
include 'phptal/PHPTAL.php';


/**
* Wrapper for PHPTAL tempalte engine
*/

class Tal extends PHPTAL{


    function __construct()
    {

        //Call PHPTAL constructor (because we can)

        parent::__construct();


        /**
        * Use CI config to set encoding, templates and compiled templates path
        */

        $CI = &get;_instance(); //BUGGGGGG!!!! CI FORUM BUG, REMOVE the ; SYNTAX ERROR!


        /**
        * You can change paths if you need to
        */

        $cache_path = $CI->config->item('cache_path');

        if(empty($cache_path))
        {
            $cache_path = APPPATH.'cache/';
        }


        $this->setEncoding($CI->config->item('charset'));
        $this->setTemplateRepository(APPPATH.'views/');
        $this->setPhpCodeDestination($cache_path);


    }


    /**
    * @param string  (template name or path)
    * @param boolean (set TRUE to return page content)
    * @result mixed (depends on second parameter)
    *
    * This method returns or echoes parsed tempalte content
    */

    function display($tpl, $return=false)
    {

        $this->setTemplate($tpl);

        if($return){

            return $this->execute();

        }

        $this->echoExecute();

    }



}


?>

5. Set autoload "tal" library in autoload config file, or preload it manually in your script

Thats it.

Use like:

Code:
//Some controller

    function index()
    {


        $this->tal->title = 'Just another blog...';

        $this->tal->display('index.html');


    }

#12
[eluser]MaD HamsteR[/eluser]
In addition, i implemented translator class for PHPTAL to use in CI.

Instructions:

1. Create file translator.php in libraries/ folder
2. Put this code inside translator.php file

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

//Include PHPTAL translation interface
require_once 'phptal/PHPTAL/TranslationService.php';


class I18n implements PHPTAL_TranslationService{


    private $_currentLang   = NULL;
    private $_currentDomain = array();
    private $_context       = array();
    private $CI             = NULL;


    /**
    * @param string (name of the language)
    * @return string (language you've just set)
    *
    * This method sets translation language.
    * Name of the language is a dir name where you keep your translation files
    */


    public function setLanguage($language='english')
    {

        $this->CI = &get;_instance();  //BUGGGGGG!!!! CI FORUM BUG, REMOVE the ; SYNTAX ERROR!

        $this->_currentLang = $language;

        return $this->_currentLang;

    }



    /**
    * @param string (translation file name)
    * @return void
    *
    * You can separate translations in several files, and use only when needed.
    * Use this method to specify witch translation file you want to
    * use for current controller.
    */


    public function useDomain($domain)
    {

        $file = APPPATH."/language/{$this->_currentLang}/{$domain}.php";

        $lang = include($file);

        $this->_currentDomain = array_merge($this->_currentDomain, $lang);

        $this->CI->tal->setTranslator($this);

    }


    /**
    * Set an interpolation var.
    *
    * Replace all ${key}s with values in translated strings.
    */

    public function setVar($key, $value){

        $this->_context[$key] = $value;

    }

    /**
    * Translate a text and interpolate variables.
    */

    public function translate($key, $htmlescape=true)
    {

        $value = $this->_currentDomain[$key];


        while (preg_match('/\${(.*?)\}/sm', $value, $m)){

            list($src,$var) = $m;

            if (!array_key_exists($var, $this->_context)){

                $err = sprintf('Interpolation error, var "%s" not set', $var);

                throw new Exception($err);
            }


            $value = str_replace($src, $this->_context[$var], $value);

        }


        return $value;


    }


    /**
    * Not implemented yet, default encoding is used
    */

    public function setEncoding($encoding)
    {


    }


}

3. Set autoload “translator” library in autoload config file AFTER tal library, or preload it manually in your script

Use like

Code:
//Some controller

    function index()
    {


        $this->translator->setLanguage('russian');

        $this->translator->useDomain('common'); //Commnon UI elemnts, buttons, links etc.
        $this->translator->useDomain('register'); //Specific page elements

        $this->tal->display('register.html'); //I LOVE Macro Extension for TAL!


    }


I assume that you have common.php and register.php files inside

/system/application/language/russian(whatever)/ folder


Language file should look like this:

Code:
//Keys which will be replaced with values in templates like i18n:translate="string:menu"

return array(

    'menu'     => 'Навигация',
    'index'    => 'Главная',
    'rules'    => 'Участие',
    'photos'   => 'Фото',
    'forum'    => 'Форум',
    'about'    => 'О проекте',
    'contacts' => 'Контакты',

);

Duplicate keys will be overwritten!


Thanx and DO report any bugs!

#13
[eluser]luke holder[/eluser]
do these methods get METAL in PHPTAL?


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.