Jump to:

23007 Posts in 11866 Topics by 2828 members

General Questions

SilverStripe Forums » General Questions » Question about $TopURLSegment

General questions about getting started with SilverStripe that don't fit in any of the categories above.

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

Page: 1
Go to End
Author Topic: 892 Views
  • micahsheets
    Avatar
    Community Member
    164 Posts

    Question about $TopURLSegment Link to this post

    I know how to use $TopURLSegment, however what I really need at this point is <% if TopURLSegment = %>. I tried to make my own function to do this but I could not figure out how to write it or where to put it.

  • Willr
    Avatar
    Forum Moderator
    5464 Posts

    Re: Question about $TopURLSegment Link to this post

    Sorry if I don't follow your question - you want to do something like

    <% control Top %>
    <% if URLSegment = mypage-url %>
    // do stuff
    <% end_if %>
    <% end_control %>

    ?

  • micahsheets
    Avatar
    Community Member
    164 Posts

    Re: Question about $TopURLSegment Link to this post

    I understand what you are saying, but what I need is more like this

    <% control Top %>
    <% if URLSegment = mypage-url %>

    // Get variable from Top

    <% end_control %>
    <% else %>

    // Get variable from This

    <% end_if %>

  • Aram
    Avatar
    Community Member
    598 Posts

    Re: Question about $TopURLSegment Link to this post

    What do you mean by 'Get variable from top/this'? are you using it in a function on the current page? If so you will need to do this stuff from the controller rather than the template, as you can't pass variables into the controller from the template.

    Alternatively you could have the function on your mypage-url page type and call it from within the control block

  • SalvaStripe
    Avatar
    Community Member
    89 Posts

    Re: Question about $TopURLSegment Link to this post

    you want to get some vars from the pages, right?
    so, did you already created these vars like this options...

    class PageWithVars extends Page {
    static $db = array(
    'Var1' => 'Text',
    'Var2' => 'Text',
    );
    static $has_one = array(
    );
    function getCMSFields() {
    $fields = parent::getCMSFields();

    $fields->addFieldToTab('Root.Content.Main', new TextField('Var1'), 'Content');
    $fields->addFieldToTab('Root.Content.Main', new TextField('Var2'), 'Content');
       
    return $fields;
    }
    }

    class PageWithVars_Controller extends Page_Controller {
    }

    With that you can get the vars in the .ss file with $Var1 and $Var2.
    if your parent page has these vars too, you can get them simple by

    $Parent.Var1
    $Parent.Var2

    #######################
    Other way is to make a function in PageWithVars_Controller
    (My example will get many Vars from a DB, and later you can use in .ss file [could be much helpful for someone.. ;)])

    function MySuperVarFunction() {

       $messages = new DataObjectSet();
          
       $result = DB::query("SELECT * FROM Messages WHERE ToID = '".Member::currentUserID()."' AND Archiv = '0' ORDER BY Date DESC");
       if($result) {
          foreach($result as $value) {
             $Date = date("d.m.Y",$value['Date']);
             $Time = date("H:i",$value['Date']);
             $entries = array(
                            'ID' => $value['ID'],
                            'Title' => $value['Title'],
                            'Message' => nl2br(htmlspecialchars($value['Message'])),
                            'Datum' => $Date,
                            'Uhrzeit' => $Time,
                            'New' => $value['isNew'],
                            'Archiv' => $value['Archiv'],
                            'SendFrom' => $SendFrom,
                            'FID' => $FID
                            );
             $messages->push(new ArrayData($entries));
          }
       }
       return $messages;
    }

    And now you can do cool stuff in the .ss file ;)

    <% control MySuperVarFunction %>
    $Date - $Time<br >
    $Title<br >
    <% if New == 1 %><img src="mysite/images/new.jpg" ><% end_if %><br >
    From <em>$SendFrom</em><br >
    $Message
    <% end_control %>

    SalvaStripe

  • micahsheets
    Avatar
    Community Member
    164 Posts

    Re: Question about $TopURLSegment Link to this post

    In my post when I had "// Get variable from Top " or "// Get variable from This ", those were sudo code for maybe $Title where in <% control Top %> I know it would be the Title of the Top. But while controlling Top I was wondering hour to break out of the control without breaking the if statements. However I found that I can end a control without breaking the if statements.

    <% control Top %>
    <% if foo(bar) %>
    <% end_control %>
    Do stuff
    <% else %>
    Do other stuff
    <% end_if %>

    This way I can get some info about the Top scope but still deal with variables and content from the scope of the current page.

    So in the end I kind of answered my own question, but I appreciate and learn from all the comments anyways.

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