Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
class ApplicationController < ActionController::Base
...
# FORCE to implement content_for in controller
def view_context
super.tap do |view|
(@_content_for || {}).each do |name,content|
view.content_for name, content
end
end
end
def content_for(name, content) # no blocks allowed yet
@_content_for ||= {}
if @_content_for[name].respond_to?(:<<)
@_content_for[name] << content
else
@_content_for[name] = content
end
end
def content_for?(name)
@_content_for[name].present?
end
end
class PostsController < ApplicationController
def index
content_for :title, "List of posts"
...
end
end
@TylerRick

This comment has been minimized.

Show comment Hide comment
@TylerRick

TylerRick Aug 1, 2011

This is exactly what I was looking for, thank you! Working for me in Rails 3.0.9.

My only suggestion would be to move it to a module ContentForInController so you don't clutter up your ApplicationController (I put mine in lib/content_for_in_controller.rb) and release it as a gem for easy reusability.

This is exactly what I was looking for, thank you! Working for me in Rails 3.0.9.

My only suggestion would be to move it to a module ContentForInController so you don't clutter up your ApplicationController (I put mine in lib/content_for_in_controller.rb) and release it as a gem for easy reusability.

@hiroshi

This comment has been minimized.

Show comment Hide comment
@hiroshi

hiroshi Aug 2, 2011

TylerRick, thanks for your comment. I have thought that if someone is interested in it, it can be worth releasing as a gem. It seems to be the time :)

Owner

hiroshi commented Aug 2, 2011

TylerRick, thanks for your comment. I have thought that if someone is interested in it, it can be worth releasing as a gem. It seems to be the time :)

@MarioRicalde

This comment has been minimized.

Show comment Hide comment
@MarioRicalde

MarioRicalde Dec 6, 2011

Awesome work hiroshi. Kudos! I suggest you release it as a gem! it's the best time to do it!

Awesome work hiroshi. Kudos! I suggest you release it as a gem! it's the best time to do it!

@hiroshi

This comment has been minimized.

Show comment Hide comment
@hiroshi

hiroshi Dec 6, 2011

For now, I'v not used rails for my projects. @kuroir, It's OK to release as a gem by you, if you wish. I'll appreciate to have my name or url for this gist on README :-)

Owner

hiroshi commented Dec 6, 2011

For now, I'v not used rails for my projects. @kuroir, It's OK to release as a gem by you, if you wish. I'll appreciate to have my name or url for this gist on README :-)

@clm-a

This comment has been minimized.

Show comment Hide comment
@clm-a

clm-a Jan 13, 2012

I made a over basic gem to embed your code with style :)

I would love any feedback on how to inject the module a bit cleaner than calling send :include on ActionController::Base...

Thanks !

clm-a commented Jan 13, 2012

I made a over basic gem to embed your code with style :)

I would love any feedback on how to inject the module a bit cleaner than calling send :include on ActionController::Base...

Thanks !

@hiroshi

This comment has been minimized.

Show comment Hide comment
@hiroshi

hiroshi Jan 14, 2012

@clmntlxndr
Thanks. I just glad to see that my idea is usable for someone other than me :-)
As for the way to inject the module, I don't have any idea of better way for now.

Owner

hiroshi commented Jan 14, 2012

@clmntlxndr
Thanks. I just glad to see that my idea is usable for someone other than me :-)
As for the way to inject the module, I don't have any idea of better way for now.

@felipesabino

This comment has been minimized.

Show comment Hide comment
@felipesabino

felipesabino Jul 7, 2012

Awesome gist!

Is there a way to prevent HTML to be escaped when appending the content? I am trying to do something like

  content_for(:head, render_to_string(:partial => "layouts/my_partial" ))

Awesome gist!

Is there a way to prevent HTML to be escaped when appending the content? I am trying to do something like

  content_for(:head, render_to_string(:partial => "layouts/my_partial" ))
@felipesabino

This comment has been minimized.

Show comment Hide comment
@felipesabino

felipesabino Jul 7, 2012

Sorry, just remembered about .html_safe...

I ended up changing the gist a bit to call html_safe before adding it to the content

Sorry, just remembered about .html_safe...

I ended up changing the gist a bit to call html_safe before adding it to the content

@MarioRicalde

This comment has been minimized.

Show comment Hide comment
@MarioRicalde

MarioRicalde Jul 7, 2012

@chrishough

This comment has been minimized.

Show comment Hide comment
@chrishough

chrishough Mar 2, 2014

Thank you so much for posting this! I was able to use this solution to override my page titles. Cheers.

Thank you so much for posting this! I was able to use this solution to override my page titles. Cheers.

@mahemoff

This comment has been minimized.

Show comment Hide comment
@mahemoff

mahemoff Jan 5, 2015

Simple hack to make it work with a block - just do @_content_for||=yield

I cut-pasted this to make provide for controllers - https://gist.github.com/hiroshi/985457

mahemoff commented Jan 5, 2015

Simple hack to make it work with a block - just do @_content_for||=yield

I cut-pasted this to make provide for controllers - https://gist.github.com/hiroshi/985457

@MaG21

This comment has been minimized.

Show comment Hide comment
@MaG21

MaG21 Apr 24, 2015

While this gist comes handy, it's too much just to use it to change the page titles. If someone wants to use this gist just to change the title of the pages dynamically it would be better just to put the following in the layout:

<title><%= @title || "default title" %></title>

With that approach one will just have to set the @title instance variable in the controller, like this:

@title = 'My title'

MaG21 commented Apr 24, 2015

While this gist comes handy, it's too much just to use it to change the page titles. If someone wants to use this gist just to change the title of the pages dynamically it would be better just to put the following in the layout:

<title><%= @title || "default title" %></title>

With that approach one will just have to set the @title instance variable in the controller, like this:

@title = 'My title'

@phil-monroe

This comment has been minimized.

Show comment Hide comment
@phil-monroe

phil-monroe Oct 28, 2015

Sorry to stir up an old topic, but what about memoizing the view_context? Will that cause other issues? Seems to work for me in rails 4.

  def view_context
    @view_context ||= super
  end
  delegate :content_for, to: :view_context

EDIT
Found out real quick: Don't do this, it breaks instance variable assignment...

Sorry to stir up an old topic, but what about memoizing the view_context? Will that cause other issues? Seems to work for me in rails 4.

  def view_context
    @view_context ||= super
  end
  delegate :content_for, to: :view_context

EDIT
Found out real quick: Don't do this, it breaks instance variable assignment...

@itkin

This comment has been minimized.

Show comment Hide comment
@itkin

itkin Feb 14, 2017

seems broken on rails 4

itkin commented Feb 14, 2017

seems broken on rails 4

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment