Jump to:

23489 Posts in 18996 Topics by 2878 members

General Questions

SilverStripe Forums » General Questions » Variable within inline style tag

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: 1522 Views
  • vegetav
    Avatar
    Community Member
    23 Posts

    Variable within inline style tag Link to this post

    I'm trying to do a simple loop but and require to have an inline style tag setting the top and left properties of the div. The problem I have is Silverstripe doesn't like my variables in the style tag, my code is as follows...

    <% control Offices %>
       <div class="location" style="left: $Left px;top: $Top px;">
          <div class="location-inner">
             <div class="address-wrapper">
                <div class="address">
                   $Address<br/>
                   T: $Tel<br/>
                   F: $Fax<br/>
                   M: $Mob<br/>
                   E: $Email<br/><br/>
                   Contact: $Person
                </div>
             </div>
             <div class="house"></div>
          </div>
       </div>
    <% end_control %>

    Does anyone know how I can get around this problem?

  • simon_w
    Avatar
    Forum Moderator
    473 Posts

    Re: Variable within inline style tag Link to this post

    I would say it's the variable $Top that SilverStripe doesn't like. Try renaming it and seeing if that works.

    This is because $Top is a reserved variable that returns you to the top when you're inside controls.

  • vegetav
    Avatar
    Community Member
    23 Posts

    Re: Variable within inline style tag Link to this post

    Hi Simon, thanks for the advice... I changed teh variable name do $Down, but it seemed the main problem was that $Down returns my value, but the browser won't recognise it unless it's next to the px, for exampple

    style="top: $Down px" outputs... style="top: 250 px" which firefox renderes as style=""

    The trick is getting silverstripe to output style="top: 250px" (without the space), but of course putting style="$Downpx" in the tempalte isn't going to work. Which brings me onto my next problem...

    I have a contact(Contact.php) which has_many offices (Office.php)
    I wrote a function to get the map location of each office, which is in my contact controller...

       function officeLocation() {
          $office = DataObject::get_by_id('Office', $This->ID);
          $style = "left: " . $office->Left . "px; top: " . $office->Down . "px;";
          return $style;
       }

    Which works when just added into the tempalte, but if I add it inside my <% control Offices %> tag (which is where it needs to be) it can't seem to call my funtion, and if I move the function into my Office.php it still doesnt work.

    This is the setup I am looking to acheive...

          <% control Offices %>
             <div class="location" style="$officeLocation">
                <div class="location-inner">
                   <div class="address-wrapper">
                      <div class="address">
                         $Address<br/>
                         T: $Tel<br/>
                         F: $Fax<br/>
                         M: $Mob<br/>
                         E: $Email<br/><br/>
                         Contact: $Person
                      </div>
                   </div>
                   <div class="house"></div>
                </div>
             </div>
          <% end_control %>

  • Invader_Zim
    Avatar
    Community Member
    141 Posts

    Re: Variable within inline style tag Link to this post

    Hi vegetav.

    Try this in your template:

    style="top: {$Down}px"

    I think wrapping it in (forgot the english name for these things ->{} ) should fix your styling issue.

    Cheers,
    Christian

  • vegetav
    Avatar
    Community Member
    23 Posts

    Re: Variable within inline style tag Link to this post

    Thanks Invader_Zim... that worked a treat. Legend!

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