SlideShare uma empresa Scribd logo
1 de 12
For each controller there is an associated directory in the
app/views directory which holds the template files that
make up the views associated with that controller. These
files are used to display the view that results from each
controller action.
The final HTML output is a composition of three Rails
elements: Templates, Partials and Layouts
Templates
 Action View templates can be written in several ways.
If the template file has a .erb extension then it uses a
mixture of ERB (included in Ruby) and HTML. If the
template file has a .builder extension then a fresh
instance of Builder::XmlMarkup library is used.
 http://railscasts.com/episodes/269-template-
inheritance
Partials
 Partial templates – usually just called "partials" – are
another device for breaking the rendering process into
more manageable chunks. With partials, you can
extract pieces of code from your templates to separate
files and also reuse them throughout your templates.
 Naming Partials
To render a partial as part of a view, you use the render method
within the view: <%= render "shared/menu“%>
That code will pull in the partial from
app/views/shared/_menu.html.erb.
 Passing Variables to Partials
%h1 User Registration
= form_for :user, url: users_path do |form|
.registration
.details.address.demographics
= render 'shared/address', form: form
%p= form.submit 'Register'
That code will pull in the partial from:
app/views/shared/_address.html.erb.
%p
%label Street
%br
= form.text_area :street, rows: 2, cols: 40
%p
%label City
%br
= form.text_field :city
 Rendering an Object
The render method also provides a shorthand syntax to render an object into a
partial, which strictly depends on Rails naming conventions.
<%= render entry %>
The partial corresponding to _entry.html.haml and gets a local variable named
entry. This is equivalent to the following:
<%= render partial: 'entry', object: entry%>
 Rendering Collections
It is very common that a template needs to iterate over a collection and
render a sub-template for each of the elements. This pattern has
been implemented as a single method that accepts an array and
renders a partial for each one of the elements in the array.
So this example for rendering all the products:
<% @products.each do |product| %>
<%= render partial: "product", locals: { product: product } %>
<% end %>
can be rewritten in a single line:
<%= render partial: "product", collection: @products %>
You can use a shorthand syntax for rendering collections. Assuming
@products is a collection of Product instances, you can simply write
the following to produce the same result:
<%= render @products %>
Layout
 Action View decides which layout to render based on the inheritance
hierarchy of controllers being executed.
 Most Rails applications have an application.html.erb file in their layout
directory. It shares its name with the ApplicationController, which is
typically extended by all the other controllers in an application;
therefore it is picked up as the default layout for all views.
 Within a layout, you have access to three tools for combining different
bits of output to form the overall response:
 Asset tags
 yield and content_for
 Partials
 Asset Tag Helpers
Asset tag helpers provide methods for generating HTML that
link views to feeds, JavaScript, stylesheets, images, videos and
audios. There are six asset tag helpers available in Rails:
 javascript_include_tag
 stylesheet_link_tag
 image_tag
 auto_discovery_link_tag
 video_tag
 audio_tag
 Linking to JavaScript Files with the javascript_include_tag
<%= javascript_include_tag "main" %>
Rails will then output a script tag such as this:
<script src='/assets/main.js'></script>
It will looking for file in app/assets/javascripts/main.js
To include app/assets/javascripts/main.js and
app/assets/javascripts/photos/columns.js:
<%= javascript_include_tag "main", "/photos/columns" %>
To include http://example.com/main.js:
<%= javascript_include_tag "http://example.com/main.js" %>
The same thing for include stylesheet_link_tag
 Understanding yield
Within the context of a layout, yield identifies a section where content from the view should be inserted.
The simplest way to use this is to have a single yield, into which the entire contents of the view
currently being rendered is inserted:
<html>
<head>
</head>
<body>
<%= yield %>
</body>
</html>
You can also create a layout with multiple yielding regions:
<html>
<head>
<%= yield :head %>
</head>
<body>
<%= yield %>
</body>
</html>
The main body of the view will always render into the unnamed yield. To render content into a
named yield, you use the content_for method.
 Using the content_for Method
 The content_for method allows you to insert content into a named yield block in your
