Page 1 of 1

registering functions in config.inc.php

Posted: Mon Nov 26, 2012 7:51 pm
by Don Chambers
Working on something in the latest 2.0 package.... This works in the 1.6/1.7 branch, but does not in the 2.0 branch:

Code: Select all

function distanceOfTimeInWords($fromTime, $toTime = 0) {
    $distanceInSeconds = round(abs($toTime - $fromTime));
    $distanceInMinutes = round($distanceInSeconds / 60);
    if ( $distanceInMinutes < 90 ) {
        return ELAPSED_ABOUT_ONE_HOUR_AGO;
    }
}

$serendipity['smarty']->register_function('elapsed_time_words', 'timeAgoInWords');

function timeAgoInWords($params, &$smarty) {
        return distanceOfTimeInWords($params['from_time'], time());
    }
I get the following error:
Fatal error: Uncaught exception 'SmartyCompilerException' with message 'Syntax Error in template "/var/www/vhosts/domain.com/templates/don/admin/index.tpl" on line 184 "{elapsed_time_words from_time=$smarty.now}" unknown tag "elapsed_time_words"' in /var/www/vhosts/domain.com/bundled-libs/Smarty/libs/sysplugins/smarty_internal_templatecompilerbase.php:657 Stack trace: #0 /var/www/vhosts/domain.com/bundled-libs/Smarty/libs/sysplugins/smarty_internal_templatecompilerbase.php(443): Smarty_Internal_TemplateCompilerBase->trigger_template_error('unknown tag "el...', 184) #1 /var/www/vhosts/domain.com/bundled-libs/Smarty/libs/sysplugins/smarty_internal_templateparser.php(2388): Smarty_Internal_TemplateCompilerBase->compileTag('elapsed_time_wo...', Array) #2 /var/www/vhosts/domain.com/bundled-libs/Smarty/libs/sysplugins/smarty_internal_templateparser.php(3101): Smarty_Internal_Templateparser->yy_r36() #3 /var/www/vhosts/s in /var/www/vhosts/domain.com/bundled-libs/Smarty/libs/sysplugins/smarty_internal_templatecompilerbase.php on line 657
Has something changed about registering functions in config.inc.php? Ive noticed I cannot install plugins in this testing branch either - are these two issues related?

Re: registering functions in config.inc.php

Posted: Tue Nov 27, 2012 8:59 am
by garvinhicking
Hi!

Hm, if something like this has changed, it should already have changed with Smarty3 in the 1.7 branch. Are you sure it works in 1.7? 1.7 and 2.0 should be similar in this regard...Timbalu?

Regards,
Garvin

Re: registering functions in config.inc.php

Posted: Tue Nov 27, 2012 3:38 pm
by Don Chambers
Now that you mention it, the highest version I am using is 1.6.2. I thought smarty 3 was only in the 2.0 branch. My mistake.

Re: registering functions in config.inc.php

Posted: Tue Nov 27, 2012 4:22 pm
by Don Chambers
I found a smarty 3 reference that mentioned a change in syntax from smarty 2 to smarty 3:
$smarty->register_function() = $smarty->register->templateFunction()
I tried:

Code: Select all

$serendipity['smarty']->register->templateFunction('elapsed_time_words', 'timeAgoInWords');
but that didn't work.

I also found a bit about registering a plugin, so I tried this without success:

Code: Select all

$serendipity['smarty']->registerPlugin("function", "elapsed_time_words", "timeAgoInWords"); 
Any ideas?

Re: registering functions in config.inc.php

Posted: Tue Nov 27, 2012 8:42 pm
by Don Chambers
More info: http://www.smarty.net/forums/viewtopic. ... erfunction

Definitely seems the replacement is supposed to be $smarty->register->templateFunction(...),but it is not working for me.

In serendipity 1.6.2, in smarty/libs/Smarty.class.php is this at line 691:

Code: Select all

    function register_function($function, $function_impl, $cacheable=true, $cache_attrs=null)
    {
        $this->_plugins['function'][$function] =
            array($function_impl, null, null, false, $cacheable, $cache_attrs);

    }
No such function exists in the 2.0 branch I am using, which seems to be @version 3.1.12. I do see the following in the file SmartyBC.class.php:

Code: Select all

    public function register_function($function, $function_impl, $cacheable=true, $cache_attrs=null)
    {
        $this->registerPlugin('function', $function, $function_impl, $cacheable, $cache_attrs);
    }
SO.... what am I doing wrong? :?

Re: registering functions in config.inc.php

Posted: Wed Nov 28, 2012 9:37 am
by Timbalu
Just a quick in...
I won't have time since I totally crashed all my system and data...
Don, why do you try this in a 2.0 environment? I am sure we even did not catch all things right by backend smartification in a default installation, so why make it worse trying things like this.
Is that a replacment for the default template...? That might be the issue, else the registerPlugin('function', foo, bar) notation should work.
The Smarty installation itself is the same as in 1.7 master.

Re: registering functions in config.inc.php

Posted: Wed Nov 28, 2012 5:46 pm
by Timbalu
Don, my test with a nearly current 2.0 gave...

2k11/config.inc.php

Code: Select all

    function distanceOfTimeInWords($fromTime, $toTime = 0) {
        $distanceInSeconds = round(abs($toTime - $fromTime));
        $distanceInMinutes = round($distanceInSeconds / 60);
        if ( $distanceInMinutes < 90 ) {
            return ELAPSED_ABOUT_ONE_HOUR_AGO;
        }
    }

    function timeAgoInWords($params, &$smarty) {
        return distanceOfTimeInWords($params['from_time'], time());
    }

    $serendipity['smarty']->registerPlugin('function', 'elapsed_time_words', 'timeAgoInWords');
index.tpl

Code: Select all

{elapsed_time_words from_time=$smarty.now}
result:
ELAPSED_ABOUT_ONE_HOUR_AGO

Re: registering functions in config.inc.php

Posted: Wed Nov 28, 2012 7:42 pm
by Don Chambers
Interesting... does not work for me. Going to reinstall.

Re: registering functions in config.inc.php

Posted: Wed Nov 28, 2012 9:23 pm
by Don Chambers
registerPlugin, or register->templateFunction both work.... for a frontend template, but neither work for a backend template. Is that by design or oversight?

Re: registering functions in config.inc.php

Posted: Thu Nov 29, 2012 8:22 am
by Timbalu
Can't say... as I dont know what exactly you were trying.
But I could imagine you would have to place the register placements to a backend core file, while the templates config.inc file will not get parsed, if in backend. That is design IMO, as Serendipity did/does not want to open the backend to the wild... This is still an open discussion, if we write a new backend markup but leave it internally or open it template-wise.

Edit: Forget my last words... it works even with set $serendipity['defaultTemplate'] = '2k11'; and/or likewise in admin/index.tpl

Re: registering functions in config.inc.php

Posted: Thu Nov 29, 2012 3:30 pm
by Don Chambers
I was creating a config.inc.php for a backend template. Perhaps this was never anticipated?

Re: registering functions in config.inc.php

Posted: Thu Nov 29, 2012 3:45 pm
by Timbalu
anticipated? maybe, but not implemented...
But you can put it into the templates config... at least with 1.7 and up...

Re: registering functions in config.inc.php

Posted: Thu Nov 29, 2012 4:54 pm
by Don Chambers
Is a template's front end config.inc.php loaded when in the backend/admin view?

Re: registering functions in config.inc.php

Posted: Thu Nov 29, 2012 5:22 pm
by Timbalu
can't say how it works internally so far, but my test said yes!