Skip to content

Instantly share code, notes, and snippets.

@matthiaslau
Last active August 29, 2015 14:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save matthiaslau/532ffa6d0ba4bd270b1f to your computer and use it in GitHub Desktop.
Save matthiaslau/532ffa6d0ba4bd270b1f to your computer and use it in GitHub Desktop.
Fragmented Web Design
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=Edge,chrome=1">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Fragmented Web Design Example</title>
<%= csrf_meta_tags %>
<%= render :partial => @head %>
</head>
<body>
<div class="container-fluid">
<!-- nav -->
<%= render :partial => @nav %>
<!-- /nav -->
<!-- content -->
<%= render :partial => @content %>
<!-- /content -->
</div>
<!-- footer -->
<%= render :partial => @footer %>
<!-- /footer -->
<!-- Javascripts
================================================== -->
<%= javascript_include_tag "application" %>
</body>
</html>
require "browser"
class ApplicationController < ActionController::Base
# Prevent CSRF attacks by raising an exception.
# For APIs, you may want to use :null_session instead.
protect_from_forgery with: :exception
before_action :fragmentSelection
private
def fragmentSelection
# default fragments
@head = 'layouts/head'
@nav = 'layouts/nav'
@content = 'layouts/content'
@footer = 'layouts/footer'
# mobile fragments
if browser.mobile?
@nav = 'layouts/mobile/nav'
end
# only deliver requested fragments
layout_empty = 'layouts/empty'
available_fragments = ['head', 'nav', 'content', 'footer']
fragments_header = request.headers["Fragments"]
# just render all fragments if header is not set
return unless fragments_header
requested_fragments = fragments_header.split(',')
# set all fragments not requested to an empty view partial
empty_fragments = (available_fragments - requested_fragments)
empty_fragments.each do |fragment_name|
instance_variable_set("@#{fragment_name}", layout_empty)
end
# because only some fragments were requested we render a different layout
# without the html skeleton
render :layout => 'selected'
end
end
<!-- nav -->
<%= render :partial => @nav %>
<!-- /nav -->
<!-- content -->
<%= render :partial => @content %>
<!-- /content -->
<!-- footer -->
<%= render :partial => @footer %>
<!-- /footer -->
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment