Welcome Guest, Not a member yet? Register   Sign In
Access helpers from smarty template
#1

[eluser]Jagar[/eluser]
I don't know if there's anyway of doing this, but I couldn't find a way to access a helper function that I needed from the template.

Therefore I created a smarty plugin that you load any helper and function and use it.

I'm sure there are smarty users out there who may benefit from this.

First place this in the smarty plugin folder: plugins/function.access_helper.php

Code:
<?php

function smarty_function_access_helper($params, &$smarty){

    if (!function_exists("get_instance")) {
        return "Can't get CI instance";
    }

    if (!function_exists($params['function'])) {
        $CI =& get_instance();
        $CI->load->helper($params['name']);
    }

    $func = $params['function'];
    return $func($params['value']);
    
}
?>

So within the template that is being parsed by smarty, you would use it as follow:
Code:
{access_helper name="helper_name" function="function_name_in_helper" value="value to be passed"}

The reason I made this is because I wanted to format the file size and needed to use the byte_format function inside number helper, so I used it as follow:
Code:
{access_helper name="number" function="byte_format" value="854565"}

and this will return the format byte

If there's a better way of doing this, please let me know.

Thanks!
#2

[eluser]Phil Sturgeon[/eluser]
Wouldn't you be better off making this into a modifier?

Code:
<?php
/*
* Smarty plugin
* -------------------------------------------------------------
* File:     modifier.helper.php
* Type:     modifier
* Name:     helper
* Purpose:  Call CodeIgniter helpers from within Smarty.
* -------------------------------------------------------------
*/
function smarty_modifier_helper($string, $helper_file, $helper_func)
{
    if (!function_exists("get_instance")) {
        return "Can't get CI instance";
    }

    if (!function_exists($helper_func)) {
        $CI =& get_instance();
        $CI->load->helper($helper_file);
    }

    // Get all the params passed in as there might be a few
    $params = func_get_args();

    // String provided should be the first param and we dont want helper file or helper func being passed
    $params[0] = $string;
    unset($params[1]);

    // Call the function with the params provided
    return call_user_func_array($helper_func, array_values($params));
}
?>

Example:

Code:
<p>Check out this amazing item <em>{$item.url|helper:'url':'anchor':$item.title:'class="more params"'}</em>.</p>

That way you can add as many params in as you like.

With modifiers the item you are modifying is the first param, then the rest go in separated by ":". With this specific modifier, you list the helper, then the helper function, then as many params afterwards as you like.

Can you test it for me and let me know if it works?
#3

[eluser]Jagar[/eluser]
Thanks for the feedback and better solution Phil Sturgeon.

This is better than plugin, I did test it and does work great.

Thanks!
#4

[eluser]mazdaFan[/eluser]
I use the modifier and and example posted here but i get error:


An Error Was Encountered

Unable to load the requested file: helpers/_helper.php

why?

Sorry i found the problem in my autoload.php: $autoload['helper'] = array('');
#5

[eluser]FDisk[/eluser]
[quote author="Jagar" date="1242394455"]Thanks for the feedback and better solution Phil Sturgeon.

This is better than plugin, I did test it and does work great.

Thanks![/quote]

need to unset one more parameter on line: 27

Code:
unset($params[1],$params[2]);
#6

[eluser]Aken[/eluser]
I don't know how pertinent this is to newer Smarty versions, given the age of this thread, but I've always used helpers just the same as before - load the helper in your controller, then call the function in your view like any other PHP function or variable. Never needed a modifier or anything special.

I do convert some proprietary code into modifiers, though. Such as when I want to process Markdown text.

Code:
{form_open('login')}




Theme © iAndrew 2016 - Forum software by © MyBB