ramiromd
ramiromd

Reputation: 2029

Codeigniter functions in Smarty

Im try use base_url() and site_url() from a Smarty template. I read, some articles, about how do this. But, none works. I create a "plugin" for Smarty called functions.url.php following this tutorial: https://github.com/EllisLab/CodeIgniter/wiki/Smarty-plugin---URL-Helper
So, i use the {url} Smarty "tag":

<form action={url type='site' url='authentication/login'} method="post" id="login_form">

But, whe i access to the site, smarty shows a large Fatal Error in the below line:

'SmartyCompilerException' with message 'Syntax Error in template

Any ideas ?.

Edit: New steps.

I changed the name of plugin for: plugin.url.php And i try register the plugin in the controller with:

$this->smartyci->registerPlugin("function", "url", "smarty_function_url");

But a new error show:

'SmartyException' with message 'Plugin not callable'

Upvotes: 0

Views: 1606

Answers (1)

ToxaBes
ToxaBes

Reputation: 1587

  1. Put Smarty to CI in some folder, for example, third_party/smarty.
  2. Add Smarty to CI - create library application/libraries/Mysmarty.php

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   = $config['smarty_template_dir'];                                                                        
        $this->compile_dir    = $config['smarty_compile_dir']; 
        $this->cache_dir      = $config['cache_dir'];   
        $this->caching        = $config['caching'];
    }

    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);
    }
} 
  1. Add new config variables to application/config/config.php

    $config['smarty_template_dir'] = APPPATH . 'views/'; // folder for your smarty templates $config['smarty_compile_dir'] = APPPATH . 'cache/smarty/compiled/'; // create this folder $config['cache_dir'] = APPPATH . 'cache/smarty/cached/'; // create this folder $config['caching'] = 0;

  2. Add new library to autoload in file application/config/autoload.php

    $autoload['libraries'] = array('database', 'session', 'mysmarty');

  3. Now in your controller try to add some variable to smarty:

    $this->mysmarty->assign('url', $this->config->item('base_url'));

and then show your template:

$this->mysmarty->view('main'); // template path is application/views/main.tpl

And in main.tpl add your form

<form action={$url} method="post" id="login_form">
...


enter code here

Upvotes: 1

Related Questions