将变量从伏特发送到自定义功能 [英] Sending variable from volt to custom function

查看:84
本文介绍了将变量从伏特发送到自定义功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了可以从伏特访问的自定义功能.该函数似乎工作正常,但我无法设法将变量发送给该函数.它将变量作为文本而不是其值发送.

I have created a custom function I can access from the volt. The function seems to work fine, but I cannot manage to send the variable to the function. It sends the variable as text instead of its value.

细枝功能:

$volt->getCompiler()->addFunction('getusergroup', function ($user) {
    return \Models\User::getUserGroup($user);
});

模型中的功能:

public static function getUserGroup($user) {
    return UserGroup::find(array('conditions' => 'user_id = ' . $user));
}

Twig中调用该函数的行:

The lines in Twig to call the function:

{% for member in getusergroup(staff.id) %}
    {{ member.Group.name }}
{% endfor %}

我得到的错误:

"staff-> id之前的扫描错误"解析:SELECT [Models \ UserGroup].* FROM [Models \ UserGroup] WHERE user_id = $ staff-> id(78)'(length = 131)

'Scanning error before 'staff->id' when parsing: SELECT [Models\UserGroup].* FROM [Models\UserGroup] WHERE user_id = $staff->id (78)' (length=131)

如您所见,它是文本,而不是$staff->id是整数.

As you can see, in stead of $staff->id being an integer, it's the text.

如何将实际ID发送给函数?

How do I go about sending the actual ID to the function?

顺便说一句,我将Twig与Phalcon结合使用,并按照本文中的说明进行操作:

By the way, I am using twig in combination with Phalcon and followed the instructions in this article: http://phalcontip.com/discussion/60/extending-volt-functions

推荐答案

如果将$user转储到方法public static function getUserGroup($user)中,则会收到此 $ staff-> id ,但是您实际上想要像 42 这样的东西.

If you dump $user inside of your method public static function getUserGroup($user), you will receive this $staff->id, but you actually want something like 42.

为避免注册该伏特功能,如下所示:

To avoid this register the Volt function like this:

$volt->getCompiler()->addFunction('getusergroup', function ($resolvedArgs, $exprArgs) {
    return 'Models\User::getUserGroup(' . $resolvedArgs . ')';
});

有关扩展电压函数

这篇关于将变量从伏特发送到自定义功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