SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Member
    Join Date
    Jun 2007
    Posts
    8
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question Setting tag attribute value in the template

    Hello everyone,

    Say I want to output something like this in the template

    Code:
    <td with="some_number">Some text</td>
    I need to set "some_number" programmatically. How can I do it? I tried
    Code:
    <td with="<%= some_variable %>">Some text</td>
    but this doesn't work.

    Any help would be appreciated.

  2. #2
    Non-Member eautocad's Avatar
    Join Date
    Jul 2008
    Posts
    154
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You would need to write a randomization script to fill the td variable
    Last edited by r937; Jul 27, 2008 at 02:52.

  3. #3
    SitePoint Member
    Join Date
    Jun 2007
    Posts
    8
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi eautocad,

    What do you mean by "randomization script"? The value i need to put into width is not random, it should come from a variable. My question is about template syntax for putting the ruby expression inside HTML attribute. E.g. if i had to write this value between <td></td> tags i would write

    Code:
    <td><%= some_value %></td>
    But how do i write it if i need to put the value inside the attribute of a tag?

    Thanks

  4. #4
    SitePoint Member
    Join Date
    Jul 2008
    Location
    UK
    Posts
    17
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The code you had to start with was correct, but some_variable would need to be set somewhere. Which is normally the controller, in which case you'd need to set it as an instance variable to allow it to be accessed outside of the controller class, @some_variable.

    example_controller.rb:

    Code Ruby:
    def index
      @some_variable = "class-one"
    end

    index.html.erb:

    Code HTML:
    <div class="<%= @some_variable %>">This is a div</div>

    Source code in browser:

    Code HTML:
    <div class="class-one">This is a div</div>


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •