Jump to:

3435 Posts in 1026 Topics by 866 members

Template Questions

SilverStripe Forums » Template Questions » Control based upon conditional operator

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

Page: 1
Go to End
Author Topic: 1815 Views
  • jondbaker
    Avatar
    Community Member
    19 Posts

    Control based upon conditional operator Link to this post

    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 %>

  • CodeGuerrilla
    Avatar
    Community Member
    105 Posts

    Re: Control based upon conditional operator Link to this post

    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.

    1815 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.