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.

Really Simple Smarty 3 Library for Codeigniter

November 28, 2010 6:40am

Subscribe [11]
  • #1 / Nov 28, 2010 6:40am

    Vheissu

    278 posts

    I have written a super simple and hopefully somewhat helpful library for Codeigniter that lets you use Smarty 3 to render your views. It is compatible with the latest version of Codeigniter as all posts about Smarty integration into Codeigniter are about 3 years old mostly.

    I am sure that most other libraries probably still work, but this is something super simple that lets you render a view using Smarty and assign variables the usual Codeigniter view loading way.

    Read about it and get the code from my post: http://ilikekillnerds.com/2010/11/using-smarty-3-in-codeigniter-2-a-really-tiny-ci-library/

    I am planning on making it a parser library extension, but don’t really have the time. Feel free to critique my code and suggest perhaps even post improvements.

  • #2 / Dec 01, 2010 11:44am

    mohrt

    27 posts

    FYI your library is featured on the Smarty home page 😊 Some of you may be interested in Template Inheritance, a new feature of Smarty 3 that greatly improves the management of template/view files. See here for an explanation with an example.

  • #3 / Dec 01, 2010 6:52pm

    Vheissu

    278 posts

    FYI your library is featured on the Smarty home page 😊 Some of you may be interested in Template Inheritance, a new feature of Smarty 3 that greatly improves the management of template/view files. See here for an explanation with an example.

    Lol, wow. Didn’t expect that to happen. Gives me an incentive to actually make the library better and get off my butt. For anyone else’s reference, template inheritance and all other Smarty 3 specific features work as intended.

    I have included an example of how the library works as well as template inheritance for anyone interested in downloading 😊

    The project for which I wrote this library for is heavily using template inheritance as we speak and it works awesomely.

  • #4 / Dec 02, 2010 12:01am

    Vheissu

    278 posts

    An update:

    In light of being featured on the Smarty page I got off my butt and updated the library. The one thing I hate about libraries is having to use propriety library names, so now to load views utilising this Smarty library you load them like you would normally via $this->load->view(); and it now supports returning the template data or displaying it.

    Keep in mind that in the package I am using a MY_Loader to extend the view functionality, so if you are using Modular CI or Modular Extensions you might want to copy and paste the code into your file yourself or it could overwrite that functionality.

  • #5 / Dec 02, 2010 11:08pm

    mohrt

    27 posts

    Hmm, I wouldn’t recommend bundling Smarty with your lib in this case. Keep your lib slim, and inform the user how to grab the latest Smarty and install it for themselves. Then you don’t worry about falling behind in your bundled version, and the download is minimal.

  • #6 / Dec 02, 2010 11:10pm

    Vheissu

    278 posts

    Hmm, I wouldn’t recommend bundling Smarty with your lib in this case. Keep your lib slim, and inform the user how to grab the latest Smarty and install it for themselves. Then you don’t worry about falling behind in your bundled version, and the download is minimal.

    That initially crossed my mind, but from what I’ve seen it’s a step people seem to screw up so easily. Perhaps I’ll offer two versions, one with Smarty bundled and one without Smarty bundled.

  • #7 / Dec 15, 2010 8:30am

    Vizra

    1 posts

    What would you say to using

    $CI->smartyp->_assign_variables(array_merge($this->_ci_cached_vars, $vars));

    instead of

    $CI->smartyp->_assign_variables($vars);

    in core/MY_Loader.php file.

    That makes use of previously cached variables. And thanks for the library, made my life easier while migrating one of my applications to CI

  • #8 / Jan 30, 2011 10:53am

    tacman1123

    1 posts

    Thanks for putting this together, very useful.

    What is the best practice for making Smarty the template engine for all my websites?  Obviously I can copy over the MY_Loader.php and the other ones, and point the path for Smarty to some common directory, but is there a way to extend the CI Library WITHOUT changing the core?  Since 2.0.0 is just out, I’ll assume that there will be minor updates and I don’t want to have to re-integrate my changes every time a new release comes out.

    If not, I’ll just add your files to the files I copy over whenever I’m making a new project, but wondered if there was a cleaner way.

    Thx,

    Tac

  • #9 / Mar 11, 2011 1:38pm

    Alexander84

    5 posts

    Current Smarty3 CI adapter outputs numerous warnings and notices if no $data is specified as argument in controller (common scenario when you just want to parse a couple of templates). To be more specific:

    $this->parser->parse(“somepage.tpl”);

    outputs as follows before template processing

    A PHP Error was encountered
    Severity: Warning
    Message: Missing argument 2 for MY_Parser::parse(), called in somepage_controller.php on line 6 and defined
    Filename: libraries/MY_Parser.php
    Line Number: 22
    
    A PHP Error was encountered
    Severity: Notice
    Message: Undefined variable: data
    Filename: libraries/MY_Parser.php
    Line Number: 44
    
    A PHP Error was encountered
    Severity: Warning
    Message: array_merge() [function.array-merge]: Argument #1 is not an array
    Filename: libraries/MY_Parser.php
    Line Number: 44

    Could one be so kind and fix these gaps in adapter?

    P.S.
    Maybe library of Dwoo CI adapter (ci_dwoo_1_0_2.zip -> Dwootemplate.php) would inspire you
    as well as Seldaek’s comment in thread about ampersand removal near $CI = get_instance().

  • #10 / Mar 11, 2011 9:00pm

    Vheissu

    278 posts

    The issue has been fixed and pushed to the repo Alexander. It was a simple fix of adding $data = ‘’ to the parser function, must have forgot to add that part in because I was checking in the function if data has been supplied anyway.

  • #11 / Mar 14, 2011 8:53pm

    ramm

    53 posts

    Hello

    I’m having some problems updating the old implementation to this new one:

    Fatal error: Class 'CI_Base' not found in D:\Sites\site\application\libraries\MY_Parser.php on line 10

    I think i have all required files, and i don’t see if i have to change something else. Don’t know what’s wrong.

  • #12 / Mar 14, 2011 11:26pm

    Vheissu

    278 posts

    Hello

    I’m having some problems updating the old implementation to this new one:

    Fatal error: Class 'CI_Base' not found in D:\Sites\site\application\libraries\MY_Parser.php on line 10

    I think i have all required files, and i don’t see if i have to change something else. Don’t know what’s wrong.

    Try the latest code just pushed to Github. I introduced that bug by mistake, it should be fixed now.

  • #13 / Mar 16, 2011 7:04am

    Alexander84

    5 posts

    Frankly saying, I think your Smarty adapter is overbloated.

    Here is a light-weight approach step-by-step.

    1. Unpack latest Smarty to application/third_party/
    2. Drop Mysmarty.php to application/libraries/
    3 (optional, for beginners). Drop example.php to application/controllers/
    4 (optional, for beginners). Drop example.tpl to application/views/

    application/libraries/Mysmarty.php

    <?php if (!defined('BASEPATH')) exit('No direct script access allowed');
    define('SMARTY_DIR', APPPATH.'third_party/Smarty/');
    require_once(SMARTY_DIR.'Smarty.class.php');
    class Mysmarty extends Smarty
    {
        public function __construct ( )
        {
            parent::__construct();
            $config =& get_config( );            
            $this->template_dir   = APPPATH . 'views/';                                                                        
            $this->compile_dir    = APPPATH . 'cache/smarty/compiled/'; 
            $this->cache_dir      = APPPATH . 'cache/smarty/cached/'; 
            $this->caching = 2;
        }
        
        function view($resource_name, $params = array())   {
            if (strpos($resource_name, '.') === false) {
                $resource_name .= '.tpl';
            }
            
            if (is_array($params) && count($params)) {
                foreach ($params as $key => $value) {
                    $this->assign($key, $value);
                }
            }
            
            if (!is_file($this->template_dir . $resource_name)) {
                show_error("template: [$resource_name] cannot be found.");
            }
            
            return parent::display($resource_name);
        }
    }

    application/controllers/example.php

    <?php
    class Example extends CI_Controller {
        function index()
        {
            $this->mysmarty->view('example');
        }
    }

    application/controllers/example.tpl

    <!DOCTYPE html>
    <html>
    <head>
        <title>Example application - Current time</title>
    </head>
    <body>
        
            — What time is it?
    
            — It's {$smarty.now|date_format:'%H:%M'} now!
    
            — Thank you, Sir.
        
    </body>
    </html>

    That’s all.

  • #14 / Mar 16, 2011 8:25am

    Vheissu

    278 posts

    Alexander84,

    The code you posted above is not how the library I developed looks. I ditched using views a long time ago in favour of extending the parser and it’s not bloated at all. I also have support for Phil Sturgeon’s template library so you can use it with themes. I suggest you check out the Github code.

  • #15 / Mar 16, 2011 1:28pm

    Icehawg

    13 posts

    A PHP Error was encountered
    Severity: Warning
    Message: Missing argument 2 for MY_Parser::parse(), called in somepage_controller.php on line 6 and defined
    Filename: libraries/MY_Parser.php
    Line Number: 22

     

    Downloaded the repo today from github and still getting this error. Added an if(is_array($data)) to My_parser library.

    if(is_array($data))
    {
        $data = array_merge($data, $this->load->_ci_cached_vars);
    }

     

    However, with and without this code change, it does not seem to be pushing my loaded variables to the Smarty engine.

    $this->_data['testvar'] = 'blue';
    $this->parser->parse('default',$this->_data);
    
    A PHP Error was encountered
    
    Severity: Notice
    
    Message: Undefined variable: testvar
    
    Filename: sysplugins/smarty_internal_data.php
    
    Line Number: 291

    Would love to use Smarty but can’t seem to puzzle this out. Been scratching my head over this for a couple of hours now. Any thoughts. I can access the variable as $testvar if I ignore Smarty and I use the standard load->view.

    BTW - Using Smarty’s built in debug mechanism causes a crash of “not enough resources” when attempting to spit out the $this variable details.

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

ExpressionEngine News!

#eecms, #events, #releases