Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

improve addpath in loader/ Filesystem to recursively add path #1501

Closed
kspal opened this issue Sep 9, 2014 · 3 comments
Closed

improve addpath in loader/ Filesystem to recursively add path #1501

kspal opened this issue Sep 9, 2014 · 3 comments

Comments

@kspal
Copy link

kspal commented Sep 9, 2014

Hi,

i use Twig as my template engine (without symfony)

I have a lot a of subdirectory in my templates directory, and i'm tired of adding manually all these subdirectory in the new Twig_Loader_Filesystem()

maybe there is an other way to do this ?

Anyway, i dont find anything to solve my problem so i propose this solution :

public function addPath($path, $namespace = self::MAIN_NAMESPACE)
{
// invalidate the cache
$this->cache = array();

    if (!is_dir($path)) {
        throw new Twig_Error_Loader(sprintf('The "%s" directory does not exist.', $path));
    }

    $this->paths[$namespace][] = rtrim($path, '/\\');

    $absolutepath=getcwd().'/';
    $absolutepath_len=strlen($absolutepath);
    $dir_iterator = new RecursiveDirectoryIterator($c.$path,FilesystemIterator::SKIP_DOTS);        
    foreach ($dir_iterator as $dir) {
        if ($dir->isDir()) {
            $path=substr($dir->getPathname(),$absolutepath_len);
            $this->paths[$namespace][] = rtrim($path, '/\\');
        }
    }      
}

kind regards

@Macsch15
Copy link

Macsch15 commented Sep 9, 2014

You can use Finder from Symfony without modify any Twig core files.

My example:

// Symfony\Component\Finder\Finder
$finder = $this->get('finder')
->directories()
->in(Application::createPath('library:Tricolore:View:Templates'));

foreach($finder as $file) {
    $directories[] = $file->getRealpath();
}

$directories = array_merge($directories, [
    Application::createPath('library:Tricolore:View:Templates'), 
    Application::createPath('library:Symfony:Bridge:Twig:Resources:views:Form')
]);

$loader = new \Twig_Loader_Filesystem($directories);

👍

@stof
Copy link
Member

stof commented Sep 10, 2014

IMO, it does not make sense to add subfolders automatically moist of the time. Whe using subfolders to organize templates you will generally want to have the subfolder appearing in the template name. Otherwise, you would still have to use unique filenames accross all you folders.

So such code has no chance to be added in Twig_Loader_Filesystem::addPath IMO. It would be totally unexpected

@kspal
Copy link
Author

kspal commented Sep 10, 2014

Macsch15 :
You're right ! i can make the array of subfolder before the call of Twig_Loader_Filesystem.

stof :
I agree with you,

Thanks to you, you open my eyes to achieve my goal in a new way !

@kspal kspal closed this as completed Sep 10, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

3 participants