How can i count array keys in ezpublish?

1.2k views Asked by At

i have a problem counting array keys value in ezpublish whose php function is

array_count_values();

i need a similar function in ezpublish. Is there any operators in ezpublish or i have to make an extension for it too?

7

There are 7 answers

0
Sujeet On BEST ANSWER

After research for long time i found that, there is no any ez publish template operators for array_count_values(); we have to make an extension for it. And process of making extension is given in the link below.

http://blog.developeronhire.com/using-custom-php-functions-in-ez-publish-templates/

Thank you all

1
Thurein Soe On

try this

function count_array_key($arr)
{          
    return count(array_keys($arr));
}

usage

$array = array("color" => array("blue", "red", "green"),
               "size"  => array("small", "medium", "large"));
echo count_array_key($array);
0
vlc On

You can declare the php function as a new template operator for templates to use.

Add it to the template.ini override file.

0
harmstyler On

If adding it to the template.ini of your site's extension does not work, just create an autoload. They are very simple to create and is how I often add any php function I need. You don't need a new extension for the autoload, just add it to your existing extension in the autoloads directory. Don't forget to declare your autoload in the eztemplateautoload.php file, you will likely have to add one of those to your autoloads directory too.

2
Nicolas On

The count operator is what you are looking for here, it appears :

{def $arrayCount = $myArray|count()}

More on this operator in the documentation : http://doc.ez.no/eZ-Publish/Technical-manual/4.x/Reference/Template-operators/Variable-and-type-handling/count

Generally, feel free to also drop your questions on the eZ Publish Community Portal : http://share.ez.no (but i think you already know this place ;) )

Hope it helps, Cheers,

0
MARTIN Damien On

If you need to use PHP native functions, you really should use this extension : http://projects.ez.no/wrap_operator

You just have to enable the selected function in the ini file of the extension and to use it in your templates.

For example :

{def $html = wrap_php_func('str_replace', array('&', '&', $article.data_map.descriptif.data_text))}

It will save your life !

0
foobar On

As said by Nicolas, you can map any PHP functions to a template operator if :

  • the function returns the result by value and not by reference
  • the function takes only one parameters (this might work with functions having one mandatory parameter and others which are optional but I can remember that it generates PHP warnings and that the optionals parameters will be ignored)

Documentation : http://doc.ez.no/eZ-Publish/Technical-manual/4.x/Reference/Configuration-files/template.ini/PHP/PHPOperatorList

So doing what you want is really easy since array_key_values() is a good candidate and matches the requirements.