开发者

Trying to call a smarty plugin (with params) in smarty foreach

开发者 https://www.devze.com 2023-02-06 16:41 出处:网络
I\'m trying to embed a personal plugin into my smarty TPL files, but I couldn\'t make it work... This is my smarty plugin:

I'm trying to embed a personal plugin into my smarty TPL files, but I couldn't make it work...

This is my smarty plugin:

<?php
function smarty_function_alticerik($params, &$smarty) {
    if (!function_exists('alticerik')) {
        if (!function_exists('get_instance')) return "Can't get CI instance";
        $CI= &开发者_C百科;get_instance();
    }

    return $CI->IceriklerMod->liste($params['where']);
}      
?>

And these are my jabber wocky TPL codes:

{foreach item=alt from=alticerik|@alticerik(where="where ustid=$ustid")}
{$alt.id}
{/foreach}

I have searched and read all of the smarty help pages but I still have no idea that how can I make this codes work correctly...


I believe your issue is that functions don't get called using that Smarty syntax.

What you're doing is sort of a mix between a function and a modifier.

Modifiers change a given input - for example, lower casing a string.

{$upperCaseWord|strtolower}

Functions take named parameters and usually do a bit more work such as creating a dropdown.

{html_options options=$arrayOfOptions selected=$selectedValue}

In your case, I'm guessing that you want to use a modifier since you look to be attempting to modify a value. You can still pass options into those, but they aren't named and it gets fairly confusing quickly. However, the code might be:

{foreach item=alt from=$alticerik|@alticerik:"where ustid=$ustid"}
     {$alt.id}
{/foreach}

Meanwhile your actual function looks like:

<?php
function smarty_modifier_alticerik($input, $whereString) {
    // Here, $input is the same as your $alticerik variable
    // and $whereString is just the string that comes after the colon.
    if (!function_exists('alticerik')) {
        if (!function_exists('get_instance')) return "Can't get CI instance";
        $CI= &get_instance();
    }

    return $CI->IceriklerMod->liste($whereString);
}      
?>

Note, however, that in your code, you don't end up using the value of $alticerik from your template, so it makes me wonder if you need a function instead. I can't really know for sure unless I get what the alticerik plugin is supposed to do.

For more information on functions and modifiers, see the documentation here: Smarty.net Function Documentation and here: Smarty.net Modifier Documentation.


Do you mean $CI =& get_instance(); perhaps?

and what's not working? Any errors?

0

精彩评论

暂无评论...
验证码 换一张
取 消