Smarty Forum Index Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon.

How to run a custom function before compilation ?

 
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> General
View previous topic :: View next topic  
Author Message
ildar
Smarty Rookie


Joined: 14 May 2015
Posts: 14

PostPosted: Thu Sep 29, 2016 3:19 pm    Post subject: How to run a custom function before compilation ? Reply with quote

Hello,

How can I run a custom function before compilation?

Ie something like 'on compile hook for a file'/'on compile event listener a file'
or
something like 'global on compile hook'/'global compile event listener'

My possible solution is nested registerFilter call


Code:
       

    $smarty->registerFilter('pre', 'mainPreFilter');


    function mainPreFilter($source, $smarty)
    {//{{{
        static $is_called ;

        if (!empty($is_called)) {
            return $source;
        }
        $is_called = 1;

        require 'custom_functions_definitions.php';
        $smarty->registerFilter('pre', 'custom_pre_filter1');
        $smarty->registerFilter('pre', 'custom_pre_filter2');
        $smarty->registerFilter('pre', 'custom_pre_filter3');

        return $source;
    }//}}}



The main goal is to call this code
require 'custom_functions_definitions.php';
only once on compile event

Is it possible ?

I do not want to use the compiler_class setting
http://www.smarty.net/docs/en/variable.compiler.class.tpl


Thank you.
Back to top
View user's profile Send private message
AnrDaemon
Administrator


Joined: 03 Dec 2012
Posts: 1785

PostPosted: Thu Sep 29, 2016 3:58 pm    Post subject: Reply with quote

You must use class autoloader for that kind of tasks. Then you won't need to waste your time inventing wheels.
SEE ALSO: Composer.
Back to top
View user's profile Send private message
ildar
Smarty Rookie


Joined: 14 May 2015
Posts: 14

PostPosted: Fri Sep 30, 2016 6:28 am    Post subject: Reply with quote

I suppose

$smarty->addAutoloadFilters(array('mainPreFilter'), 'pre');

is better
Back to top
View user's profile Send private message
ildar
Smarty Rookie


Joined: 14 May 2015
Posts: 14

PostPosted: Fri Sep 30, 2016 6:29 am    Post subject: Reply with quote

AnrDaemon wrote:
You must use class autoloader for that kind of tasks. Then you won't need to waste your time inventing wheels.
SEE ALSO: Composer.


Thank you for the solution
Back to top
View user's profile Send private message
ildar
Smarty Rookie


Joined: 14 May 2015
Posts: 14

PostPosted: Fri Sep 30, 2016 8:17 am    Post subject: Reply with quote

AnrDaemon wrote:
You must use class autoloader for that kind of tasks. Then you won't need to waste your time inventing wheels.
SEE ALSO: Composer.


It seems autoloader does not work in this case


$smarty->registerFilter('pre', 'mainPreFilter');
will call
is_callable($callback)
will call
the autoloader function
will call
require 'custom_functions_definitions.php';
Back to top
View user's profile Send private message
AnrDaemon
Administrator


Joined: 03 Dec 2012
Posts: 1785

PostPosted: Fri Sep 30, 2016 8:50 am    Post subject: Reply with quote

Make your functions a static methods of a class.
Then it'll work.
However, I don't understand, why don't you just include the needed files explicitly?
Back to top
View user's profile Send private message
U.Tews
Administrator


Joined: 22 Nov 2006
Posts: 5068
Location: Hamburg / Germany

PostPosted: Sun Oct 23, 2016 10:29 pm    Post subject: Reply with quote

Why did you no use
Code:

require_once 'custom_functions_definitions.php';
Back to top
View user's profile Send private message
ildar
Smarty Rookie


Joined: 14 May 2015
Posts: 14

PostPosted: Thu Nov 10, 2016 4:22 pm    Post subject: Reply with quote

U.Tews wrote:
Why did you no use
Code:

require_once 'custom_functions_definitions.php';


I have posted an related issue
https://github.com/smarty-php/smarty/issues/314
Back to top
View user's profile Send private message
Display posts from previous:   
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> General All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group
Protected by Anti-Spam ACP