Phalcon伏特数组是字符串类型 [英] Phalcon Volt Array is of string type

查看:349
本文介绍了Phalcon伏特数组是字符串类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个要在自定义帮助程序中调用的Phalcon volt模板,它将接受一个数组,但是发送给该帮助程序的数组是字符串类型.

I have a Phalcon volt template I wanted to call in my custom helper, it will accept an array but the array sent to the helper is of string type.

在我的list.volt中,我有此代码

{% set myfolder = data.foldername %}
{% set key = data.folderkey %}
{% set url = convert([myfolder, key]) %}

我的loader.php 中,我已经声明了helper目录并具有以下代码:

In my loader.php, I have declared the helper directory and have this code:

 //$params should be single dimensional array
 $compiler->addFunction('convert', function($params){
      var_dump($params);
      return MyCustomHelper::convert($params);  
 });

将输出string(31) "array($fname, $fkey)"而不是数组类型.这使我的助手停止工作了.

Will output string(31) "array($fname, $fkey)" instead of an array type. It made my helper stop working.

有人面对这个吗,我需要它是数组类型而不是字符串吗?

Anyone face this, I need it to be of an array type not string?

更新:应用@Nikolay Mihaylov建议之后.

UPDATE: After applying @Nikolay Mihaylov suggestion.

出现错误

致命错误:在第56行的缓存/volt/%apps%%invo%%views%%test%%list.volt.php中找不到类"MyCustomUrlHelper"

Fatal error: Class 'MyCustomUrlHelper' not found in cache/volt/%apps%%invo%%views%%test%%list.volt.php on line 56

在我的 services.php 中,我包含了我的帮助程序目录

In my services.php, I've included my helper directory

use Modules\Library\MyCustomUrlHelper;

/*
    ......
    Some code here
   ..............................
   ....................

*/

$compiler->addFunction('convert', function($resolvedArgs, $exprArgs){ 
                    return 'MyCustomUrlHelper::convert('.$resolvedArgs.')';
                });

在loader.php中,我已经注册了目录

In loader.php, i've registered the directory

 ........
..................... 
$loader->registerDirs(array(APP_PATH.'Modules/Library'))->register();
...................
........................

在我的 Modules/Library 目录中,我有这个 MyCustomUrlHelper.php

In my Modules/Library directory, i have this MyCustomUrlHelper.php

<?php
namespace Modules\Library;
use Phalcon\Tag;    

class MyCustomUrlHelper extends Tag
{

    public function convert($params)
    {
        if(!is_array($params))
        {
            $params = array($params);   
        }

        /*
            ..... some code here ...
            .................
            ..........  
        */

        return $converted;
    }

}
?>

我还想念其他东西吗?

推荐答案

这是扩展伏特的正确方法:

This is the correct way of extending volt:

$compiler->addFunction('convert', function($resolvedArgs, $exprArgs){
    return 'MyCustomHelper::convert(' . $resolvedArgs . ')';  
});

将允许我自己引用文档:

Will allow myself to quote docs:

函数充当普通的PHP函数,需要有效的字符串名称 作为函数名称.可以使用两种策略来添加功能, 返回一个简单的字符串或使用一个匿名函数.永远是 要求所选策略返回有效的PHP字符串 表达.

Functions act as normal PHP functions, a valid string name is required as function name. Functions can be added using two strategies, returning a simple string or using an anonymous function. Always is required that the chosen strategy returns a valid PHP string expression.

以下链接中的更多信息:

More info in the following links:

文档: https://docs.phalconphp.com/en /latest/reference/volt.html#id1

SO上的类似问题:将变量从伏特发送到自定义功能

Similar question at SO: Sending variable from volt to custom function

更新:添加示例代码和输出.

Update: adding example code and output.

伏特自定义功能:

$compiler->addFunction('testArrays', function($resolvedArgs, $exprArgs) {
    return 'Helpers\VoltCms::testArrays(' . $resolvedArgs . ')';
}); 

帮助文件:

public static function testArrays($param)
{
    d($param);
}

用法和输出:

{{ testArrays(['asd', 'asd1']) }}

Array
(
    [0] => asd
    [1] => asd1
)

这篇关于Phalcon伏特数组是字符串类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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