Forums / Cotonti / Extensions / Slots and Tags

Extension to use TPL tags and functions in menu slots

Macik
#36388 2012-12-02 19:47
#36386 Trustmaster:

Yeah, that's why I've been releasing a lot of plugings which work in the templates this way: {PHP|insert_me_anywhere('some_params')}. It requires some good reasoning though whether to make something global or for a fixed place only, so many plugs (especially the old ones which could not benefit from the new CoTemplate features) still work the old way.

It's a good practice and I use it too. But… It has a back (negative) side. If we use this style: «insert_me_anywhere» callbacks - we force to load bundle of code in advance, so we have pack of functions in global space that can be called never at all.

For example, lets see in «attach2» plugin (it's great itself but I speaking now about callback styles only). It always load `attach2.functions.php` in «global» hook. What does it mean - 23kb file loaded and adds 26 addition functions in global space. It's not terrible themself but it happend on all pages. More plugins - more junk loaded and waste resources.

And I think we have to choose solution from 2 ways:

  1. Make core function «cot_load»:
    // Includes specified file
    // for parameters description see cot_incfile() function 
    function cot_load($name,$type,$part)
    {
      require_once cot_incfile($name,$type,$part);
    }

    Now we don't need to use «global» hook and load code always. We add {PHP|cot_load('attach2', 'plug')} in the beginning of desired TPL file, where {PHP|att_*} functions should be used.

  2. Using functions autoload register:  our plugin («attach2» in this example) in «global» hook should register list of its function in global «autoload function register» (that Cototnti should provide), like this:

    $cot_autoload_functions['attach2.plug'] = array('att_add','att_check','att_count', … );

    Other magic happened in CoTemplate while parsing callbacks: it checks for function exists in global space, then if not exists it checks «autoload register» and includes required file.

And I preffered second one. So it should be simple to inplement, not crashed logic of use tpl callbacks, and can be done backward compatible in plugins.

https://github.com/macik
правильный хостинг — https://goo.gl/fjCa1F
This post was edited by Macik (2012-12-02 19:55, 11 years ago)