Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

All other Modules /

Discuss all other Modules here.

Moderators: martimiz, Sean, biapar, Willr, Ingo, swaiba, simon_w

How to list/display tags (TagField) in template?


Reply


1255 Views

Avatar
vwd

Community Member, 160 Posts

19 April 2011 at 9:49am

Hi,

I am using the TagField module to incorporate tags into data an SS site.

Outcome I'd like: iterate through tags, listing them in my template...

<div class="items'>
<h2>Item 1</h2>
<ul class="tags">
<li>tag 1 item 1</li>
<li>tag 2 item 2</li>
</ul>
</div>
<div class="items'>
<h2>Item 2</h2>
<ul class="tags">
<li>tag 1 item 2</li>
<li>tag 2 item 2</li>
</ul>
</div>

The data model....

class MyItem extends DataObject
{
static $db = array (
'ItemName' => 'Text',
'Tags' => 'Text'
);

static $has_one = array (
'MyItemPage' => 'MyItemPage'
);

   public function getCMSFields_forPopup()
   {
$tf = new TagField('Tags', null, null, 'MyItem');
$tf->setSeparator(',');
return new FieldSet(
new TextField('ItemName'),
$tf
);
}
}

The template that I envisaged (not sure if this is the best approach)...

<% control MyItems %>
<div class="items>
<h2>$ItemName </h2>
<ul class="tags">
<% control TagList %>
<li>$Value</li>
<% end_control %>
</ul>
<% end_control %>

I realise that the 'model' (MyItem) basically has the tag list stored as a single string/text entry. And TagField is the field type is used to store a list of created tags in the DB and assist with auto-suggestion of tags etc.

What is the smartest/proper way of implementing the functionality above (iterating through individual tags)? It didn't feel right to re-implement the string tokenizing logic (from TagField.php) in the page controller. So how would I go about achieving this functionality please?

Thank you very much.
VWD.