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're retiring the forums!

The SilverStripe forums have passed their heyday. They'll stick around, but will be read only. We'd encourage you to get involved in the community via the following channels instead:

Template Questions /

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

Encapsulate variables in Templates


Go to End


5 Posts   1238 Views

Avatar
MonkeyFish

Community Member, 12 Posts

21 September 2011 at 10:16am

I have a field in the database called $RowOffset which is an Integer and I want to use it in an embedded style like this:

<div class="seatsrow" style="margin-left:$RowOffsetpx">

However it doesn’t work as it sees it as a variable called $RowOffsetpx instead of $RowOffset for obvious reasons.

Desired result is

<div class="seatsrow" style="margin-left:20px">

This doesn’t work because of the space:

<div class="seatsrow" style="margin-left:$RowOffset px">

Is there a way to encapsulate the variable say like this:

<div class="seatsrow" style="margin-left:_v($RowOffset)px">

Hope that makes sense. :)

Avatar
Ryan M.

Community Member, 309 Posts

21 September 2011 at 11:05am

Edited: 21/09/2011 11:05am

You could try putting it in brackets, such as {$Variable}. If that doesn't work, why not create a function in your dataobject file and concatenate the strings together there? Like so:

public function GetRowOffset() {
return $this->RowOffset . 'px';
}

Then you could use $GetRowOffset within your control loop.

Avatar
Willr

Forum Moderator, 5523 Posts

21 September 2011 at 5:04pm

Like Ryan Said, {$Foo}px should work.

public function GetRowOffset() { 
return $this->RowOffset . 'px'; 
}

PROTIP: name it getRowOffset then you can use the slightly nicer string $RowOffset in the template.

Avatar
MonkeyFish

Community Member, 12 Posts

26 September 2011 at 12:37pm

Chur :)

Avatar
radu706

Community Member, 1 Post

18 December 2011 at 3:43pm

You can if using Escaping

<div class="seatsrow" style="margin-left:{$RowOffset}px">