Action View: Templates

25Sep09

(Notes from Chapter 23 of “Agile Web Development with Rails, Third Edition”, sections 23.1 to 23.3)

Template Environment: you have access to the following:

  • all of the controller’s instance variables – useful for communicating data from the action to the template
    • this includes flash
    • can access the current controller with the attribute controller – any public methods in the controller are thus accessible
  • base_path contains the path to the base directory to templates

Template types:

  1. builder – use the Builder library to construct XML responses
  2. ERb (embedded ruby) – HTML, mixed in with some Ruby
  3. rjs – special Ruby JavaScript

ERb templates

  • use <% … %> to execute Ruby code
  • use <%= … %> to output the result of Ruby code
  • the method html_escape, or h for short, escapes text for rendering in the template
  • the sanitize method cleans up dangerous HTML such as <form> and <script> tags, on= attributes and links starting with javascript:

Helpers: a module containing methods that assist all the views for a specific controller

  • a controller named ResultsController would have a corresponding helper module called ResultsHelper in the file results_helper.rb in the app/helpers directory
  • all views for this controller then have access to all of the public methods in this module
  • list of useful formatting helpers pp. 512-4

Page Linking

  • modules ActionView::Helpers::AssetTagHelper and ActionView::Helpers::UrlHelper contain useful methods for this purpose
  • link_to creates a link to another action, with the following parameters:
    1. text to display for the link
    2. hash specifying the link’s target (same as the url_for method) – :action, :id, etc.
    3. hash of three options:
      1. :class is the HTML class of the anchor tag
      2. :confirm, if present, will use JavaScript to display the message and require the user’s confirmation for the link to be followed
      3. :method – hack, don’t use this
    4. :popup can be the value true or an array of window creation options [first the window name passed to JavaScript’s window.open and the second is the option string]
  • button_to generates a button in a self-contained form instead of a link, but other than that, its parameters are the same as link_to‘s parameters
  • link_to_if and link_to_unless generate links if the condition is met and otherwise just return the link text. Their first parameter is the condition
  • link_to_unless_current is useful for creating menus in sidebars with the current page name in plain text and the others as hyperlinks
  • absolute URLs such as https://taraclark.wordpress.com can also be included in instead of the hash specifying the link’s target

Resource Linking

  • image_tag creates <img> tags with parameters and it can be used as the “text” portion of a link_to call:
    1. image filename – if it doesn’t start with a slash, then it is assumed to be in the images directory
    2. :class
    3. :size in the form “widthxheight” or :width and :height
    4. :alt or generated based on the image filename
  • mail_to creates a mailto: hyperlink
    1. e-mail address
    2. name of the link
    3. :subject, :cc, :bcc, :body
    4. :encode => “javascript” uses JavaScript to obscure the generated link
  • stylesheet_link_tag links to a stylesheet file
    1. filename without the .css
    2. :media => “all”
    3. :all => true would include all styles in the stylesheets directory
  • javascript_include_tag – list of JavaScript filenames living in public/javascripts
Advertisements


No Responses Yet to “Action View: Templates”

  1. Leave a Comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


%d bloggers like this: