php - How can I set own Tag class in Volt (Phalcon) -


i want redeclare , add methods helper tag.

class mytags extends \phalcon\tag {     public static function mytag($params)     {        <...>     } } 

in services.php

$di->set('tag', function() {     return new mytags(); }; 

but works php engine, not volt.

{{ mytag() }} 

returned

undefined function 'mytag' 

first of all: don't use tag service name because it's used phalcon's tag object. secondly can use static methods class.

below working example mytag using config app changed names example.

$di->set( 'view', function () use ($config) {     $view = new view();     $view->setviewsdir($config->application->viewsdir);     $view->registerengines(         array(             '.volt' => function ($view, $di) use ($config) {                  $volt = new voltengine($view, $di);                 $volt->setoptions(                     array(                         'compiledpath' => $config->application->cachedir,                         'compiledseparator' => '_',                         'compilealways' => false                     )                 );                 $compiler = $volt->getcompiler();                  // add function                 $compiler->addfunction(                     'mytag',                     function ($resolvedargs, $exprargs) {                         return 'mytags::mytag(' . $resolvedargs . ')';                     }                 );                  // or filter                 $compiler->addfilter(                     'myfilter',                     function ($resolvedargs, $exprargs) {                         return 'mytags::mytag(' . $resolvedargs . ')';                     }                 );                  return $volt;             }         )         );          return $view;     },     true ); 

then can use mytag() function in volt views.

but if want use object don't use static methods:

class mytags extends \phalcon\tag {     /**      * no static keyword here      */     public function mytag($params)     {        <...>     } } 

in services use object:

$di->set('mahtag', function() {     return new mytags(); }; 

and in in volt:

{{ mahtag.mytag() }} 

Comments

Popular posts from this blog

plot - Remove Objects from Legend When You Have Also Used Fit, Matlab -

java - Why does my date parsing return a weird date? -

Need help in packaging app using TideSDK on Windows -