Correct coding convention for inline code in web page templates

I got some experience with PHP again and am now learning to use Ruby on Rails. But one simple question bothered me in both of these languages, so I think I can cross-post it in both tags.

As you know, one concept is that you can embed PHP or Ruby code into a web page template. Then these instructions are executed, and the result of its execution is inserted in certain places on the page, marked with "brackets" <%= ... %>

.

Or ... wait. We program Ruby / PHP, but not HTML. Maybe we should think of the template as Ruby / PHP code that sometimes has HTML markup added to it? Thus, the process is processed so that HTML is inserted into the ruby ​​code in "brackets" %> ... <%

.

These are two different approaches:

  • The HTML page is the main object and is affected by code execution; or
  • code is the main object and it gets executed and HTML snippets are inserted at specific locations.

This philosophy leads to different possibilities in coding conventions: the result of the code execution affects the page. If we stick to the first understanding, then the code will look like this:

<p>
<% (1..10).foreach do |i| %>
Iteration number <strong><%= i %></strong>. <br/>
<% end %>
</p>

      

But if we stick with the second alternative, the code will be formatted like this:

<%
%><p><%
(1..10).foreach do |i|
  %>Iteration number <strong><%
  %><%= i %><%
  %></strong>. <br/><%
end
%>

      

How should you understand the concept of templates? What concepts do you have, more experienced web developers take into account the coding convention?

+2


source to share


4 answers


If it's in your view layer (and it should be), then HTML is the main object. This is the most important part of this layer - marking your data for display in meaningful ways to the user.

Even aside from that, your second example is almost unreadable. I can see what you are doing, but it took me a minute to trick my brain. I've never even seen the View level code like your second example (and I would make it one of my priorities to change it wherever I see it if it was in a project I was working on).



To be more succinct: you emphasize the wrong. In my opinion, readability beats everything else. The coding style that produces the most readable code is thus the most excellent (all other things being equal, and YMMV, of course).

+4


source


Maybe you should look into Hamla? I don't know if there is a php equivalent, but as far as Rails goes, it sits somewhere in between the two schemas. It's not really code. But when used correctly, all of the raw html is prepared programmatically.

In short, all considered that the text is displayed directly, unless a prefix or %

, -

or =

. Which translate to html tag, ruby ​​code not outputting. Ruby code that outputs. Haml then uses spaces to nest things correctly, like python does. The raw html output is intact, but using% to specify the tag handles the closing tags.

Example:

#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 will be HTML or raw text. We shrink the bulk of this text with inclusions and helpers, but it still exists. If there was a code oriented approach, my approach to it would depend on the relationship of the program logic to the html. Personally, I would rather use an html oriented approach.

+1


source


If you are interested in code-centric representation, this is what you can try to implement 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 tag :p do ... end

, you can approve tag.p do ... end

.

+1


source


I recommend doing only very simple logic in your template files. This way, designers who can edit HTML can easily edit even these files to change the layout if needed.

+1


source







All Articles