Wanze Posted July 3, 2014 Share Posted July 3, 2014 TemplateEngineTwig This module adds Twig as engine to the TemplateEngineFactory. Screenshot of the available configuration options: Project on GitHub: https://github.com/wanze/TemplateEngineTwig Project in modules directory: http://modules.processwire.com/modules/template-engine-twig/ Only Twig related things should be discussed in this thread. For common problems/features/questions about the Factory, use the TemplateEngineFactory thread. 8 Link to comment Share on other sites More sharing options...
Gazley Posted July 20, 2016 Share Posted July 20, 2016 Hi @Wanze, Many thanks for providing this fantastic module! I have just installed the module but immediately hit a problem that is not of your making in any way, but a code change in TemplateEngineTwig.module would make my life very much easier and possibly other future users of the module! Basically, I already use Twig for some TemplateFile rendering (Field level templates) using my own custom module. I've decided to use your Controller/View approach for my regular ProcessWire templates but when trying to install your module, I get the following error: "Cannot redeclare class Twig_Autoloader". Obviously, this is because my module has already loaded this class and TemplateEngineTwig.module is trying to do the same thing again. The following code change at the top of TemplateEngineTwig.module will not hurt anyone else, but will help me enormously. If you could modify the module to make this check, I'd really appreciate it! if (!class_exists('Twig_Autoloader')) { require_once(__DIR__ . '/vendor/twig/twig/lib/Twig/Autoloader.php'); } Many thanks indeed 1 Link to comment Share on other sites More sharing options...
Wanze Posted July 20, 2016 Author Share Posted July 20, 2016 Hi Gazley, Sorry I missed your post. Thanks for the suggestion, I'll add this to the next version so you are safe when you upgrade the module! Edit: Just saw that your post was pretty new haha Cheers 2 Link to comment Share on other sites More sharing options...
Gazley Posted July 20, 2016 Share Posted July 20, 2016 Thank you very much @Wanze!!! Link to comment Share on other sites More sharing options...
Manaus Posted July 26, 2016 Share Posted July 26, 2016 Hello, I'm trying out a template engine conversion of a website. In a foo.twig page I'm calling a isValidUser function, which I required in a foo.php template page. But the engine returns the function in unknown. How do I include functions in a twig template? Thanks! Link to comment Share on other sites More sharing options...
dotnetic Posted July 26, 2016 Share Posted July 26, 2016 Hi Manaus, which Template Engine Module do you use? Template Engine Factory or Template Twig Replace, or something different? Please post the code of the twig file here. Please give more information about your setup (ProcessWire Version, etc). Normally you would call a function in twig with {% myFunc(params) %} Link to comment Share on other sites More sharing options...
Manaus Posted July 26, 2016 Share Posted July 26, 2016 Thanks jmartsch, I'm using Template Engine Fatory, with 2.7. I think the problem resides in passing values to the twig templates. If I do $login = $pages->get('/login/'); And then in the twig template: <p>{{ login.url }}</p> I don't see any value. How do I pass values? Thanks! Link to comment Share on other sites More sharing options...
Manaus Posted July 27, 2016 Share Posted July 27, 2016 Ok I got it, I gotta put values within the $view object. It would be fun to have all values passed automatically yet. Too prone to chaos? Link to comment Share on other sites More sharing options...
dotnetic Posted July 27, 2016 Share Posted July 27, 2016 So you found the answer yourself. Yes, you need to assign your variables to the view variable like this: $view->set('login', $login); All processwire API variables (page, input, config etc.) are available (per setting in Template Engine Twig). So you can use {{ page.title }} directly. Link to comment Share on other sites More sharing options...
Gazley Posted July 27, 2016 Share Posted July 27, 2016 Hi @Manaus, If you want to call your isValidUser function in your Twig template, you'll have to extend Twig by hooking the ___initTwig method in TemplateEngineTwig.module. Assuming, I've called the hook method 'hookInitTwig', you'd do something like below (not tested!!!): public function hookInitTwig(HookEvent $event) { $twig = $event->arguments('twig'); $function = new \Twig_SimpleFunction('isValidUser', function (...) { // Assuming isValidUser function is in scope ... return isValidUser(...); }); $twig->addFunction($function); } Obviously, if you don't want to add the function directly into Twig, you can evaluate your isValidUser in the ProcessWire template (Controller) and assign its result to a variable that can be referenced inside the Twig template (view). Link to comment Share on other sites More sharing options...
Manaus Posted July 27, 2016 Share Posted July 27, 2016 Thanks guys, I spotted also that some native objects are passed with native values only, e.g. {{ user.summerVacationHotelAddress }} will not work. Link to comment Share on other sites More sharing options...
Manaus Posted July 27, 2016 Share Posted July 27, 2016 Hello, how do I enable the {{ dump(var) }} function in this module? Also if I do a var_dump from the calling page, the output is not displayed. Thank you very much Link to comment Share on other sites More sharing options...
Gazley Posted July 27, 2016 Share Posted July 27, 2016 Have you configured it? From http://twig.sensiolabs.org/doc/functions/dump.html The dump function is not available by default. You must add theTwig_Extension_Debug extension explicitly when creating your Twig environment: Link to comment Share on other sites More sharing options...
Manaus Posted July 27, 2016 Share Posted July 27, 2016 Hi @Gazley, yes -- or I think so --: I have added these lines on top of the calling php page: $twig = new Twig_Environment($loader, array( 'debug' => true, )); $twig->addExtension(new Twig_Extension_Debug()); But still {{ dump(var) }} is not recognized... Link to comment Share on other sites More sharing options...
Gazley Posted July 27, 2016 Share Posted July 27, 2016 You would have to add that code in the ___initTwig method in the Twig module. The $twig variable you create in the calling page is not connected to the template engine in any way - it's just a random, isolated variable. See my earlier post for and idea of where/how this is done: Link to comment Share on other sites More sharing options...
Manaus Posted July 27, 2016 Share Posted July 27, 2016 I tweaked the function like this: protected function ___initTwig(Twig_Environment $twig) { $loader = new Twig_Loader_Filesystem($this->getTemplatesPath()); // unless 'loader' unknown error $this->twig = new Twig_Environment($loader, array( 'debug' => true, )); } But I get a unknown "dump" function. I see there is also a initEngine function having 'debug' => $this->wire('config')->debug, So if debug is set to true, it should work... Thanks Link to comment Share on other sites More sharing options...
Gazley Posted July 27, 2016 Share Posted July 27, 2016 Yep, it seems that the module configures 'debug' (and therefore "dump") based on the value on config.php so, no additional setup required. You can remove the code from ___initTwig for the above reason. However, you do not add code directly to ___initTwig - you just hook ___initTwig and the code is elsewhere (see ProcessWire hooks). Next obvious question, do you have 'debug' set to true in your site/config.php file? If not, set it to true. Link to comment Share on other sites More sharing options...
Manaus Posted July 27, 2016 Share Posted July 27, 2016 I added wire()->addHook("TemplateEngineTwig::___initTwig", function($event) { $twig = new Twig_Environment($loader, array( 'debug' => true, )); $twig->addExtension(new Twig_Extension_Debug()); }); to a _init.php file I prepend to pages, but alas, dump is still unknown.... Yes $config->debug is set to true in the config file... Link to comment Share on other sites More sharing options...
Gazley Posted July 27, 2016 Share Posted July 27, 2016 You don't need the above code in the hook because, like I said earlier, it seems that the Twig module is already doing this. However, despite not needing the hook code, you have not implemented it correctly as you would need to access the $twig variable from the $event parameter. $twig = $event->arguments('twig'); Either way, remove your hook code from your _init.php file. If $config->debus is true, then I'm all out of ideas on this. Sorry. Link to comment Share on other sites More sharing options...
Manaus Posted July 27, 2016 Share Posted July 27, 2016 Oh ok pal, no problem, thanks for your time! Link to comment Share on other sites More sharing options...
Gazley Posted July 27, 2016 Share Posted July 27, 2016 Just one other thing, have you actually tried using var_dump in PHP on the value you are trying to dump? It may be that var_dump is outputting nothing natively and if so, the Twig dump function (being just a wrapper around var_dump) will likewise output nothing. Link to comment Share on other sites More sharing options...
Gazley Posted July 27, 2016 Share Posted July 27, 2016 Just a heads-up, I've just tried to use the dump function in my own code and get: Exception: Unknown "dump" function in "_header.html.twig" at line 31. Looks like I'll have to check this further!!! Link to comment Share on other sites More sharing options...
Wanze Posted July 27, 2016 Author Share Posted July 27, 2016 @Gazley Did you add the dump extension via hook? http://twig.sensiolabs.org/doc/functions/dump.html Even if debug is active in twig, this function needs to be added additionally. Cheers 1 Link to comment Share on other sites More sharing options...
Gazley Posted July 27, 2016 Share Posted July 27, 2016 Ah, that'll explain it and will likely resolve @Manaus's issue too! Many thanks for pointing this out Link to comment Share on other sites More sharing options...
Gazley Posted July 27, 2016 Share Posted July 27, 2016 @Wanze Yep, that was it -- dump() works perfectly now! 1 Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now