Jump to:

11001 Posts in 2733 Topics by 1823 members

All other Modules

SilverStripe Forums » All other Modules » How to list/display tags (TagField) in template?

Discuss all other Modules here.

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

Page: 1
Go to End
Author Topic: 1228 Views
  • vwd
    Avatar
    Community Member
    158 Posts

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

    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.

    1228 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.