Is there a way to have layouts/snippets as files?

Feed 12 posts, 5 voices

Avatar
4 posts

I just started using Frog, and love the simplicity of it.

The only thing I miss is a way to edit the layouts (and perhaps snippets) as files, and not stored in the database. The admin editor is quite limited when you're used to Textmate or Notepad++. It would of course be perfect if you could place a php file in a specific "layouts" folder and then in the admin interface specify that a layout was to use this file, but maybe that's something for future versions.

So does anyone know of a way to fetch the raw text from a file and have it used as the layout code? I've tried moving the layout code to a file and use a simple include(), but it just resulted in a blank page.

 
Avatar
1493 posts

I know very little about the programming/code side of Frog, so I'm not sure about this. My guess is that since (unlike CSS, which is in a separate file), "layouts" and "snippets" are stored in the db, it would be difficult to have them as separate files. At least, I can't see how it would be done!

One thing that ought to be possible is to have a better "code" editor within the admin backend. Another CMS I use (Symphony -- very different from Frog!) has a basic, but nice XML editor in its admin area which makes writing code in the CMS quite satisfying. Could there be something similar for Frog? Perhaps this is another plugin project for someone, adding a "filter" option for Layout and Snippet areas? Google found me this Javascript one. Again, I do not know how this would fit in with the other .js utilities Frog uses in its backend.

Any other ideas?

 
Avatar
4 posts

Thank you for the reply.

After some further investigation it actually seems that it actually is as simple as using a require() directive in the layout, and then just put all of the layout code in a file. The reason it didn't work the first time I tried might be that I used the include() function instead of require(), or that file includes were not allowed in my development environment.

I now tried both include() and require() running xampp lite (Win Vista), and both worked fine. Previously I tried using include() in a MAMP (OS X) environment which didn't work. I'll try that again later with require() instead.

I also found that in php.ini, these values need to be set for include() to work: allow_url_fopen = On allow_url_include = On

So, to use a php file for your layout, instead of working in the admin editor, simply: 1. Create a file. I created a "public/layouts/normal.php". 2. In the layout, add the single line <?php require('public/layouts/normal.php'); ?> 3. Put all the layout code in the file instead.

An improved editor in the admin area might be a good option, since it's nice to have everything in one place, but I'm very happy now, being able to edit HTML/CSS/JS as files.

 
Avatar
6 posts

Thanks!! I don't know I didn't came out with that solution!!! :P

thanks!

 
Avatar
651 posts

@Johan - Have you tried including a file using its absolute path? (i.e. /home/myuser/www/public/layouts/normal.php)

That should give you the same result without having to turn on allow_url_fopen and allow_url_include. That would make your installation more secure since there are some security concerns when setting allow_url_fopen to On for example. See http://nl.php.net/manual/en/filesystem.configuration.php

I tested this on my system and it worked quite nicely.

 
Avatar
4 posts

@mvdkleijn - Using an abolute path worked fine, and certainly seems like the preferred solution. Thanks!

 
Avatar
486 posts

this will be very helpful. thanks for posting the question and the solution!

 
Avatar
486 posts

Would it be good to make this the default behavior for Frog? To have layouts in files? I would prefer it that way, but what does everybody else think?

 
Avatar
486 posts

Also are there any security implications to putting layouts and snippets under the public directory?

 
Avatar
4 posts

@ricks:

I would also prefer this to be the default behaviour, but perhaps even better might be to make it possible through a checkbox in the pages/snippets interface: “Use file”. If this is checked or not by default could be a setting in admin.

If “Use file” is checked, a textbox enables entering the file name (or perhaps naming of the file should be like the page/snippet).

I don’t know about security problems, but I don’t think so (and hope not since I’ve published two sites with this solution).

 
Avatar
651 posts

About security implications when using the files – There should be none assuming you’ve named them XXXXX.php In that case they get interpreted as PHP and would likely output a blank/broken page? If the pages output any information (echo, etc.), that information might be a problem. But otherwise there should be no problem.

As for making this the default – I’d actually not prefer files as the default though we might look at making it an option. My guess is that we may look at this for 1.0 but not before…

As for the preferred solution of including files, something like below would actually be best:

<?php include(FROG_ROOT.'/public/layouts/normal.php'); ?>

Or even better..

<?php include('../public/layouts/normal.php'); ?>

And for those who are unfamiliar with the difference between include and require:

  • include() – includes a file, but the script continues executing if it can’t be found.
  • include_once() – includes a file like above, but makes sure it only gets included exactly once.
  • require() – includes a file and halts script execution if the file can’t be found.
  • require_once() – includes a file like above, but makes sure it only gets included exactly once.