cakephp-twig
cakephp-twig copied to clipboard
.ctp templates from Plugins
Hi Paul,
Nice work on this plugin!
I'm using this within a CakePHP project that relies on plugins like Acl, which has it's own templates in ctp plain PHP. How have you been dealing with this kind of issue?
Rather than re-write those in twig syntax, I've considered a couple approaches including checking for non-existent .twig templates, and dropping into regular cake View with the available vars, before returning to TwigView flow.
Though I'd like to stay consistent with the project and contribute a patch if it's needed.
Thanks! Mike
Or perhaps a custom filesystemloader that can handle either?
@mikescar Glad you are liking the plugin. Things are still under development so feel free to contribute with pull requests. I am not quite sure how I want to address what you stated yet.
I was trying to not rely on the View:: _getViewFileName() method to find the appropriate views, and instead rely on the custom FileSystemLoader
included in the plugin. CakePHP considers both .ctp and .twig views when you define a custom extension, which doubles potential file lookups.
I will have to test a little more, but I believe you should be able to add your own .twig
views for that plugin in app/View/Plugin/
(example: app/View/Plugin/PluginName/ControllerName/view.html.twig
) path and they should work. Mind sharing any particular plugin you are using and trying to get this working with?
You might also be interested in the feature/docs branch which is a start of documentation. It's not complete, but I am going to be adding more in that feature branch and then merging it in later.
Here's one plugin that I am using for my project: http://www.alaxos.net/blaxos/pages/view/plugin_acl_2.0 (git repo: https://github.com/mikescar/alaxos_acl)
It has a lot of ctp templates that I don't necessarily want to convert to Twig, and get out of sync. Also if we had failover capability it would be easier to add Twig to existing Cake projects without re-writing all the templates up front. Trying Cake plugins would be easier as well.
I'd like to create a solution like the twig->php failover by filename here: http://symfony.com/doc/current/cookbook/templating/PHP.html ... it wouldn't necessarily have the same issues with multiple lookups.
Though you mention "CakePHP considers both .ctp and .twig views when you define a custom extension, which doubles potential file lookups." -- am I missing something simple that's already in place, if the file lookup cost isn't a concern for me?
At the moment, I am completely relying on the custom FileSystemLoader class to find templates instead of the _getViewFilename()
method. This is a large diversion from CakePHP's built-in view so I need to spend more time reviewing the render
method. If you need something quick, perhaps you can try dropping in an older version of the TwigView class that I originally wrote for CakePHP 1.3. That version considers .ctp
files and perhaps might be close to a drop-in replacement for now.
I am thinking that many CakePHP developers would need ctp
support for plugins, so I do want to revisit this and try to figure out an optimum way of overcoming this.
The other issue too, is that the TwigView render method does not support 2-pass rendering at the moment, which .ctp files use. This needs more investigation and planning on my part.
Thanks Paul!
Not in a hurry, I want to do this right and it's not directly impacting main application stuff. Just nice to have Twig instead of raw PHP echo type stuff. I'm new to Cake so learning the internals first will be best.
What license do you plan on using?
I haven't figured that out yet, but definitely "do whatever you want" open-source :) I might like to have the author info included.
@mikescar I started experimenting with mixing the rendering of .ctp and .twig files in the experimental/twig-and-ctp branch.
If your layout is a .html.twig file, use {{ content_for_layout }}
in the layout as the default output of the block. Generally the default would be {{ content_for_layout }}
and .twig templates would override this block.