layout. For example, this view would work with the layout that you just saw:
<% content_for :head do %>
<title>A simple page</title>
<% end %>
<p>Hello, Rails!</p>
The result of rendering this page into the supplied layout would be this HTML:
<html>
<head>
<title>A simple page</title>
</head>
<body>
<p>Hello, Rails!</p>
</body>
</html>
The content_for method is very helpful when your layout contains distinct regions such as
sidebars and footers that should get their own blocks of content inserted. It's also useful
for inserting tags that load page-specific JavaScript or css files into the header of an
otherwise generic layout.
References
 http://guides.rubyonrails.org/layouts_and_rendering.
html
 Rails 4 way.pdf in dcserver

Mais conteúdo relacionado

Mais procurados

Adrotator in asp
Adrotator in aspAdrotator in asp
Adrotator in aspSireesh K
 
Jsp elements
Jsp elementsJsp elements
Jsp elementsNuha Noor
 
RichControl in Asp.net
RichControl in Asp.netRichControl in Asp.net
RichControl in Asp.netBhumivaghasiya
 
Symfony Admin Generator - generator.yml
Symfony Admin Generator - generator.ymlSymfony Admin Generator - generator.yml
Symfony Admin Generator - generator.ymlRavi Mone
 
Creating Single Page Applications with Oracle Apex
Creating Single Page Applications with Oracle ApexCreating Single Page Applications with Oracle Apex
Creating Single Page Applications with Oracle ApexDick Dral
 
Murach : HOW to work with controllers and routing
Murach : HOW to work with controllers and routingMurach : HOW to work with controllers and routing
Murach : HOW to work with controllers and routingMahmoudOHassouna
 
Murach : How to develop a single-page MVC web
Murach : How to develop a single-page MVC web Murach : How to develop a single-page MVC web
Murach : How to develop a single-page MVC web MahmoudOHassouna
 
Salesforce ANT migration
Salesforce ANT migration Salesforce ANT migration
Salesforce ANT migration Cloud Analogy
 
'Best Practices for Elgg Plugin Developers' Cash Costello #ECSF
'Best Practices for Elgg Plugin Developers' Cash Costello #ECSF'Best Practices for Elgg Plugin Developers' Cash Costello #ECSF
'Best Practices for Elgg Plugin Developers' Cash Costello #ECSFCondiminds
 
Integration of APEX and Oracle Forms
Integration of APEX and Oracle FormsIntegration of APEX and Oracle Forms
Integration of APEX and Oracle FormsRoel Hartman
 

Mais procurados (20)

Adrotator in asp
Adrotator in aspAdrotator in asp
Adrotator in asp
 
Jsp elements
Jsp elementsJsp elements
Jsp elements
 
RichControl in Asp.net
RichControl in Asp.netRichControl in Asp.net
RichControl in Asp.net
 
Rails introduction
Rails introductionRails introduction
Rails introduction
 
Mvc in symfony
Mvc in symfonyMvc in symfony
Mvc in symfony
 
Symfony Admin Generator - generator.yml
Symfony Admin Generator - generator.ymlSymfony Admin Generator - generator.yml
Symfony Admin Generator - generator.yml
 
Creating Single Page Applications with Oracle Apex
Creating Single Page Applications with Oracle ApexCreating Single Page Applications with Oracle Apex
Creating Single Page Applications with Oracle Apex
 
JSP Directives
JSP DirectivesJSP Directives
JSP Directives
 
Standard List Controllers
Standard List Controllers Standard List Controllers
Standard List Controllers
 
Getting a Quick Start with Visualforce
Getting a Quick Start with Visualforce Getting a Quick Start with Visualforce
Getting a Quick Start with Visualforce
 
Murach : HOW to work with controllers and routing
Murach : HOW to work with controllers and routingMurach : HOW to work with controllers and routing
Murach : HOW to work with controllers and routing
 
Struts Intro
Struts IntroStruts Intro
Struts Intro
 
MVC Training Part 1
MVC Training Part 1MVC Training Part 1
MVC Training Part 1
 
Murach : How to develop a single-page MVC web
Murach : How to develop a single-page MVC web Murach : How to develop a single-page MVC web
Murach : How to develop a single-page MVC web
 
Salesforce ANT migration
Salesforce ANT migration Salesforce ANT migration
Salesforce ANT migration
 
'Best Practices for Elgg Plugin Developers' Cash Costello #ECSF
'Best Practices for Elgg Plugin Developers' Cash Costello #ECSF'Best Practices for Elgg Plugin Developers' Cash Costello #ECSF
'Best Practices for Elgg Plugin Developers' Cash Costello #ECSF
 
Starting with angular js
Starting with angular js Starting with angular js
Starting with angular js
 
Jsf intro
Jsf introJsf intro
Jsf intro
 
Routing
RoutingRouting
Routing
 
Integration of APEX and Oracle Forms
Integration of APEX and Oracle FormsIntegration of APEX and Oracle Forms
Integration of APEX and Oracle Forms
 

Destaque (13)

Jessie lit id
Jessie lit idJessie lit id
Jessie lit id
 
Primeface
PrimefacePrimeface
Primeface
 
Team building
Team buildingTeam building
Team building
 
Ut ultrasonic methode 2
Ut ultrasonic methode 2Ut ultrasonic methode 2
Ut ultrasonic methode 2
 
My lit id
My lit idMy lit id
My lit id
 
Literacy
LiteracyLiteracy
Literacy
 
Baocao bt7 n15
Baocao bt7 n15Baocao bt7 n15
Baocao bt7 n15
 
Ngon ngu lap_trinh_c++
Ngon ngu lap_trinh_c++Ngon ngu lap_trinh_c++
Ngon ngu lap_trinh_c++
 
Matrix2 english
Matrix2 englishMatrix2 english
Matrix2 english
 
Guia ingreso al curso
Guia ingreso al cursoGuia ingreso al curso
Guia ingreso al curso
 
Body language
Body languageBody language
Body language
 
Iaea ndt question
Iaea ndt questionIaea ndt question
Iaea ndt question
 
Radar and sonar subbu
Radar and sonar subbuRadar and sonar subbu
Radar and sonar subbu
 

Semelhante a Templates, partials and layouts

Useful Rails Plugins
Useful Rails PluginsUseful Rails Plugins
Useful Rails Pluginsnavjeet
 
RubyOnRails-Cheatsheet-BlaineKendall
RubyOnRails-Cheatsheet-BlaineKendallRubyOnRails-Cheatsheet-BlaineKendall
RubyOnRails-Cheatsheet-BlaineKendalltutorialsruby
 
RubyOnRails-Cheatsheet-BlaineKendall
RubyOnRails-Cheatsheet-BlaineKendallRubyOnRails-Cheatsheet-BlaineKendall
RubyOnRails-Cheatsheet-BlaineKendalltutorialsruby
 
Rails
RailsRails
RailsSHC
 
Template rendering in rails
Template rendering in rails Template rendering in rails
Template rendering in rails Hung Wu Lo
 
RoR 101: Session 2
RoR 101: Session 2RoR 101: Session 2
RoR 101: Session 2Rory Gianni
 
Zend Framework Quick Start Walkthrough
Zend Framework Quick Start WalkthroughZend Framework Quick Start Walkthrough
Zend Framework Quick Start WalkthroughBradley Holt
 
AEM Sightly Deep Dive
AEM Sightly Deep DiveAEM Sightly Deep Dive
AEM Sightly Deep DiveGabriel Walt
 
Web Programming - 7 Blading Template
Web Programming - 7 Blading TemplateWeb Programming - 7 Blading Template
Web Programming - 7 Blading TemplateAndiNurkholis1
 
6 introduction-php-mvc-cakephp-m6-views-slides
6 introduction-php-mvc-cakephp-m6-views-slides6 introduction-php-mvc-cakephp-m6-views-slides
6 introduction-php-mvc-cakephp-m6-views-slidesMasterCode.vn
 
