Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[TwigBundle] [Form] Problem with twig warmer and form templates #21322

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

Closed
twoleds opened this issue Jan 17, 2017 · 5 comments
Closed

[TwigBundle] [Form] Problem with twig warmer and form templates #21322

twoleds opened this issue Jan 17, 2017 · 5 comments

Comments

@twoleds
Copy link
Contributor

twoleds commented Jan 17, 2017

Q A
Bug report? yes
Feature request? no
BC Break report? no
RFC? no
Symfony version 3.2.*

Twig warmer doesn't warm up templates for the form component. We use read-only file system on production and we have problem with the missing templates.

The form templates are in non-standard path: src/Symfony/Bridge/Twig/Resources/views/Form

bootstrap_3_horizontal_layout.html.twig
bootstrap_3_layout.html.twig
form_div_layout.html.twig
form_table_layout.html.twig
foundation_5_layout.html.twig

I try fix it somehow but I need some code review or discussion about it: master...twoleds:twig-form-warming

@xabbuh
Copy link
Member

xabbuh commented Jan 17, 2017

Couldn't we do that in the extension? And shouldn't we also add the paths discovered from other bundles here too?

@twoleds
Copy link
Contributor Author

twoleds commented Jan 17, 2017

I tried to do that in the extension and it works, but I want to keep warming the templates only if the form.extension service is enabled and I cannot check it in the extension because the service isn't available there. But if it isn't big problem with warming the templates when the extension is disabled, we can do it in the extension.

@yceruto
Copy link
Member

yceruto commented Aug 12, 2018

After compilation absolutely all template paths are defined in Twig\Loader\FilesystemLoader::$paths through twig.loader.native_filesystem service, including bundle paths, form theme paths, default project paths, custom project paths, etc.

My point is, shouldn't it be better iterate over the Twig loader paths instead of hardcoding again all possible paths here? (see #27764 where the new default path has been forgotten)

$this->templates = $this->findTemplatesInDirectory($this->rootDir.'/Resources/views');
foreach ($this->kernel->getBundles() as $bundle) {
$name = $bundle->getName();
if ('Bundle' === substr($name, -6)) {
$name = substr($name, 0, -6);
}
$this->templates = array_merge(
$this->templates,
$this->findTemplatesInDirectory($bundle->getPath().'/Resources/views', $name),
$this->findTemplatesInDirectory($this->rootDir.'/'.$bundle->getName().'/views', $name)
);
}
foreach ($this->paths as $dir => $namespace) {
$this->templates = array_merge($this->templates, $this->findTemplatesInDirectory($dir, $namespace));
}

Specifically, this is what I'm talking about:

// ...

$loader = $this->twig->getLoader();
if (!$loader instanceof FilesystemLoader) {
    return;
}

foreach ($loader->getNamespaces() as $namespace) {
    foreach ($loader->getPaths($namespace) as $path) {
        $this->templates = array_merge($this->templates, $this->findTemplatesInDirectory($path, $namespace));
    }
}

// ...

so we don't have to keep this dependency ($paths) updated everywhere:

$container->getDefinition('twig.cache_warmer')->replaceArgument(2, $config['paths']);
$container->getDefinition('twig.template_iterator')->replaceArgument(2, $config['paths']);

$paths = $container->getDefinition('twig.cache_warmer')->getArgument(2);
$paths[$coreThemePath] = null;
$container->getDefinition('twig.cache_warmer')->replaceArgument(2, $paths);
$container->getDefinition('twig.template_iterator')->replaceArgument(2, $paths);

WDYT?

@twoleds
Copy link
Contributor Author

twoleds commented Aug 13, 2018

@yceruto I forgot about this issue, thanks for reminding me. I think refactoring is not subject of this bug issue. You should create a new one.
@yceruto The issue with the form templates is they aren't part of any bundle, they are in bridge package for twig and the iterator doesn't include these paths. The second issue is the templates are part of bridge package for twig and the templates use functions & filters from the form component of symfony. The form component is optional and if the component is not installed then compilation of the templates will fail. The templates should be compiled only in case if the form component is installed & enabled.

@twoleds
Copy link
Contributor Author

twoleds commented Aug 13, 2018

I check this bug again and @weaverryan fixed it in the commit 2ef619f, thanks for fix

@twoleds twoleds closed this as completed Aug 13, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants