Database Design – Storing Hierarchical Templates in a Database

recursiontemplates

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?

Best Answer

for your reference you can see Active directory implementation it is pretty much the same thing. but i would prefer you must use hierarchical database for this purpose.

you problem has solution is in No-SQL database. there are many databases with key value store schema under no-SQL umbrella. Relational databases are not design for this purpose although you can use them by having parent child relationships, insertion is fine but we will face difficulties while retrieving data.

if there is no restriction on changing the database you must go with that. but if you have no option other than SQL DB as you might have been using SQL for relational purposes it would be cumbersome to have separate DB base for this purpose only.

but again don't use SQL DB for this purpose.

Related Topic