Building a dashboard using AngularJS
Building a dashboard using AngularJSBuilding a dashboard using AngularJS
Building a dashboard using AngularJSRajthilakMCA
 
Ruby On Rails Siddhesh
Ruby On Rails SiddheshRuby On Rails Siddhesh
Ruby On Rails SiddheshSiddhesh Bhobe
 
Overview of MVC Framework - by software outsourcing company india
Overview of MVC Framework - by software outsourcing company indiaOverview of MVC Framework - by software outsourcing company india
Overview of MVC Framework - by software outsourcing company indiaJignesh Aakoliya
 
Asp.Net 2.0 Presentation
Asp.Net 2.0 PresentationAsp.Net 2.0 Presentation
Asp.Net 2.0 Presentationsasidhar
 

Semelhante a Templates, partials and layouts (20)

Actionview
ActionviewActionview
Actionview
 
Rails review
Rails reviewRails review
Rails review
 
Useful Rails Plugins
Useful Rails PluginsUseful Rails Plugins
Useful Rails Plugins
 
RubyOnRails-Cheatsheet-BlaineKendall
RubyOnRails-Cheatsheet-BlaineKendallRubyOnRails-Cheatsheet-BlaineKendall
RubyOnRails-Cheatsheet-BlaineKendall
 
RubyOnRails-Cheatsheet-BlaineKendall
RubyOnRails-Cheatsheet-BlaineKendallRubyOnRails-Cheatsheet-BlaineKendall
RubyOnRails-Cheatsheet-BlaineKendall
 
Ruby on rails RAD
Ruby on rails RADRuby on rails RAD
Ruby on rails RAD
 
Rails
RailsRails
Rails
 
Template rendering in rails
Template rendering in rails Template rendering in rails
Template rendering in rails
 
RoR 101: Session 2
RoR 101: Session 2RoR 101: Session 2
RoR 101: Session 2
 
Zend Framework Quick Start Walkthrough
Zend Framework Quick Start WalkthroughZend Framework Quick Start Walkthrough
Zend Framework Quick Start Walkthrough
 
AEM Sightly Deep Dive
AEM Sightly Deep DiveAEM Sightly Deep Dive
AEM Sightly Deep Dive
 
Web Programming - 7 Blading Template
Web Programming - 7 Blading TemplateWeb Programming - 7 Blading Template
Web Programming - 7 Blading Template
 
6 introduction-php-mvc-cakephp-m6-views-slides
6 introduction-php-mvc-cakephp-m6-views-slides6 introduction-php-mvc-cakephp-m6-views-slides
6 introduction-php-mvc-cakephp-m6-views-slides
 
The Rails Way
The Rails WayThe Rails Way
The Rails Way
 
Building a dashboard using AngularJS
Building a dashboard using AngularJSBuilding a dashboard using AngularJS
Building a dashboard using AngularJS
 
ReactJS.pptx
ReactJS.pptxReactJS.pptx
ReactJS.pptx
 
Ruby On Rails Siddhesh
Ruby On Rails SiddheshRuby On Rails Siddhesh
Ruby On Rails Siddhesh
 
Overview of MVC Framework - by software outsourcing company india
Overview of MVC Framework - by software outsourcing company indiaOverview of MVC Framework - by software outsourcing company india
Overview of MVC Framework - by software outsourcing company india
 
Session 1
Session 1Session 1
Session 1
 
Asp.Net 2.0 Presentation
Asp.Net 2.0 PresentationAsp.Net 2.0 Presentation
Asp.Net 2.0 Presentation
 

Último

RSA Conference Exhibitor List 2024 - Exhibitors Data
RSA Conference Exhibitor List 2024 - Exhibitors DataRSA Conference Exhibitor List 2024 - Exhibitors Data
RSA Conference Exhibitor List 2024 - Exhibitors DataExhibitors Data
 
Cracking the Cultural Competence Code.pptx
Cracking the Cultural Competence Code.pptxCracking the Cultural Competence Code.pptx
Cracking the Cultural Competence Code.pptxWorkforce Group
 
