Is there an equivalent to Smarty's {strip} in erb?
To clarify:
I'm not looking for .strip. Read the docs on Smarty's {strip};
Whitespace between tags is significant and matters very much when you try to match widths and so on. For example, if you have this code:
<ul>
<li>Something</li>
<li>Something else</li>
</ul>
and the lis have display: inline, there will be a space between them even if they have no margin. That space appears because of the whitespace between </li> and <li>. So, the only solution to not have that space between the <li>s is to do this:
<ul><li>Something</li><li>Something else</li></ul>
Which is pretty fugly and you end up with huge, unreadable lines when you start putting <a>s in the <li>s and so on.
There is always the "traditional" method of putting spaces inside the tags (still valid XML):
<ul
><li>bla</li
><li>bla</li
></ul>
You have the following options
in Haml you can do with > and <
in ERB the ERB::Compiler::TrimScanner maybe could help you out, but I've never used it. As far as I understand, it would look like this: <%w capture do %>
here comes your code with whitespaces
<% end %>
Related
I'm trying to use haml on RoR.
I faced some problems in haml.
My sample code is below.
ERB:
<p> <div class="hello"> <%= #sample.val %> <%= #hogehoge.val %> </div> </p>
HAML:
%p
.hello
= #sample.val
= #hogehoge.val
I can write one line in erb file.
But, I have to write 4 lines in haml.
Haml is so smart and beautiful syntacx structure, isn't it?
But I think writing so many lines is not smart.
How do I write in one line in haml in this case.
If you don't like the way haml work, why do you use it? that's how haml work. If you want something different you need something different, there are more template languages.
You complain about the amount of line but think about the amount of characters and readability, maintainability. The amout of line is one of the less important things on your code, or do you write all your html code in one line when you use erb or plain html?
You could save a few line though doing:
%p
.hello= "#{#sample.val} #{#hogehoge.val}"
but is it really THAT important? Well, now you have 2 lines instead of 1, is it "smart" enough? I don't think you can have that in one line using haml.
But, I have to write 4 lines in haml.
Yes, but you've saved a lot of characters and now have compile-time errors if you don't get your nesting right. Don't focus on the lines used, but the clarity.
I think you can do this:
%p %div.hello
= #sample.val + " " + #hogehoge.val
.hello
= #sample.val
= #hogehoge.val
If you're using <p> for styling, you'd be better putting a margin property in your .hello css
You're missing the point of haml - it's less verbose than erb
I have this:
= link_to user_path(f.object.user) do
%span.hourly-rate>= f.object.user.hourly_rate.to_currency
\/hour
Which renders as:
<a href="/users/44"><span class='hourly-rate'>$16</span>/hour
</a>
The close tag is on a separate line. This results in the link looking funny when there is underlining (eg. on hover)
The solution is to have the markup look like:
<span class='hourly-rate'>$16</span>/hour
But I haven't found a way of doing that in Haml. I'd prefer to keep using the block form of link_to if possible (I imagine not using the block would result in even messier code, but at this point I'm open to anything).
Tangent: I imagine this would be fixed if I was using the :ugly Haml option. I have Haml::Template.options[:ugly] = true in my config/environments/development.rb, and I have haml-rails in my Gemfile (I know it doesn't include :ugly, but it's worth mentioning), and I have an initializer with;
# config/initializers/haml.rb
Haml::Template.options[:format] = :html5
Haml::Template.options[:ugly] = true
... but the code still isn't ugly :( Any ideas why not?
If you can live with the content wrapped inside another div, you can use this:
= link_to user_path(f.object.user) do
%div<>
%span.hourly-rate>= f.object.user.hourly_rate.to_currency
\/hour
The span was being used by javascript, to get its value (in this case, the user's hourly rate) - it wasn't being used for CSS at all.
Thus, I ended up removing the span and adding the user's hourly rate as a data- attribute of a separate field. So the final markup for the link was
= link_to f.object.user.hourly_rate.to_currency + "/hour", user_path(f.object.user)
I have a directory filled with partials. I'm looking to list ONLY the first h1 tags in each partial. The methods to accomplish this task could probably be modified to grab other elements as well.
Right now I use ruby to open each file, print out the first few characters, close file, and repeat. My ruby file parsing skills are limiting me. Here's the code I have at the moment:
<% Dir["app/views/partials/show/*.html.erb"].each do |f1| %>
<% aFile = File.open(f1, "r") %>
<% if aFile %>
<% content = aFile.sysread(20) %>
<p><%= content %></p>
<% else %>
<%= "Unable to open file!" %>
<% end %>
<% end %>
I also think I'm opening the entire partial in memory? Wondering If I can just read up until I find my h1 tag then close file and move on? Again I'm only reading first 20 characters because I haven't yet grasped a way to search for the first h1 tag.
I'll make edits as I work through the open, parse, piece... I appreciate any guidance and direction you can offer. Thanks!
EDIT:
Based on comments below there may be a far better way to accomplish my task. So I'm providing some additional background to get direction on other solutions.
This is for a slide show based on partials in a directory. The slide show is controlled with a navigation element which I would like to populate by the h1 tags in the partials. I'm not going to manually enter these things every time a change is made! I want the end user to simply drag and drop partials into a directory (with a certain name convention and h1 tag description for navigation) and let the slide show do everything else.
I could impose a class on the h1 tag "forNavigation" and on the content "sliderContent" and then use jquery to create a post load <ul> but that doesn't seem right. Plus they'll all be part of the same rendered div.
I guess I'm not clear why reading the first 50 characters of a partial, copying whats in the h1 tags, and putting it in a isn't the most elegant solution?
Like I said, above does everything needed except copy and print whats between the first h1 tag... With an xml parser or some regexp it'll be done. I'm just no good with parsing files.
Please let me know other methods to approach this. Right now I still think it's best to parse the partial (with or without rendering) and put what I need where I want it as needed.
Partials are not meant to be "parsed", but to be rendered inside other partials and templates. If you need to grab a part of a partial, you should probably extrat that part as a further partial, and use that inner partial in both the "listed" partial and in the "aggregated" view.
Rails 2.3.5 (internal work server - stuck at this version for inside apps)
The company 'standard' browser where I work is IE and about 70% of users are using IE7.
What I've slowly been learning (IE7 only) is that if you have FORM beginning or end tags inside a TR or TD, IE7 will create extra lines, sometimes doing very odd things. My solution so far is to put FORM beginning and end tags outside the TABLE tags.
Then, because I want a single line break between tables ... if I use a tag after tables in IE7 only I'll get 3 blank lines between tables where in every other browser there will just be a single line.
Right now I'm dealing with a simple table list of users with a form on each line (delete or change access level). After playing around A LOT with this, IE7 messes up the least when I place the FORM and FORM END tags inbetween table tags like:
<table class="table_standard_blue">
<tr>
<td>
FOO
</td>
</tr>
<% users.each do |user| %>
<% form_for(user) do |f| %>
<tr>
<td>
SOME SELECT / SOME BUTTON
</td>
</tr>
<% end %>
<% end %>
</table>
While the 'guts' of the table will look fine this way, the problem this leaves behind is basically what looks like an extra line break above and below the table (in IE7 only). If I have a couple of tables like this, the effect magnifies and it looks like two blank lines between tables (where in IE8/Firefox) there will be no blank lines.
I know there's something about RAILS putting extra spaces in with FORM tags (and there's suppose to be some fix in RAILS 3 - which I can't use of course at work). Does anyone have any idea how I could fix or hide what's going on in IE7?
Thanks - much appreciated.
I'm not sure you're allowed to place form tags around tr elements, but to answer your question:
Use CSS to reduce the margin-top and margin-bottom of the tables. Before that, though, are you using the right DOCTYPE declaration? That's important to make sure IE7 isn't falling back to emulate layout quirks from IE6 or something.
Before you try what Satya suggested you can try to wrap the form into a span-tag; not sure if it works here but worth a try.
Thanks for the help. Span tags didn't have any affect at all. Also, I went throught multiple doc types and none of them mand any difference. I kept google searching and found a solution though. I'm not 100% sure but it seems to be a problem with IE7 and forms in general (not something a RAILS form_for is doing). Adding this into my stylesheet fixed everything:
FORM
{
display: inline;
}
I had come experience with PHP a time ago and now I'm learning to use Ruby on Rails. But one simple question bothered me in both these languages, so I think I can cross-post it to both tags.
As you know, one of the concepts there is that one can embed PHP or Ruby code into web page template. Then these statements are executed and result of its execution is inserted in certain places of the page, marked with "brackets" <%= ... %>.
Or... wait. We program Ruby/PHP, but not HTML. Maybe we should treat template as Ruby/PHP code, into which sometimes HTML markup is inserted? So the process is treated like that HTML are inserted into ruby code into the "brackets" %> ... <%.
These are two different approaches:
HTML page is the primary entity, and it is affected by code execution; or
code is the primary entity, and it is executed, while HTML snippets are inserted in certain places.
This philosophy leads to different possibilities in coding conventions: result of code execution influences the page If we adhere the first insight, then the code would look like this:
<p>
<% (1..10).foreach do |i| %>
Iteration number <strong><%= i %></strong>. <br/>
<% end %>
</p>
But if we stick to the second alternative, the code would be formatted like this:
<%
%><p><%
(1..10).foreach do |i|
%>Iteration number <strong><%
%><%= i %><%
%></strong>. <br/><%
end
%>
How should the concept of templates be construed? What concepts do you, way more experienced Web developers, account for the coding convention?
If this is in your View layer (and it should be), then the HTML is the primary entity. It's the most pertinent part of that layer -- marking up your data to display in meaningful ways to the user.
Even aside from that, your second example is nearly unreadable. I see what you're doing, but it took me a minute to wrap my brain around it. I've also never, ever seen View-layer code like your second example (and I would make it one of my priorities to change it wherever I saw it if it was in a project I was working on).
To be more concise: you're putting the emphasis on the wrong thing. In my opinion, readability trumps just about everything else. The coding style that produces the most readable code is therefore the most superior (ceteris paribus and YMMV, of course).
Maybe you should look into Haml? I don't know if there's a php equivalent, but as far as Rails goes, it's somewhere in between the two schemes. It's not quite code centric. But when used right, all the raw html is prepared programatically.
In short everything is considered text to be directly outputted, unless prefixed with either a %, - or =. Which translate to html-tag, ruby code that doesn't output. Ruby code that does output. Haml then uses whitespacing to nest things properly, much like python does. Raw html outputs untouched but using % to specify a tag handles closing tags.
Sample:
#outer-div
- #items.each do |i|
%span.item
= i
%br
Outputs
<div id="outer-div">
<span class="item">
item
</span>
<br>
</div>
See the haml tutorial for more information.
To answer the central question. The bulk of any page is going to be HTML or raw text. We reduce the bulk of that text with includes and helpers, but it's still there. If there were a truly code centered approach my use of it would depend on the ratio of program logic to html. Personally I'd rather go with the html centered approach.
If you are interested in a code-oriented view, this is something you might try implementing as a pure Ruby DSL:
tag :p, :class => 'iterations-container' do
(1..10).each do |i|
text "Iteration number "
tag :strong { text i }
text "."
tag :br
end
end
Or perhaps instead of tag :p do ... end, you may favor tag.p do ... end.
I recommend doing only very simple logic in your template files. That way designers who can edit HTML can easily edit even those files to alter the layout if need be.