Where is the meta content located in MVC?

I thought about the meta content in MVC, in particular the page title and meta description (which is useful for specifying the snippet Google displays in search results).

I can’t firmly decide where it should live. Often there is little logic around it, depending (for the UGC application) on how readers interacted with the content.

I can’t decide if this meta content is better designed at the presentation level or in the controller. It almost certainly does not live in the model, since it is specific to the specific presentation of the data, however, although my first instinct was to put it in a point of view, I feel that it can be better abstracted.

I am interested in the approach that other people have taken.

+6
source share
1 answer

Meta content is usually set using the helpers content_for and yield .

For instance:

 # app/helpers/application_helper.rb def title(title) content_for :title, title end def description(description) content_for :description, description end # app/views/layouts/application.html.erb <title>My app <%= yield :title %></title> <meta name="description"><%= yield :description %></meta> # app/views/some_controller/some_action.html.erb <% title @an_instance.an_attribute # or whatever you want by the way description @an_instance.another_attribute %> 

If you intend to do streaming , you should use provide instead of content_for in your helpers.

Never put an instance variable in the controller that is used for meta content (e.g. @title = 'blabla'; @description = 'blablabla' )

Here are some resources that do the same (non-exhaustive list):

+6
source

Source: https://habr.com/ru/post/904240/


All Articles