Call Girls Hebbal Just Call 👗 7737669865 👗 Top Class Call Girl Service Bangalore
Call Girls Hebbal Just Call 👗 7737669865 👗 Top Class Call Girl Service BangaloreCall Girls Hebbal Just Call 👗 7737669865 👗 Top Class Call Girl Service Bangalore
Call Girls Hebbal Just Call 👗 7737669865 👗 Top Class Call Girl Service Bangaloreamitlee9823
 
Enhancing and Restoring Safety & Quality Cultures - Dave Litwiller - May 2024...
Enhancing and Restoring Safety & Quality Cultures - Dave Litwiller - May 2024...Enhancing and Restoring Safety & Quality Cultures - Dave Litwiller - May 2024...
Enhancing and Restoring Safety & Quality Cultures - Dave Litwiller - May 2024...Dave Litwiller
 
Best VIP Call Girls Noida Sector 40 Call Me: 8448380779
Best VIP Call Girls Noida Sector 40 Call Me: 8448380779Best VIP Call Girls Noida Sector 40 Call Me: 8448380779
Best VIP Call Girls Noida Sector 40 Call Me: 8448380779Delhi Call girls
 
B.COM Unit – 4 ( CORPORATE SOCIAL RESPONSIBILITY ( CSR ).pptx
B.COM Unit – 4 ( CORPORATE SOCIAL RESPONSIBILITY ( CSR ).pptxB.COM Unit – 4 ( CORPORATE SOCIAL RESPONSIBILITY ( CSR ).pptx
B.COM Unit – 4 ( CORPORATE SOCIAL RESPONSIBILITY ( CSR ).pptxpriyanshujha201
 
The Path to Product Excellence: Avoiding Common Pitfalls and Enhancing Commun...
The Path to Product Excellence: Avoiding Common Pitfalls and Enhancing Commun...The Path to Product Excellence: Avoiding Common Pitfalls and Enhancing Commun...
The Path to Product Excellence: Avoiding Common Pitfalls and Enhancing Commun...Aggregage
 
👉Chandigarh Call Girls 👉9878799926👉Just Call👉Chandigarh Call Girl In Chandiga...
👉Chandigarh Call Girls 👉9878799926👉Just Call👉Chandigarh Call Girl In Chandiga...👉Chandigarh Call Girls 👉9878799926👉Just Call👉Chandigarh Call Girl In Chandiga...
👉Chandigarh Call Girls 👉9878799926👉Just Call👉Chandigarh Call Girl In Chandiga...rajveerescorts2022
 
Insurers' journeys to build a mastery in the IoT usage
Insurers' journeys to build a mastery in the IoT usageInsurers' journeys to build a mastery in the IoT usage
Insurers' journeys to build a mastery in the IoT usageMatteo Carbone
 
Monthly Social Media Update April 2024 pptx.pptx
Monthly Social Media Update April 2024 pptx.pptxMonthly Social Media Update April 2024 pptx.pptx
Monthly Social Media Update April 2024 pptx.pptxAndy Lambert
 
Pharma Works Profile of Karan Communications
Pharma Works Profile of Karan CommunicationsPharma Works Profile of Karan Communications
Pharma Works Profile of Karan Communicationskarancommunications
 
How to Get Started in Social Media for Art League City
How to Get Started in Social Media for Art League CityHow to Get Started in Social Media for Art League City
How to Get Started in Social Media for Art League CityEric T. Tung
 
The Coffee Bean & Tea Leaf(CBTL), Business strategy case study
The Coffee Bean & Tea Leaf(CBTL), Business strategy case studyThe Coffee Bean & Tea Leaf(CBTL), Business strategy case study
The Coffee Bean & Tea Leaf(CBTL), Business strategy case studyEthan lee
 
Call Girls Electronic City Just Call 👗 7737669865 👗 Top Class Call Girl Servi...
Call Girls Electronic City Just Call 👗 7737669865 👗 Top Class Call Girl Servi...Call Girls Electronic City Just Call 👗 7737669865 👗 Top Class Call Girl Servi...
Call Girls Electronic City Just Call 👗 7737669865 👗 Top Class Call Girl Servi...amitlee9823
 
It will be International Nurses' Day on 12 May
It will be International Nurses' Day on 12 MayIt will be International Nurses' Day on 12 May
It will be International Nurses' Day on 12 MayNZSG
 
Boost the utilization of your HCL environment by reevaluating use cases and f...
Boost the utilization of your HCL environment by reevaluating use cases and f...Boost the utilization of your HCL environment by reevaluating use cases and f...
Boost the utilization of your HCL environment by reevaluating use cases and f...Roland Driesen
 
Dr. Admir Softic_ presentation_Green Club_ENG.pdf
Dr. Admir Softic_ presentation_Green Club_ENG.pdfDr. Admir Softic_ presentation_Green Club_ENG.pdf
Dr. Admir Softic_ presentation_Green Club_ENG.pdfAdmir Softic
 
Call Girls Navi Mumbai Just Call 9907093804 Top Class Call Girl Service Avail...
Call Girls Navi Mumbai Just Call 9907093804 Top Class Call Girl Service Avail...Call Girls Navi Mumbai Just Call 9907093804 Top Class Call Girl Service Avail...
Call Girls Navi Mumbai Just Call 9907093804 Top Class Call Girl Service Avail...Dipal Arora
 

Último (20)

RSA Conference Exhibitor List 2024 - Exhibitors Data
RSA Conference Exhibitor List 2024 - Exhibitors DataRSA Conference Exhibitor List 2024 - Exhibitors Data
RSA Conference Exhibitor List 2024 - Exhibitors Data
 
Cracking the Cultural Competence Code.pptx
Cracking the Cultural Competence Code.pptxCracking the Cultural Competence Code.pptx
Cracking the Cultural Competence Code.pptx
 
Call Girls Hebbal Just Call 👗 7737669865 👗 Top Class Call Girl Service Bangalore
Call Girls Hebbal Just Call 👗 7737669865 👗 Top Class Call Girl Service BangaloreCall Girls Hebbal Just Call 👗 7737669865 👗 Top Class Call Girl Service Bangalore
Call Girls Hebbal Just Call 👗 7737669865 👗 Top Class Call Girl Service Bangalore
 
Enhancing and Restoring Safety & Quality Cultures - Dave Litwiller - May 2024...
Enhancing and Restoring Safety & Quality Cultures - Dave Litwiller - May 2024...Enhancing and Restoring Safety & Quality Cultures - Dave Litwiller - May 2024...
Enhancing and Restoring Safety & Quality Cultures - Dave Litwiller - May 2024...
 
Best VIP Call Girls Noida Sector 40 Call Me: 8448380779
Best VIP Call Girls Noida Sector 40 Call Me: 8448380779Best VIP Call Girls Noida Sector 40 Call Me: 8448380779
Best VIP Call Girls Noida Sector 40 Call Me: 8448380779
 
B.COM Unit – 4 ( CORPORATE SOCIAL RESPONSIBILITY ( CSR ).pptx
B.COM Unit – 4 ( CORPORATE SOCIAL RESPONSIBILITY ( CSR ).pptxB.COM Unit – 4 ( CORPORATE SOCIAL RESPONSIBILITY ( CSR ).pptx
B.COM Unit – 4 ( CORPORATE SOCIAL RESPONSIBILITY ( CSR ).pptx
 
The Path to Product Excellence: Avoiding Common Pitfalls and Enhancing Commun...
The Path to Product Excellence: Avoiding Common Pitfalls and Enhancing Commun...The Path to Product Excellence: Avoiding Common Pitfalls and Enhancing Commun...
The Path to Product Excellence: Avoiding Common Pitfalls and Enhancing Commun...
 
👉Chandigarh Call Girls 👉9878799926👉Just Call👉Chandigarh Call Girl In Chandiga...
👉Chandigarh Call Girls 👉9878799926👉Just Call👉Chandigarh Call Girl In Chandiga...👉Chandigarh Call Girls 👉9878799926👉Just Call👉Chandigarh Call Girl In Chandiga...
👉Chandigarh Call Girls 👉9878799926👉Just Call👉Chandigarh Call Girl In Chandiga...
 
Insurers' journeys to build a mastery in the IoT usage
Insurers' journeys to build a mastery in the IoT usageInsurers' journeys to build a mastery in the IoT usage
Insurers' journeys to build a mastery in the IoT usage
 
Monthly Social Media Update April 2024 pptx.pptx
Monthly Social Media Update April 2024 pptx.pptxMonthly Social Media Update April 2024 pptx.pptx
Monthly Social Media Update April 2024 pptx.pptx
 
VVVIP Call Girls In Greater Kailash ➡️ Delhi ➡️ 9999965857 🚀 No Advance 24HRS...
VVVIP Call Girls In Greater Kailash ➡️ Delhi ➡️ 9999965857 🚀 No Advance 24HRS...VVVIP Call Girls In Greater Kailash ➡️ Delhi ➡️ 9999965857 🚀 No Advance 24HRS...
VVVIP Call Girls In Greater Kailash ➡️ Delhi ➡️ 9999965857 🚀 No Advance 24HRS...
 
Pharma Works Profile of Karan Communications
Pharma Works Profile of Karan CommunicationsPharma Works Profile of Karan Communications
Pharma Works Profile of Karan Communications
 
How to Get Started in Social Media for Art League City
How to Get Started in Social Media for Art League CityHow to Get Started in Social Media for Art League City
How to Get Started in Social Media for Art League City
 
The Coffee Bean & Tea Leaf(CBTL), Business strategy case study
The Coffee Bean & Tea Leaf(CBTL), Business strategy case studyThe Coffee Bean & Tea Leaf(CBTL), Business strategy case study
The Coffee Bean & Tea Leaf(CBTL), Business strategy case study
 
Call Girls Electronic City Just Call 👗 7737669865 👗 Top Class Call Girl Servi...
Call Girls Electronic City Just Call 👗 7737669865 👗 Top Class Call Girl Servi...Call Girls Electronic City Just Call 👗 7737669865 👗 Top Class Call Girl Servi...
Call Girls Electronic City Just Call 👗 7737669865 👗 Top Class Call Girl Servi...
 
It will be International Nurses' Day on 12 May
It will be International Nurses' Day on 12 MayIt will be International Nurses' Day on 12 May
It will be International Nurses' Day on 12 May
 
Forklift Operations: Safety through Cartoons
Forklift Operations: Safety through CartoonsForklift Operations: Safety through Cartoons
Forklift Operations: Safety through Cartoons
 
Boost the utilization of your HCL environment by reevaluating use cases and f...
Boost the utilization of your HCL environment by reevaluating use cases and f...Boost the utilization of your HCL environment by reevaluating use cases and f...
Boost the utilization of your HCL environment by reevaluating use cases and f...
 
Dr. Admir Softic_ presentation_Green Club_ENG.pdf
Dr. Admir Softic_ presentation_Green Club_ENG.pdfDr. Admir Softic_ presentation_Green Club_ENG.pdf
Dr. Admir Softic_ presentation_Green Club_ENG.pdf
 
Call Girls Navi Mumbai Just Call 9907093804 Top Class Call Girl Service Avail...
Call Girls Navi Mumbai Just Call 9907093804 Top Class Call Girl Service Avail...Call Girls Navi Mumbai Just Call 9907093804 Top Class Call Girl Service Avail...
Call Girls Navi Mumbai Just Call 9907093804 Top Class Call Girl Service Avail...
 

Templates, partials and layouts

  • 1. For each controller there is an associated directory in the app/views directory which holds the template files that make up the views associated with that controller. These files are used to display the view that results from each controller action. The final HTML output is a composition of three Rails elements: Templates, Partials and Layouts
  • 2. Templates  Action View templates can be written in several ways. If the template file has a .erb extension then it uses a mixture of ERB (included in Ruby) and HTML. If the template file has a .builder extension then a fresh instance of Builder::XmlMarkup library is used.  http://railscasts.com/episodes/269-template- inheritance
  • 3. Partials  Partial templates – usually just called "partials" – are another device for breaking the rendering process into more manageable chunks. With partials, you can extract pieces of code from your templates to separate files and also reuse them throughout your templates.  Naming Partials To render a partial as part of a view, you use the render method within the view: <%= render "shared/menu“%> That code will pull in the partial from app/views/shared/_menu.html.erb.
  • 4.  Passing Variables to Partials %h1 User Registration = form_for :user, url: users_path do |form| .registration .details.address.demographics = render 'shared/address', form: form %p= form.submit 'Register' That code will pull in the partial from: app/views/shared/_address.html.erb. %p %label Street %br = form.text_area :street, rows: 2, cols: 40 %p %label City %br = form.text_field :city
  • 5.  Rendering an Object The render method also provides a shorthand syntax to render an object into a partial, which strictly depends on Rails naming conventions. <%= render entry %> The partial corresponding to _entry.html.haml and gets a local variable named entry. This is equivalent to the following: <%= render partial: 'entry', object: entry%>
  • 6.  Rendering Collections It is very common that a template needs to iterate over a collection and render a sub-template for each of the elements. This pattern has been implemented as a single method that accepts an array and renders a partial for each one of the elements in the array. So this example for rendering all the products: <% @products.each do |product| %> <%= render partial: "product", locals: { product: product } %> <% end %> can be rewritten in a single line: <%= render partial: "product", collection: @products %> You can use a shorthand syntax for rendering collections. Assuming @products is a collection of Product instances, you can simply write the following to produce the same result: <%= render @products %>
  • 7. Layout  Action View decides which layout to render based on the inheritance hierarchy of controllers being executed.  Most Rails applications have an application.html.erb file in their layout directory. It shares its name with the ApplicationController, which is typically extended by all the other controllers in an application; therefore it is picked up as the default layout for all views.  Within a layout, you have access to three tools for combining different bits of output to form the overall response:  Asset tags  yield and content_for  Partials
  • 8.  Asset Tag Helpers Asset tag helpers provide methods for generating HTML that link views to feeds, JavaScript, stylesheets, images, videos and audios. There are six asset tag helpers available in Rails:  javascript_include_tag  stylesheet_link_tag  image_tag  auto_discovery_link_tag  video_tag  audio_tag
  • 9.  Linking to JavaScript Files with the javascript_include_tag <%= javascript_include_tag "main" %> Rails will then output a script tag such as this: <script src='/assets/main.js'></script> It will looking for file in app/assets/javascripts/main.js To include app/assets/javascripts/main.js and app/assets/javascripts/photos/columns.js: <%= javascript_include_tag "main", "/photos/columns" %> To include http://example.com/main.js: <%= javascript_include_tag "http://example.com/main.js" %> The same thing for include stylesheet_link_tag
  • 10.  Understanding yield Within the context of a layout, yield identifies a section where content from the view should be inserted. The simplest way to use this is to have a single yield, into which the entire contents of the view currently being rendered is inserted: <html> <head> </head> <body> <%= yield %> </body> </html> You can also create a layout with multiple yielding regions: <html> <head> <%= yield :head %> </head> <body> <%= yield %> </body> </html> The main body of the view will always render into the unnamed yield. To render content into a named yield, you use the content_for method.
  • 11.  Using the content_for Method  The content_for method allows you to insert content into a named yield block in your layout. For example, this view would work with the layout that you just saw: <% content_for :head do %> <title>A simple page</title> <% end %> <p>Hello, Rails!</p> The result of rendering this page into the supplied layout would be this HTML: <html> <head> <title>A simple page</title> </head> <body> <p>Hello, Rails!</p> </body> </html> The content_for method is very helpful when your layout contains distinct regions such as sidebars and footers that should get their own blocks of content inserted. It's also useful for inserting tags that load page-specific JavaScript or css files into the header of an otherwise generic layout.