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.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

Template Questions /

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

Control based upon conditional operator


Go to End


2 Posts   2629 Views

Avatar
jondbaker

Community Member, 19 Posts

22 December 2009 at 9:34am

Within my template, I'm trying to loop display individual Links organized by their respective LinkCategories. Do I have my model set up completely wrong to achieve this? As it is, I can't get the template to output any data.

LinkHolder.php

<?php
class LinkHolder extends Page {
    static $singular_name = 'LinkHolder';
    static $plural_name = 'LinkHolders';
    static $has_many = array(
        'Links' => 'Link'
    );
    function getCMSFields() {
        $fields = parent::getCMSFields();
        $misAmigosTable = new ComplexTableField($this, 'Links', 'Link');
        $fields->addFieldToTab('Root.Content.Mis Amigos', $misAmigosTable);
        return $fields;
    }
}
class LinkHolder_Controller extends Page_Controller {}
?>

Link.php

<?php
class Link extends DataObject {
    static $singular_name = 'Link';
    static $plural_name = 'Links';
    static $db = array(
        'LinkURI' => 'Text',
        'LinkDisplay' => 'Text',
        'LinkCategory' => 'Varchar(100)'
    );
    static $has_one = array(
        'LinkHolder' => 'LinkHolder'
    );
    function getCMSFields() {
        $categories = array(
            'MisAmigos' => 'Mis Amigos'
        );
        $fields = new FieldSet(
            new TextField('LinkURI', 'Link URI'),
            new TextField('LinkDisplay', 'Link Display Text'),
            new DropdownField('LinkCategory', 'Link Category', $categories)
        );
        return $fields;
    }
}
?>

LinkHolder.ss

                <% if Links %>
                    <% if LinkCategory == MisAmigos %>
                        <ul id="mis-amigos" class="links">
                        <h5>$LinkCategory</h5>
                        <% control Links %>
                            <li><a href="$LinkURI">$LinkDisplay</a></li>
                        <% end_control %>
                        </ul>
                    <% end_if %>
                <% end_if %>

Avatar
CodeGuerrilla

Community Member, 105 Posts

14 January 2010 at 8:03pm

I would do this in the LinkHolder_Controller by making a custom methods

Links.php
Change Category to has_one relationship

Create LinkCategory to extend DataObject have a look at ModelAdmin to manage your links

LinkHolder_Controller.php
create functions to retrieve categories and links

LinkHolder.ss
control the functions you created and output in the desired way

Sorry I couln't provide any code at the moment as I was suppose to leave work 30misn ago let me know if you need a better explaination.