Why Gemfury? Push, build, and install  RubyGems npm packages Python packages Maven artifacts PHP packages Go Modules Bower components Debian packages RPM packages NuGet packages

sleewoo / el   ruby

Repository URL to install this package:

/ docs / TagFactory.md

Generic Tags

Espresso Lungo comes with a set of useful helpers aimed at generating HTML in plain Ruby:

div_tag 'some text'
#=> <div>some text</div>

HTML attributes can be passed via options Hash:

h1_tag 'Welcome!', class: 'well'
#=> <h1 class="well">Welcome!</h1>

Content can be provided via block:

script_tag do
  // some js here
end
#=> <script>
#=>   // some js here
#=> </script>

Important! Any content, provided via first argument or block, will be HTML escaped! To emit content as is, use bang methods and MAKE SURE you do not pass any untrusted input into HTML helpers!

Default Behavior:

div_tag 'some <evil> string'
#=> <div>some &lt;evil&gt; string</div>

Bang helpers wont escape output! Use with care!

div_tag! 'some <evil> string'
#=> <div>some <evil> string</div>

Though bang methods are mostly dangerous, they are necessary when using nested tags:

div_tag! do # without bang <b> tag will be escaped
  b_tag 'some <evil> string'
end
#=> <div><b>some &lt;evil&gt; string</b></div>

The main concern when using nested tags is to not mix helpers with any untrusted input.

Note: when using multiple tags you have to concat them, otherwise only the last one will be shown:

Wrong:

form_tag! do
  input_tag(name: :name)
  input_tag(name: :email)
end
#=> <form><input name="email"></form>

Correct:

form_tag! do
  input_tag(name: :name) +  # or <<
  input_tag(name: :email)
end
#=> <form><input name="name"><input name="email"></form>

[ contents ↑ ]

link_to

link_to allow to build a HTML <a> tag.

If first param is a valid action, the URL of given action will be used.

Action accepted as a symbol or a string representing action name and format.

Action can also be passed in deRESTified form, eg. :read instead of :post_read

class App < E
  format '.html'

  def read
    link_to :read        #=> <a href="/app/read" ...
    link_to 'read.html'  #=> <a href="/app/read.html" ...
    link_to 'read.xml'   #=> <a href="read.xml" ... - not translated, used as is
  end

  def post_write
    link_to :post_write  #=> <a href="/app/write" ... - works but it is tedious, use :write instead
    link_to :write       #=> <a href="/app/write" ...
    link_to 'write.html' #=> <a href="/app/write.html" ...
    link_to '/something' #=> <a href="/something" ... - not translated, used as is
  end
end

When you need to create a link to an arbitrary controller, use base_url, route or [] of target controller:

  class News < E
    format '.html'

    def home
      # ...
    end

    def get_read id = nil
      # ...
    end

  end

  link_to News.base_url      #=> <a href="/news" ...
  link_to News[:home]        #=> <a href="/news/home" ...
  link_to News['home.html']  #=> <a href="/news/home.html" ...
  link_to News[:get_read]    #=> <a href="/news/read" ...
  link_to News[:read]        #=> <a href="/news/read" ...
  link_to News['read.html']  #=> <a href="/news/read.html" ...

  link_to News.route(:read, 100)         #=> <a href="/news/read/100" ...
  link_to News.route(:read, '100.html')  #=> <a href="/news/read/100.html" ...

If nil passed as first argument, a void link will be created:

link_to nil, 'something'
#=> <a href="javascript:void(null);>something</a>

Anchor can be passed via second argument.

If it is missing, the link will be used as anchor:

link_to :something
#=> <a href="/something">/something</a>

link_to :foo, 'bar'
#=> <a href="/foo">bar</a>

Anchor can also be passed as a block:

link_to(:foo) { 'bar' }
#=> <a href="/foo>bar</a>

Important! Anchor will be HTML escaped:

link_to(:foo) { 'some <evil>' }
#=> <a href="/foo>some &lt;evil&gt;</a>

To emit content as is, use bang method instead and make sure you do not pass any untrusted content to link_to helper:

link_to! :foo, 'some <evil>'
#=> <a href="/foo>some <evil></a>

Also you'll need bang helper when you build nested tags:

link_to! :foo do
  b_tag 'some text'
end
#=> <a href="/foo><b>some text</b></a>

Attributes can be passed as a hash via last argument:

link_to :foo, target: '_blank'
#=> <a href="/foo" target="_blank">/foo</a>

link_to :foo, :bar, target: '_blank'
#=> <a href="/foo" target="_blank">bar</a>

[ contents ↑ ]