feulf / raintpl3

The easiest Template Engine for PHP
https://feulf.github.io/raintpl
258 stars 57 forks source link

Template subfolder #147

Open ghost opened 10 years ago

ghost commented 10 years ago

I can't load a template in a subfolder.

When I use this:

  $rainTpl->draw("work/overview");

It does this

  C:\wamp\www\ravenproj/storage/themes/minimal/overview.html

but when changing to

  $rainTpl->draw("work\overview");

then it works.. but how can I let it work with forward slashes

  C:\wamp\www\ravenproj/storage/themes/minimal/work\overview.html
WebInac commented 10 years ago

I just fixed it for my project, Here is my solution:

protected function _check_template( $template ){ // set filename $template_name = basename( $template );

    $template_basedir           = dirname($template)== '.' ?null: dirname($template) . '/';
    $template_directory         = (strpos($template,"//")=== false&&strpos($template,":\\")=== false)?static::$conf['tpl_dir'] . $template_basedir:$template_basedir;
    $template_filepath          = $template_directory . $template_name . '.' . static::$conf['tpl_ext'];
    $parsed_template_filepath   = static::$conf['cache_dir'] . $template_name . "." . md5( $template_directory . serialize( static::$conf['checksum'] ) ) . '.rtpl.php';

    // if the template doesn't exsist throw an error
    if( !file_exists( $template_filepath ) ){
        $e = new RainTpl_NotFoundException( 'Template '. $template_name .' not found!' );
        throw $e->setTemplateFile($template_filepath);
    }

    // Compile the template if the original has been updated
    if( static::$conf['debug']  ||  !file_exists( $parsed_template_filepath )  ||  ( filemtime($parsed_template_filepath) < filemtime( $template_filepath ) ) )
        $this->_compile_file( $template_name, $template_basedir, $template_filepath, $parsed_template_filepath );

    return $parsed_template_filepath;
}