Skip to content

Instantly share code, notes, and snippets.

@bindzus
Forked from alexsanford/application.html.erb
Last active December 14, 2015 05:08
Show Gist options
  • Save bindzus/5032895 to your computer and use it in GitHub Desktop.
Save bindzus/5032895 to your computer and use it in GitHub Desktop.
<!-- File: app/views/layouts/application.html.erb -->
<%= yield %>
<%= locomotive_content_for :block => 'main_content' do %>
<h1>Hello Locomotive</h1>
<% end %>
<%= locomotive_render :path => '/templates/simple-page' %>
# File: config/initializers/inherited_block.rb
class InheritedBlock < ::Locomotive::Liquid::Tags::InheritedBlock
def render(context)
rails_view = context.registers[:rails_view]
if rails_view.present? && name == context.registers[:rails_view_block]
rails_view
else
super
end
end
::Liquid::Template.register_tag('block', InheritedBlock)
end
# File: app/helpers/rails_locomotive_helper.rb
module RailsLocomotiveHelper
def locomotive_append_to(options = {}, &block)
options = {
:block => 'content',
}.merge(options)
output = capture &block
@blocks ||= {}
if [email protected]_key? options[:block]
@blocks[options[:block]] = ''
end
@blocks[options[:block]] << output
end
def locomotive_content_for(options = {}, &block)
options = {
:block => 'content',
}.merge(options)
output = capture &block
@blocks ||= {}
@blocks[options[:block]] = output
end
def locomotive_render(options = {})
options = {
:path => ''
}.merge(options)
page = locomotive_page(options[:path])
template = page.try(:template)
if template.present?
options = options_for_current_controller if options.blank?
context = current_context(page, @blocks, options)
template.render(context).html_safe
else
# TODO: Something meaningful needs to be done here like sending the 404 page
end
end
protected
include Locomotive::Routing::SiteDispatcher
def options_for_current_controller
options = {}
options[:title] = @page_title || I18n.t("#{params[:controller]}.#{params[:action]}", :scope => :page_titles, :default => '').presence || I18n.t(params[:controller], :scope => :page_titles)
options[:full_path] = request.path.slice(1..request.path.length - 1)
options[:slug] = options[:full_path].try(:parameterize, '_')
options
end
def normalize_path(path)
norm = path
norm.sub!(/^\/+/, '')
norm.sub!(/\/+$/, '')
norm.gsub!(/\/{2,}/, '/')
if norm.empty?
'index'
else
norm
end
end
def locomotive_page(fullpath)
fullpath = normalize_path(fullpath)
current_site.pages.where(:fullpath => fullpath).first
end
# # Sets up the context to be passed in for rendering
# # Any extra parameters passed in here must also
# # be mixedin to the locomotive rendering controller
def current_context(page, blocks, options={})
[:title, :slug, :fullpath].each do |option|
page.send(:"#{option}=", options[option]) if options[option].present?
end
assigns = {
'site' => current_site,
'page' => page,
'contents' => Locomotive::Liquid::Drops::ContentTypes.new,
'current_page' => self.params[:page],
}
registers = {
:controller => self,
:site => current_site,
:page => page,
:inline_editor => false,
:blocks => blocks,
}
Liquid::Context.new({}, assigns, registers)
end
end
{% extends 'parent' %}
<!--
This is the template for a page located under /templates/simple-page in Locomotive's page hierarchy.
The html + body tag is located in the template for templates, so it's not shown below
-->
<div id="main">
{% block main_content %}{% endblock %}
</div>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment