registering functions in config.inc.php

Discussion corner for Developers of Serendipity.
Post Reply
Don Chambers
Regular
Posts: 3652
Joined: Mon Feb 13, 2006 2:40 am
Location: Chicago, IL, USA
Contact:

registering functions in config.inc.php

Post 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?
=Don=
garvinhicking
Core Developer
Posts: 30022
Joined: Tue Sep 16, 2003 9:45 pm
Location: Cologne, Germany
Contact:

Re: registering functions in config.inc.php

Post 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
# Garvin Hicking (s9y Developer)
# Did I help you? Consider making me happy: http://wishes.garv.in/
# or use my PayPal account "paypal {at} supergarv (dot) de"
# My "other" hobby: http://flickr.garv.in/
Don Chambers
Regular
Posts: 3652
Joined: Mon Feb 13, 2006 2:40 am
Location: Chicago, IL, USA
Contact:

Re: registering functions in config.inc.php

Post 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.
=Don=
Don Chambers
Regular
Posts: 3652
Joined: Mon Feb 13, 2006 2:40 am
Location: Chicago, IL, USA
Contact:

Re: registering functions in config.inc.php

Post 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?
=Don=
Don Chambers
Regular
Posts: 3652
Joined: Mon Feb 13, 2006 2:40 am
Location: Chicago, IL, USA
Contact:

Re: registering functions in config.inc.php

Post 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? :?
=Don=
Timbalu
Regular
Posts: 4598
Joined: Sun May 02, 2004 3:04 pm

Re: registering functions in config.inc.php

Post 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.
Regards,
Ian

Serendipity Styx Edition and additional_plugins @ https://ophian.github.io/ @ https://github.com/ophian
Timbalu
Regular
Posts: 4598
Joined: Sun May 02, 2004 3:04 pm

Re: registering functions in config.inc.php

Post 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
Regards,
Ian

Serendipity Styx Edition and additional_plugins @ https://ophian.github.io/ @ https://github.com/ophian
Don Chambers
Regular
Posts: 3652
Joined: Mon Feb 13, 2006 2:40 am
Location: Chicago, IL, USA
Contact:

Re: registering functions in config.inc.php

Post by Don Chambers »

Interesting... does not work for me. Going to reinstall.
=Don=
Don Chambers
Regular
Posts: 3652
Joined: Mon Feb 13, 2006 2:40 am
Location: Chicago, IL, USA
Contact:

Re: registering functions in config.inc.php

Post 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?
=Don=
Timbalu
Regular
Posts: 4598
Joined: Sun May 02, 2004 3:04 pm

Re: registering functions in config.inc.php

Post 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
Regards,
Ian

Serendipity Styx Edition and additional_plugins @ https://ophian.github.io/ @ https://github.com/ophian
Don Chambers
Regular
Posts: 3652
Joined: Mon Feb 13, 2006 2:40 am
Location: Chicago, IL, USA
Contact:

Re: registering functions in config.inc.php

Post by Don Chambers »

I was creating a config.inc.php for a backend template. Perhaps this was never anticipated?
=Don=
Timbalu
Regular
Posts: 4598
Joined: Sun May 02, 2004 3:04 pm

Re: registering functions in config.inc.php

Post by Timbalu »

anticipated? maybe, but not implemented...
But you can put it into the templates config... at least with 1.7 and up...
Regards,
Ian

Serendipity Styx Edition and additional_plugins @ https://ophian.github.io/ @ https://github.com/ophian
Don Chambers
Regular
Posts: 3652
Joined: Mon Feb 13, 2006 2:40 am
Location: Chicago, IL, USA
Contact:

Re: registering functions in config.inc.php

Post by Don Chambers »

Is a template's front end config.inc.php loaded when in the backend/admin view?
=Don=
Timbalu
Regular
Posts: 4598
Joined: Sun May 02, 2004 3:04 pm

Re: registering functions in config.inc.php

Post by Timbalu »

can't say how it works internally so far, but my test said yes!
Regards,
Ian

Serendipity Styx Edition and additional_plugins @ https://ophian.github.io/ @ https://github.com/ophian
Post Reply