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.

General Questions

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

Variable within inline style tag


Reply

5 Posts   1554 Views

Avatar
vegetav

10 November 2010 at 11:02pm Community Member, 23 Posts

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?

Avatar
simon_w

11 November 2010 at 12:04am Forum Moderator, 474 Posts

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.

Avatar
vegetav

11 November 2010 at 5:19am Community Member, 23 Posts

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

Avatar
Invader_Zim

11 November 2010 at 7:20am (Last edited: 11 November 2010 7:21am), Community Member, 141 Posts

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

Avatar
vegetav

17 November 2010 at 1:55am Community Member, 23 Posts

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