Some examples of “light” content management systems:
What they have in common is that you just add a specific classname to the parts of your webpage that you want to make editable.
In the backend they somehow save the edited snippets back to the file you edited (no database).
Example of original content and edited content:
Original HTML code
<div class="editable">
<ul>
<li>an</li>
<li>unordered list</li>
</ul>
</div>
Edited HTML code
<div class="editable">
<ol>
<li>an</li>
<li>ordered list</li>
</ol>
</div>
The frontend is pretty clear to me but how would I best go about the backend (PHP)?
I need to make sure that dynamic PHP code keeps working, so I can’t just update the complete file.
Any resources or pointers are very welcome since I have searched for this technique but couldn’t find any in depth ideas or methods of doing this.
The light content management systems listed above aren’t open source so learning from the source is not an option either.
EDIT
I stated that I don’t want to use PHP DOM but things have changed. Both Surreal CMS and CushyCMS are using it, which is a good indicator it is the way to go.
The problem I am experiencing with PHP DOM is that I can’t replace only the part of the original document the user edited.
When loading the original file, PHP DOM will do unwanted things to the document like changing the meta tag, converting single quotes to double quotes, …
Now, for the edited part this is fine, but it is unacceptable for the rest of the document since that should remain untouched (the user didn’t edit this, right).
Cushy and Surreal both seem to know how to stop PHP DOM from touching the rest of the document but I don’t. So if you know, let me know.
You question is a little vague, but maybe this will help you:
Part A: Editing
To edit your lightweight pages, you have to inject some javascript into the page. This javascript searches for all elements with “editable” class and sets them to editable (have a look at the contentEditable property for this as a very basic starting point).
Keep some identification of the edited element, for example the id, the xpath or the index. To save the texts, just ajax them to your server.
Part B: Saving to the file
You might store the pieces in a database, but I assume you want to change the file directly. To do this, you just replace the content of each of the elements with what you received via AJAX. To find the elements in the sourcecode, you will have to use something like PHPs SimpleDOM (this also implies that the HTML pages are valid XHTML).
Just find the element, write the changes, write the file, done.
Note, however, that things will become much more complicated if you want to make it work for real projects.
EDIT: just saw your “PHP code has to keep on working” part of the question. Consider some regular expression magic for this one – replace each
/(<?.*?>)/with a text placeholder (for example “-PHPCODE-$i-“) and after updating the dom tree, replace these placeholders back. Either way, a hard job.