Forums / Setup & design / Code to automatically list content object attribute names and data

Code to automatically list content object attribute names and data

Author Message

Fraser Hore

Thursday 01 April 2004 8:52:28 am

I would like to set up a template that will fetch the child nodes and then display a table showing the content of the data map. The trick is, I would like this template to work for all classes, even custom ones. This way a user can create a class for themselves (e.g. Contacts), create a folder for objects of that class (e.g. Contact List), populate the folder with Contacts content objects, and then surf to their folder to see a list of their contacts.

I know this can be set up manually by fetching the child nodes and then creating a table with stuff like
<td>
$ContactLoop:item.data_map.first_name.content
</td>
<td>
$ContactLoop:item.data_map.first_name.content
</td>

But is there a way to design the template so you get the same result without knowing in advance what the content attributes will be?

I've figured out that i can fetch the class attributes using the class id to create the column headings. But I'm stumped on printing the data.

Ultimately I'd like to set up a "view" class to gather view parameters like which fields (or attributes) to show, how to sort, filter etc. Then the user could define their own views by adding "view" content objects in the same node as the content that they want to see, and the template could have a menu of these views. Clicking on the view link brings up a template that shows all "Contacts" in the same node.

Any suggestions would be greatly appreciated.

Fraser Hore

Tuesday 06 April 2004 1:48:13 pm

I figured out how to have the template automatically create a table that lists the class attribute names across the first row and then lists the content of those attributes for all children in subsequent rows. The child name in the first column links to the view of the child.

Next step is to create a class where the user can set parameters to control the way the list is displayed (e.g. attributes to include, filters etc.). It would also be great if the column headings could be clicked to sort by that attribute. If anyone wants to take a stab at these enhancements it would be a great help. The end result will be pretty powerful.

Here's the code:

{let attribute_content=fetch( content, list, hash( parent_node_id, $node.node_id))}
{section var=class loop=$attribute_content max=1}
{let attribute_name=fetch( 'content', 'class_attribute_list', hash( 'class_id', $class.item.object.contentclass_id ) )}

<table border="1">

<tr>
{section var=heading loop=$attribute_name}
<td>
{$heading.item.name|wash}
</td>
{/section}
</tr>

<tr>
{section var=record loop=$attribute_content}
{section var=attribute_name_link loop=$attribute_name max=1}
{let data=$attribute_name_link.item.identifier|wash}
<td>
<a href={concat("/content/view/full/",$record.item.node_id)|ezurl}>
{attribute_view_gui attribute=$record.item.object.data_map.$data}
</a>
</td>
{/let}
{/section}

{section var=attribute_variable loop=$attribute_name offset=1}
{let data=$attribute_variable.item.identifier|wash}
<td>
{attribute_view_gui attribute=$record.item.object.data_map.$data}
</td>
{/let}
{/section}
</tr>
{/section}

</table>

{/let}
{/section}
{/let}

For some reason {attribute_view_gui attribute=$record.item.object.data_map.$attribute_variable.item.identifier|wash} didn't work so I had to assign the $data variable. Also, at the moment it will only work for the first class type it comes across so it assumes that the folder only has one type of child class.

Cheers,

Fraser

Fraser Hore

Monday 12 April 2004 4:56:45 am

I would be grateful for some help with the attribute filter parameter. I've used the code:

{let attribute_content=fetch( content, list, hash( parent_node_id, $parent_node.node_id, 'attribute_filter', array('or', array('folder/description', '=', 'test'))))}

I have a child folder with 'test' in the description attribute. I am getting the full list of children and not just the child folder where description = test.

Any suggestions?

Thanks in advance

Fraser Hore

Monday 12 April 2004 5:17:33 am

I am using a section to loop through attribute names. I created a variable for the array of values to loop through called attribute_name. I can manually set my identifiers in the array like this:

{let attribute_name=array(Name, Description, Image)}

Then loop through and print the names like this:

{section var=heading loop=$attribute_name}
{$heading.item}</br>
{/section}

So far so good. But I want values in a text_line attribute called 'attribute_name_array' as the array. In this attribute I have entered; Name, Description.

Now I define the attribute_name variable as:

{let attribute_name=array($node.data_map.attribute_name_array.content)}

The output from this is 'Name, Description' all together rather than seperated (i.e. it's not looping through 'Name' then 'Description'.

I then added implode(', ') to try to separate them;

{let attribute_name=array($node.data_map.attribute_name_array.content)|implode(', ')}

and it separates each character rather than seperating at the ', ' delimiter.

Any suggestions would be greatly appreciated.