weixin_39622521 2020-11-29 14:52
浏览 0

Extending Smarty_Internal_Resource_File populate method

Hi,

i write dynamic module loading and i need to overwrite a class Smarty_Internal_Resource_File -> populate method.

  • {include file=module/ppp/template.tpl} doeas not exists
  • if folder "ppp" does not exists on module DIR, i need to load module/dynamic/template.tpl
  • Is there a better way from editing SMARTY_INTERNAL_RESOURCE_FILE method?

Now i'm doing this:


public function populate(Smarty_Template_Source $source, Smarty_Internal_Template $_template = null)
    {
        /* START DYNAMIC */
        $resource = $source->resource;
        if (
            !file_exists($_SERVER['DOCUMENT_ROOT'] . str_replace($_SERVER['DOCUMENT_ROOT'], '', str_replace('file:','', $resource)))
            &&
            file_exists($_SERVER['DOCUMENT_ROOT'] . 'modules/dynamic')
        ){
            $test = str_replace($_SERVER['DOCUMENT_ROOT'], '', str_replace('file:','', $resource));
            $construct = explode('/', $test);
            $construct[1] = 'dynamic';
            $resource = ((strpos($resource, 'file:') === false) ? '' : 'file:') . implode('/', $construct);
            $name = ((strpos($resource, 'file:') === false) ? '' : 'file:') . implode('/', $construct);

            $source->resource = $resource;
            $source->name = $name;
        }
        /* DYNAMIC END */
        /* START SMARTY ORIGINAL CODE  */
    $source->filepath = $this->buildFilepath($source, $_template);

        if ($source->filepath !== false) {
            if (isset($source->smarty->security_policy) && is_object($source->smarty->security_policy)) {
                $source->smarty->security_policy->isTrustedResourceDir($source->filepath, $source->isConfig);
            }
            $source->exists = true;
            $source->uid = sha1($source->filepath . ($source->isConfig ? $source->smarty->_joined_config_dir :
                                    $source->smarty->_joined_template_dir));
            $source->timestamp = filemtime($source->filepath);
        } else {
            $source->timestamp = $source->exists = false;
        }
    }

Thank you for any answers

该提问来源于开源项目:smarty-php/smarty

  • 写回答

0条回答 默认 最新

    报告相同问题?