Skip to content

Allow extending FileLoader implementation #219

@daun

Description

@daun

Would it be possible to set the $baseDir property of the FileLoader class to protected instead of private? (see FileLoader.php@L23)

I'm trying to implement a custom file loader that allows prepending an alias to the root directory when including (e.g. {include '~partials/image.latte'} would render /path/to/views/dir/partials/image.latte).

The only change needed is in the getReferredName method, so I'd like to extend the base class and avoid re-implementing the whole thing. However, from my child class, I can't access the base dir property since it's private. See code below.

Let me know if there's a simpler method to accomplish this or any good reason to keep it as private.

Ideally, this would be part of the framework, but I'm okay with creating a custom loader for that and understand it might not make sense for every app.

Thanks.

BTW. If relevant, I'm not using Nette framework, this is on top of a third-party CMS.

This is what the custom loader would look like:

<?php namespace App; use Latte\Loaders\FileLoader; /**  * Custom template file loader.  *  * Modified to allow including files from basedir directly by prepending ~ or /  * Example:  * /partials/image.latte >> /path/to/view/dir/partials/image.latte  * ~partials/image.latte >> /path/to/view/dir/partials/image.latte  */ class CustomFileLoader extends FileLoader { /**  * Aliases that refer to the base dir when prepended to a filename  *  * @var array  */ static protected $baseDirAliases = [ '/', '~',	]; /**  * Returns referred template name.  *  * Check for prepended alias to baseDir and return path from there if found  * Otherwise, refer to default implementation  *  */ public function getReferredName($file, $referringFile): string	{ if ( $this->baseDir && in_array(substr($file, 0, 1), static::$baseDirAliases)	) { return $this->normalizePath(substr($file, 1));	} return parent::getReferredName($file, $referringFile);	} }

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions