accessing variables in templates?

108 views
Skip to first unread message

jesus gollonet

unread,
Dec 5, 2011, 5:54:19 AM12/5/11
to middlem...@googlegroups.com
hi
first of all, thanks for middlemanapp.It's really refreshing to craft websites this way and has been my introduction to slim, compass and all that awesomeness.

I have what I suspect is a dumb question. more to do with my inexperience with all this stuff:

Let's say I have three pages which are really similar. sharing most of the css and layout.

index
how-to
buy

let's say I want
- the title of each page to be slightly different
- index needs an addditional css file,
- both index and how-to show on the header a buy button which shouldn't appear on the buy section

ideally I'd like to have a single layout.slim, which then includes/excludes stuff depending on the page which it is rendering. something like (pseudo/invented code)

doctype html
html
  head
    title @page | Awesome webpage
  
    == stylesheet_link_tag :main

    if @page == 'index'
    == stylesheet_link_tag :home
   
   
    meta http-equiv="Content-Type" content="text/html; charset=utf-8"
    .....
   
  body
    nav.lateral-shadow
        == render '_nav'       

    #share
        == render '_share'   

       
    div id="content" class="home lateral-shadow"
        if @page == 'index' || @page == 'how-to'
        == render '_headerbuy'
        else
        == render '_headerbuy'

        == yield

    div id="footer"
        == render '_footer'


 now, I haven't found a way to make such an approach work, but I still don't know the whole picture so
is such a thing possible/sensitive with middleman?

if It is I'd really appreciate a simple example, and if not, a way to structure what I'm after without having to create 3 almost identical templates.

Thanks a lot!
j

Thomas Reynolds

unread,
Dec 5, 2011, 12:44:36 PM12/5/11
to middlem...@googlegroups.com
My suggested solution to your problem would be to:

Write a helper method on your config.rb which looks at the current path and parses it to give you something to write statements off of. Something like:

    helpers do
      def page_name
        if request.path_info.match(/about/)
          :about
        else 
          :other
      end
    end

Then in templates:

    <% if page_name == :about %>

Alternatively, you can use different layouts for different pages.

---
Thomas Reynolds

Reply all
Reply to author
Forward
0 new messages