Welcome Guest, Not a member yet? Register   Sign In
Really Simple Smarty 3 Library for Codeigniter
#11

[eluser]ramm[/eluser]
Hello

I'm having some problems updating the old implementation to this new one:
Code:
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

[eluser]Vheissu[/eluser]
[quote author="ramm" date="1300164783"]Hello

I'm having some problems updating the old implementation to this new one:
Code:
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.[/quote]

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

[eluser]Alexander84[/eluser]
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
Code:
<?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
Code:
<?php
class Example extends CI_Controller {
    function index()
    {
        $this->mysmarty->view('example');
    }
}

application/controllers/example.tpl
Code:
<!DOCTYPE html>
&lt;html&gt;
&lt;head&gt;
    &lt;title&gt;Example application - Current time&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
    <p>
        — What time is it?<br />
        — It's {$smarty.now|date_format:'%H:%M'} now!<br />
        — Thank you, Sir.
    </p>
&lt;/body&gt;
&lt;/html&gt;

That's all.
#14

[eluser]Vheissu[/eluser]
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

[eluser]Icehawg[/eluser]
[quote author="Alexander84" date="1299886705"]
Code:
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
[/quote]



Downloaded the repo today from github and still getting this error. Added an if(is_array($data)) to My_parser library.
Code:
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.

Code:
$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.
#16

[eluser]Alexander84[/eluser]
Icehawg, use the approach I've offered earlier this day as follows

addition to controller
Code:
$data['name']='Alexander';
$this->mysmarty->view('example', $data);

addition to template
Code:
— Thank you, {$name}.
#17

[eluser]Vheissu[/eluser]
Implementing a fix as we speak. Give me a couple of hours from this post to get to work and push the changes when I get a proper connection. Thanks!
#18

[eluser]Icehawg[/eluser]
[quote author="Alexander84" date="1300313172"]Icehawg, use the approach I've offered earlier this day as follows[/quote]

Thanks, Alexander84, but I prefer Vheissu's method.

[quote author="Vheissu" date="1300325797"]Implementing a fix as we speak. Give me a couple of hours from this post to get to work and push the changes when I get a proper connection. Thanks![/quote]

No problemo. I forked yours and added my "is_array" check to the $data variable. Not sure what you were working on, so feel free to ignore it. I also modified the fork a bit to make the template extension a variable instead of hardcoded to .tpl.
#19

[eluser]Vheissu[/eluser]
Thanks for your pull request Icehawg, I liked the way you implemented a solution for the array checking. I did write some code, but opted for yours because it was better than mine and looked cleaner.
#20

[eluser]Sazan[/eluser]
First of all I want say thanks for this library and for the job you've done.
I'm a little bit new to Smarty and while testing your lib got a problem with passing model results to the template. Please advice how this can be implemented.

I won't post whole model code as it is very simple and just returns result as object:

Test_model

Code:
function all ()
{
  return $this->db->get('table')->result();
}

Controller

Code:
if ($data = $this->test_model->all())
    $this->parser->parse('template.php', $data);

Template

Code:
{foreach $data as $item}
  {$item.id}
{foreachelse}
  Nothing found.
{/foreach}

And the issue is that I see only "Nothing found" everytime. But $data HAS records! (checked with print_r)

How can I achieve this?




Theme © iAndrew 2016 - Forum software by © MyBB