I came across the view helper function "provide" today. By looking into its manual I am still confused on how it is different from "content_for".
provide(name, content = nil, &block)
The same as content_for but when used with streaming flushes straight back to the layout. In other words, if you want to concatenate several times to the same buffer when rendering a given template, you should use content_for, if not, use provide to tell the layout to stop looking for more contents.
Question 1: this is quite abstract to me - could anyone flesh it out by giving a demonstrative example?
Question 2: working with asset pipeline, which performs better and why?
Thanks!
First of all, what is streaming? Why would you use it?
Streaming is alternate method of rendering pages top-down (outside-in). The default rendering behavior is inside-out. Streaming must be enabled in your controller:
According to the documentation:
So, if you're not using streaming, is there still a difference?
Yes.
The difference is a template can define multiple content blocks by calling
content_formultiple times. Doing so will concatenate the blocks and pass that to the layout:Since
providedoesn't continue searching the provided template, only the block passed to the firstprovidecall will be sent to the template: