Storing hierarchical template into a database

Posted by pduersteler on Programmers See other posts from Programmers or by pduersteler
Published on 2011-08-16T18:02:22Z Indexed on 2011/11/28 10:39 UTC
Read the original article Hit count: 242

If this title is ambiguous, feel free to change it, I don't know how to put this in a one-liner.

Example:
Let's assume you have a html template which contains some custom tags, like <text_field />. We now create a page based on a template containing more of those custom tags. When a user wants to edit the page, he sees a text field. he can input things and save it.

This looks fairly easy to set up. You either have something like a template_positions table which stores the content of those fields.

Case:
I now have a bit of a blockade keeping things as simple as possible. Assume you have the same tag given in your example, and additionally, <layout> and <repeat> tags. Here's an example how they should be used:

<repeat>
  <layout name="image-left">
    <image /> <text_field />
  </layout>

  <layout name="image-right">
    <text_field /> <image />
  </layout>
</repeat>

We now have a block which can be repeated, obviously. This means: when creting/editing a page containing such a template block, I can choose between a layout image-left and image-right which then gets inserted as content element (where content for <image /> and <text_field /> gets stored). And because this is inside a <repeat>, content elements from the given layouts can be inserted multiple times.

How do you store this? Simply said, this could be stored with the same setup I've wrote in the example above, I just need to add a parent_id or something similiar to maintain a hierarchy. but I think I am missing something. At least the relation between an inserted content element and the origin/insertion point is missing. And what happens when I update the template file?

Do I have to give every custom tag that acts as editable part of a template an identifier that matches an identifier in the template to substitue them correctly?

Or can you think of a clean solution that might be better?

© Programmers or respective owner

Related posts about best-practices

Related posts about recursion