Skip to content

Form helper with Smarty

Derek Jones edited this page Jul 5, 2012 · 8 revisions

Category:Help::TipsAndTricks

If you use Smarty as your template engine in CodeIgniter you may find this useful for generating form tags.

Usage:

Regular form

{form url='module/function'}

    <input type="submit" />

{form}

Upload Form

{form url='module/function' type='upload'}

    <input type="submit" />

{form}

Note:

if you use {form} without the url parameter this will close the form.

Installation Create a file "function.form.php" in Smarty-x.y.z/libs/plugins where x.y.z are the version number of Smarty.

<?php
Author: Svetoslav Marinov; svetoslavm [] gmail.com
Inspired by: http://codeigniter.com/wiki/Use_URL_helper_from_Smarty/
*/

function smarty_function_form($params, &$smarty)
{
    //check if the needed function exists
    //otherwise try to load it
    if (!function_exists('form_open')) {
        //return error message in case we can't get CI instance
        if (!function_exists('get_instance')) return "Can't get CI instance";
        $CI =& get_instance();
        $CI->load->helper('form');
    }
    
    if (isset($params['url']) && $params['type'] == 'upload') {
        return form_open_multipart($params['url']);
    } elseif (isset($params['url'])) {
        return form_open($params['url']);
    } else {
        return form_close();
    }
}
?>
Clone this wiki locally