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

main controller does not use same twig instance #3065

Closed
oleg-andreyev opened this issue Aug 27, 2017 · 15 comments
Closed

main controller does not use same twig instance #3065

oleg-andreyev opened this issue Aug 27, 2017 · 15 comments

Comments

@oleg-andreyev
Copy link

Main controller does not use same instance of Twig which does not allow to register custom extensions, in make case it's \Twig_Extension_StringLoader

@LukeTowers
Copy link
Contributor

This is not enough information to do anything with. Please provide detailed issue reports that actually describe what you are trying to do and why it is not working.

@oleg-andreyev
Copy link
Author

Trying to register Twig_Extension_StringLoader for template_from_string, but there is no entry point for this, I've found twig.enviroment singleton as entry point, but it's not used in main controller.

@LukeTowers
Copy link
Contributor

@oleg-andreyev I'm still not sure what you are trying to do. Does http://octobercms.com/docs/plugin/registration#extending-twig help?

See http://xyproblem.info/

@oleg-andreyev
Copy link
Author

  • I want to use build-in in Twig template_from_string function
  • To use it I need to register \Twig_Extension_StringLoader
  • To register any custom extension I need point of entrance and it's twig.environment or \Twig_Environment class
  • I've found \System\ServiceProvider::registerTwigParser which registers singleton twig.environment
  • I'm creating an extend of this singleton with following code
App::extend('twig.environment', function (\Twig_Environment $twig, Application $app) {
    $twig->addExtension(new \Twig_Extension_StringLoader());
});
  • ER: everything to work and template_from_string should be available
  • AR: template_from_string isn't available because \Cms\Classes\Controller::initTwigEnvironment (front controller) does not use twig.environment singleton and is creating Twig_Environment on it's own

Instead of creating new instance of Twig_Environment, OctoberCMS should call it from DI by calling App::make('twig.environment')

@oleg-andreyev
Copy link
Author

ping

@LukeTowers
Copy link
Contributor

What is it exactly that you are trying to do that requires the template_from_string? I understand how you're running into issues with your approach, what I'm looking for is what you are actually wanting to do with that.

@oleg-andreyev
Copy link
Author

I have form with prototype element, when I click on button it's adding new field.
To better understand prototypes, I'll refer to Symfony Docs: https://symfony.com/doc/current/form/form_collections.html#allowing-new-tags-with-the-prototype

@LukeTowers
Copy link
Contributor

I'm afraid I still have no idea what you're talking about.

Are you trying to render a Twig template from a string?

@oleg-andreyev
Copy link
Author

To keep it simple, yes, I need to render template from string.

@LukeTowers
Copy link
Contributor

@oleg-andreyev does

use Twig;

Twig::parse($templateString, $templateVars);

not work for some reason?

@KonstantinObuhov
Copy link
Contributor

I'm trying to do something like @oleg-andreyev.
For instance it may be rendering some parts of twig template from a data base, but Twig::parse doesn't work with october filters and this won't work {{ 'sertifikatyi'|page }}. Any suggestions?

@LukeTowers
Copy link
Contributor

@KonstantinObuhov Using the Twig facade by itself won't load any CMS extensions, to fix that just register them again in your plugin instead. See modules/cms/ServiceProvider.php

@oleg-andreyev
Copy link
Author

@LukeTowers imo application should have single instance of Twig (unless there is really specific case), and it should accessible by all plugins via DI and facade should provide access to it.

Case

  • customer made registered Twig extension A
  • customer installed plugin B
    ER:
  • extension A work in plugin B
    AR:
  • extension A does not work in plugin B

Because Twig instances isn't shared

@LukeTowers
Copy link
Contributor

@oleg it already does. The CMS is a specific exception because it is a removable module, you don't have to use the CMS module to have a working October instance. It registers its own twig extensions only when it's actually being used, as opposed to the System module and all plugins loaded by the System module

@LukeTowers
Copy link
Contributor

I do agree that the behaviour is confusing for new developers, I myself spent a few hours figuring out why CMS extensions weren't working in my custom twig parsing. Perhaps you should submit a PR to include the CMS extensions in the global twig instance by default, although you'd have to keep in mind possible conflicts with the Mail twig instance / extensions

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