Phalcon Volt Array is of string type

北战南征 提交于 2019-12-25 06:33:35

问题


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.

In my list.volt I have this code,

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

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

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?

UPDATE: After applying @Nikolay Mihaylov suggestion.

Got an error

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

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.')';
                });

In loader.php, i've registered the directory

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

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

}
?>

Did i miss something else?


回答1:


This is the correct way of extending volt:

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

Will allow myself to quote docs:

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:

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

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


Update: adding example code and output.

Volt custom function:

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

Helper file:

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

Usage and Output:

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

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


来源:https://stackoverflow.com/questions/39148199/phalcon-volt-array-is-of-string-type

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!