尝试在 smarty foreach 中调用 smarty 插件(带参数)

发布于 2024-10-14 03:50:04 字数 651 浏览 1 评论 0原文

我正在尝试将个人插件嵌入到我的 smarty TPL 文件中,但我无法使其工作...

这是我的 smarty 插件:

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

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

这些是我的 jabber wocky TPL 代码:

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

我已经搜索并阅读了所有 smarty 帮助页面,但我仍然不知道如何使这些代码正确工作......

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= &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...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

你的背包 2024-10-21 03:50:04

我相信您的问题是函数不会使用 Smarty 语法调用。

您正在做的事情有点像函数和修饰符之间的混合。

修饰符更改给定的输入 - 例如,小写字符串。

{$upperCaseWord|strtolower}

函数采用命名参数,通常会做更多的工作,例如创建下拉列表。

{html_options options=$arrayOfOptions selected=$selectedValue}

就您而言,我猜测您想要使用修饰符,因为您似乎试图修改一个值。您仍然可以将选项传递给它们,但它们没有命名,而且很快就会变得相当混乱。但是,代码可能是:

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

同时您的实际函数如下所示:

<?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);
}      
?>

但是请注意,在您的代码中,您最终不会使用模板中 $alticerik 的值,因此这让我想知道您是否需要一个函数。我真的不能确定,除非我明白 alticerik 插件应该做什么。

有关函数和修饰符的更多信息,请参阅此处的文档:Smarty.net 函数文档< /a> 和此处:Smarty.net 修改器文档

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.

撩动你心 2024-10-21 03:50:04

您的意思是 $CI =& get_instance(); 或许?

什么不起作用?有错误吗?

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

and what's not working? Any errors?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文