/scalate-jruby/src/main/resources/haml-3.0.25/doc-src/FAQ.md

http://github.com/scalate/scalate · Markdown · 187 lines · 135 code · 52 blank · 0 comment · 0 complexity · 2ced383b5d367cc92ca943c4a5355ca4 MD5 · raw file

  1. # Frequently Asked Questions
  2. * Table of contents
  3. {:toc}
  4. ## Haml
  5. ### Why is my markup indented properly in development mode, but not in production?
  6. {#q-indentation-in-production}
  7. To improve performance, Haml defaults to {file:HAML_REFERENCE.md#ugly-option "ugly" mode} in Rails
  8. apps running in production.
  9. ### How do I put a punctuation mark after an element, like "`I like <strong>cake</strong>!`"?
  10. {#q-punctuation}
  11. Expressing the structure of a document
  12. and expressing inline formatting are two very different problems.
  13. Haml is mostly designed for structure,
  14. so the best way to deal with formatting is to leave it to other languages
  15. that are designed for it.
  16. You could use Textile:
  17. %p
  18. :textile
  19. I like *cake*!
  20. or Markdown:
  21. %p
  22. :markdown
  23. I like **cake**!
  24. or plain old XHTML:
  25. %p I like <strong>cake</strong>!
  26. If you're inserting something that's generated by a helper, like a link,
  27. then it's even easier:
  28. %p== I like #{link_to 'chocolate', 'http://franschocolates.com'}!
  29. ### How do I stop Haml from indenting the contents of my `pre` and `textarea` tags?
  30. {#q-preserve}
  31. Because Haml automatically indents the HTML source code,
  32. the contents of whitespace-sensitive tags like `pre` and `textarea`
  33. can get screwed up.
  34. The solution is to replace the newlines inside these tags
  35. with HTML newline entities (`&#x000A;`),
  36. which Haml does using the {Haml::Helpers#preserve} and {Haml::Helpers#find_and_preserve} helpers.
  37. Normally, Haml will do this for you automatically
  38. when you're using a tag that needs it
  39. (this can be customized using the {file:HAML_REFERENCE.md#preserve-option `:preserve`} option.
  40. For example,
  41. %p
  42. %textarea= "Foo\nBar"
  43. will be compiled to
  44. <p>
  45. <textarea>Foo&#x000A;Bar</textarea>
  46. </p>
  47. However, if a helper is generating the tag,
  48. Haml can't detect that and so you'll have to call {Haml::Helpers#find_and_preserve} yourself.
  49. You can also use `~`, which is the same as `=`
  50. except that it automatically runs `find_and_preserve` on its input.
  51. For example:
  52. %p= find_and_preserve "<textarea>Foo\nBar</textarea>"
  53. is the same as
  54. %p~ "<textarea>Foo\nBar</textarea>"
  55. and renders
  56. <p><textarea>Foo&#x000A;Bar</textarea></p>
  57. ### How do I make my long lines of Ruby code look nicer in my Haml document?
  58. {#q-multiline}
  59. Put them in a helper or your model.
  60. Haml purposefully makes it annoying to put lots of Ruby code into your templates,
  61. because lots of code doesn't belong in the view.
  62. If you take that huge `link_to_remote` call
  63. and move it to a `update_sidebar_link` helper,
  64. it'll make your view both easier to read and more semantic.
  65. If you absolutely must put lots of code in your template,
  66. Haml offers a somewhat awkward multiline-continuation tool.
  67. Put a `|` (pipe character) at the end of each line you want to be merged into one
  68. (including the last line!).
  69. For example:
  70. %p= @this.is(way.too.much). |
  71. code("and I should"). |
  72. really_move.it.into( |
  73. :a => @helper) |
  74. Note that sometimes it is valid to include lots of Ruby in a template
  75. when that Ruby is a helper call that passes in a lot of template information.
  76. Thus when a function has lots of arguments,
  77. it's possible to wrap it across multiple lines
  78. as long as each line ends in a comma.
  79. For example:
  80. = link_to_remote "Add to cart",
  81. :url => { :action => "add", :id => product.id },
  82. :update => { :success => "cart", :failure => "error" }
  83. ### `form_for` is printing the form tag twice!
  84. Make sure you're calling it with `-`, not `=`.
  85. Just like in ERB, you have to do
  86. <% form_for stuff do %>
  87. ...
  88. <% end %>
  89. in Haml, you have to do
  90. - form_for stuff do
  91. ...
  92. ### I have Haml installed. Why is Rails (only looking for `.html.erb` files | rendering Haml files as plain text | rendering Haml files as blank pages)?
  93. {#q-blank-page}
  94. There are several reasons these things might be happening.
  95. First of all, make sure that Haml really is installed;
  96. either you've loaded the gem (via `config.gem` in Rails 2.3 or in the Gemfile in Rails 3),
  97. or `vendor/plugins/haml` exists and contains files.
  98. Then try restarting Mongrel or WEBrick or whatever you might be using.
  99. Finally, if none of these work,
  100. chances are you've got some localization plugin like Globalize installed.
  101. Such plugins often don't play nicely with Haml.
  102. Luckily, there's usually an easy fix.
  103. For Globalize, just edit `globalize/lib/globalize/rails/action_view.rb`
  104. and change
  105. @@re_extension = /\.(rjs|rhtml|rxml)$/
  106. to
  107. @@re_extension = /\.(rjs|rhtml|rxml|erb|builder|haml)$/
  108. For other plugins, a little searching will probably turn up a way to fix them as well.
  109. ## Sass
  110. ### Can I use a variable from my controller in my Sass file?
  111. {#q-ruby-code}
  112. No. Sass files aren't views.
  113. They're compiled once into static CSS files,
  114. then left along until they're changed and need to be compiled again.
  115. Not only don't you want to be running a full request cycle
  116. every time someone requests a stylesheet,
  117. but it's not a great idea to put much logic in there anyway
  118. due to how browsers handle them.
  119. If you really need some sort of dynamic CSS,
  120. you can define your own {Sass::Script::Functions Sass functions} using Ruby
  121. that can access the database or other configuration.
  122. *Be aware when doing this that Sass files are by default only compiled once
  123. and then served statically.*
  124. If you really, really need to compile Sass on each request,
  125. first make sure you have adequate caching set up.
  126. Then you can use {Sass::Engine} to render the code,
  127. using the {file:SASS_REFERENCE.md#custom-option `:custom` option}
  128. to pass in data that {Sass::Script::Functions::EvaluationContext#options can be accessed}
  129. from your Sass functions.
  130. ## You still haven't answered my question!
  131. Sorry! Try looking at the [Haml](http://haml-lang.com/docs/yardoc/HAML_REFERENCE.md.html)
  132. or [Sass](http://sass-lang.com/docs/yardoc/file.SASS_REFERENCE.html) references,
  133. If you can't find an answer there,
  134. feel free to ask in `#haml` on irc.freenode.net
  135. or send an email to the [mailing list](http://groups.google.com/group/haml?hl=en).