0

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?

Sumit patel
  • 3,807
  • 9
  • 34
  • 61
user1149244
  • 711
  • 4
  • 10
  • 27

1 Answers1

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
)
Community
  • 1
  • 1
Nikolay Mihaylov
  • 3,868
  • 8
  • 27
  • 32
  • Yes true, but what you stated above will only accept string parameters. My $parameters is a dynamic array. It could be 3 values or 4 values in just one request to the custom helper. My problem is how to make the $parameters to array type not string. – user1149244 Aug 25 '16 at 14:52
  • Just test it and you will see it is working as intended. Updating my answer with test example. – Nikolay Mihaylov Aug 25 '16 at 14:57
  • I just tried it and now i got this error, Fatal error: Class 'MyCustomUrlHelper' not found in cache/volt/%apps%%invo%%views%%test%%list.volt.php on line 56. I will add my sample code above so that it would be easier to read – user1149244 Aug 26 '16 at 02:17
  • 1
    Alright figured out why i got that fatal error. You should put all the directory in the services compiler registration like in my case it will be return 'Modules\Library\MyCustomUrlHelper.php::convert('.$resolvedArgs.')'; And in your helper, the method should be public static function convert($params) { define some code } or else you will get a warning "Strict Standards: Non-static method Modules should not be called statically". I consider this as resolve now. Thanks, @Nikolay! – user1149244 Aug 26 '16 at 02:59
  • What that ```d($param);``` mean? – Boris Delev Aug 26 '16 at 07:34
  • 1
    `d()` is a pretty print wrapper for `print_r()` :) – Nikolay Mihaylov Aug 26 '16 at 07:36