View previous topic :: View next topic |
Author |
Message |
ildar Smarty Rookie
Joined: 14 May 2015 Posts: 14
|
Posted: Thu Sep 29, 2016 3:19 pm Post subject: How to run a custom function before compilation ? |
|
|
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 |
|
AnrDaemon Administrator
Joined: 03 Dec 2012 Posts: 1785
|
Posted: Thu Sep 29, 2016 3:58 pm Post subject: |
|
|
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 |
|
ildar Smarty Rookie
Joined: 14 May 2015 Posts: 14
|
Posted: Fri Sep 30, 2016 6:28 am Post subject: |
|
|
I suppose
$smarty->addAutoloadFilters(array('mainPreFilter'), 'pre');
is better |
|
Back to top |
|
ildar Smarty Rookie
Joined: 14 May 2015 Posts: 14
|
Posted: Fri Sep 30, 2016 6:29 am Post subject: |
|
|
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 |
|
ildar Smarty Rookie
Joined: 14 May 2015 Posts: 14
|
Posted: Fri Sep 30, 2016 8:17 am Post subject: |
|
|
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 |
|
AnrDaemon Administrator
Joined: 03 Dec 2012 Posts: 1785
|
Posted: Fri Sep 30, 2016 8:50 am Post subject: |
|
|
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 |
|
U.Tews Administrator
Joined: 22 Nov 2006 Posts: 5068 Location: Hamburg / Germany
|
Posted: Sun Oct 23, 2016 10:29 pm Post subject: |
|
|
Why did you no use
Code: |
require_once 'custom_functions_definitions.php';
|
|
|
Back to top |
|
ildar Smarty Rookie
Joined: 14 May 2015 Posts: 14
|
|
Back to top |
|
|