---
title: "Haml&Radius 不错"
author: "xxj"
date: "2008-08-12T14:52:06Z"
tags: []
slug: "haml-radius"
draft: false
lastmod: "2008-08-12T14:52:06Z"
id: 227025
url: "https://mmm.iteye.com/blog/227025"
---

**Haml**
http://haml.hamptoncatlin.com/
今天花了点时间读了遍Reference:http://haml.hamptoncatlin.com/docs/rdoc/classes/Haml.html

对haml的印象彻底改变了，说句老实话，之前懒得看这个东西，一直认为它没啥意思，今天才发现，使用他，代码简洁多了，而且很容易上手，成本比较低。


```ruby

!!! XML
!!!
%html
  %head/
  %body
    #main
      .articles
        .article.title
          Hello World
        .article.date
          2008-8-12 14:10:58
        .article.content
          Content goes here 

```


生成如下的代码：


```html
<?xml version='1.0' encoding='utf-8' ?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

    <html>
      <head />
      <body>
        <div id='main'>
          <div class='articles'>
            <div class='article title'>
              Hello World
            </div>
            <div class='article date'>

              2008-8-12 14:10:58
            </div>
            <div class='article content'>
              Content goes here
            </div>
          </div>
        </div>
      </body>
    </html>


```


在haml中，我们还可以直接写textile文本，如

```ruby

#main
  .articles
    .article.title
      :redcloth
        h2. Hello World
    .article.date
      :markdown
        _2008-8-12 14:10:58_         

```


生成

```html
<div id='main'>
      <div class='articles'>
        <div class='article title'>
          <h2>Hello World</h2>
        </div>

        <div class='article date'>
          <p><em>2008-8-12 14:10:58</em></p>
        </div>
      </div>
    </div>

```


资料：
http://redcloth.org/
http://www.rubyinside.com/redcloth-4-released-962.html

来个更复杂点的内容(来自Radiant的application.html.haml)：

```ruby

!!!
%html{html_attrs}
  %head
    %meta{"http-equiv"=>"Content-type", :content=>"text/html; charset=utf-8"}/
    %title= @page_title || default_page_title
    - @stylesheets.uniq.each do |stylesheet|
      = stylesheet_link_tag stylesheet
    - @javascripts.uniq.each do |javascript|
      = javascript_include_tag javascript
    /[if lt ie 7]
      %script{:type=>"text/javascript", :src=>"/admin/javascripts/pngfix.js"}
    - if @content_for_page_scripts
      = javascript_tag @content_for_page_scripts
    - if @content_for_page_css
      %style{:type => "text/css"}= @content_for_page_css
  %body
    #page
      #header
        #site-title= link_to_unless_current title, admin_url
        #site-subtitle= subtitle
        - if logged_in?
          #navigation= links_for_navigation
      %hr{:class=>"hidden"}/
      #main
        - if flash[:notice]
          #notice
            %p= flash[:notice]
        - if flash[:error]
          #error
            %p= flash[:error]
        #content
          = find_and_preserve(yield)
      %hr{:class=>"hidden"}/
      #footer
        %p 
          This site was made with Ruby and is powered by 
          %a{:href=>"http://radiantcms.org"} Radiant CMS
          version 
          = Radiant.loaded_via_gem? ? "#{Radiant::Version} (gem)." : "#{Radiant::Version}."
        %p#site-links
          - if logged_in?
            - if admin?
              = nav_link_to 'Users', user_index_url
              %span.separator=" | "
              = nav_link_to 'Extensions', extension_index_url
            - else
              = nav_link_to 'Preferences', user_preferences_url
            %span.separator=" | "
            = nav_link_to 'Log Out', logout_url
            %span.separator=" | "
            = link_to image('view-site.gif', :alt => "View Site", :title => ''), homepage_url
      - if @content_for_popups
        #popups
          = yield :popups

```


**Radius**
http://radius.rubyforge.org/

Radius 是一个强大的基于标签的模板语言。

我们引用下文档中的示例：
 
```ruby
 require 'radius'


  context = Radius::Context.new do |c|
    c.define_tag 'hello' do
      'Hello world'
    end
    c.define_tag 'repeat' do |tag|
      number = (tag.attr['times'] || '1').to_i
      result = ''
      number.times { result << tag.expand }
      result
    end
  end
```


在页面中，我们编写如下代码：


```ruby

<radius:repeat times="3">
  * <radius:hello />!
</radius:repeat>

```


生成

> 
>   * Hello world!
>   * Hello world!
>   * Hello world!
> 


有人问，这个东西和helper不一样吗？
恩... 差不多，但是如果你的应用程序涉及到客户编码的话，那么提供这一堆tag应该还是不个不错的主意，譬如最常见的，用户自定义模板、皮肤等，现在的博客仅仅是规定死的布局和内容，无法用户完全自定义的。