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.

General Questions /

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

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

Variable within inline style tag


Go to End


5 Posts   2870 Views

Avatar
vegetav

Community Member, 23 Posts

10 November 2010 at 11:02pm

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
(deleted)

Community Member, 473 Posts

11 November 2010 at 12:04am

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

Community Member, 23 Posts

11 November 2010 at 5:19am

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

Community Member, 141 Posts

11 November 2010 at 7:20am

Edited: 11/11/2010 7:21am

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

Community Member, 23 Posts

17 November 2010 at 1:55am

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