Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nested ERB template layouts like Liquid?

With Liquid, you can nest layouts. E.g., my site's default layout builds off of the base layout.

Is this possible with ERB? If so, how?

I'm asking because I'm migrating a GitHub Pages site to Sinatra, so that I can handle forms natively (not with Wufoo or Google Forms).

like image 948
ma11hew28 Avatar asked Dec 06 '25 19:12

ma11hew28


1 Answers

try this:

make a file called layout.haml (or erb or whatever your templates are in), and put it in the views folder. this is your site layout, and it could look like this (I'm using haml):

 %html
  %head 
    %link(rel="stylesheet" type="text/css" href="style.css")  
  %body
    %div.outer
      %div.inner
        = yield

the magic part is the =yield this is where Sinatra will render whatever template you call in your route. the = tells your template to expect ruby code.

for example, if your route is

get '/' do 
  haml :index
end

then your template at /views/index.haml will be inserted. you can also use a custom layout if you tell your template to override the one at /views/layout.haml.

hope this helps.

like image 145
corneliusk Avatar answered Dec 09 '25 01:12

corneliusk