pihentagy asked:

page templates

Tags: template

Hi!

How can I easily do a page template globally to my nitro app? And if I want it to be controller-local?

Where can the css for the controllers be?

(1 attempts)

Kashia answered:

In Nitro this is commonly done like so:

h3. The Skin

require 'nitro/element'

class Page < Nitro::Element
  
  def header
    %~<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
                "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
      <head>
        <title>#@title</title>
         <link rel="stylesheet" href="/css/main.css" type="text/css" media="screen" title="standard style" charset="utf-8" />
      </head>
      <body>
    ~
  end
  
  def footer
    %~
      </body>
    </html>
    ~
  end
  
  def render  
    %~#{header}
      #{content}
      #{footer}
    ~
  end
end

This basically sets up a basic structure of your pages. (As you can see, a css file is in: "/css/" which is in $app/public/)

This code is often put into $app/src/skin.rb because it is used kind of like a skin for all of your pages.

h3. How to use it

In your run.rb require the skin.rb

# ...
require 'src/skin.rb'
# ...

And then use it in your templates:

templates/index.xthml :

<Page title="Index Page">
  <!-- put all your other html code here, which goes into the body tag -->
</page>

What you could do, is create several of those Page Elements and use them in the templates of the controller you want it to work in.

I hope that helps.

Rating: 5