Jump to:

3435 Posts in 1026 Topics by 866 members

Template Questions

SilverStripe Forums » Template Questions » Encapsulate variables in Templates

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

Page: 1
Go to End
Author Topic: 696 Views
  • MonkeyFish
    Avatar
    Community Member
    12 Posts

    Encapsulate variables in Templates Link to this post

    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.

  • Ryan M.
    Avatar
    Community Member
    309 Posts

    Re: Encapsulate variables in Templates Link to this post

    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.

  • Willr
    Avatar
    Forum Moderator
    5489 Posts

    Re: Encapsulate variables in Templates Link to this post

    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.

  • MonkeyFish
    Avatar
    Community Member
    12 Posts

    Re: Encapsulate variables in Templates Link to this post

    Chur

  • radu706
    Avatar
    Community Member
    1 Post

    Re: Encapsulate variables in Templates Link to this post

    You can if using Escaping

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

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