`
biyeah
  • 浏览: 202884 次
  • 来自: ...
社区版块
存档分类
最新评论

Markaby (Markup as Ruby)

    博客分类:
  • Ruby
 
阅读更多
http://markaby.rubyforge.org/

Markaby is a very short bit of code for writing HTML pages in pure Ruby. It is an alternative to ERb which weaves the two languages together. Also a replacement for templating languages which use primitive languages that blend with HTML.

Using Markaby as a Rails plugin
Write Rails templates in pure Ruby. Example layout:

 
html do
    head do
      title 'Products: ' + action_name
      stylesheet_link_tag 'scaffold'
    end

    body do
      p flash[:notice], :style => "color: green"

      self << content_for_layout
    end
  end

Using Markaby as a Ruby class
Markaby is flaming easy to call from your Ruby classes.

 require 'markaby'

  mab = Markaby::Builder.new
  mab.html do
    head { title "Boats.com" }
    body do
      h1 "Boats.com has great deals"
      ul do
        li "$49 for a canoe"
        li "$39 for a raft"
        li "$29 for a huge boot that floats and can fit 5 people"
      end
    end
  end
  puts mab.to_s

Markaby::Builder.new does take two arguments for passing in variables and a helper object. You can also affix the block right on to the class.

See Markaby::Builder for all of that.

A Note About instance_eval
The Markaby::Builder class is different from the normal Builder class, since it uses instance_eval when running blocks. This cleans up the appearance of the Markaby code you write. If instance_eval was not used, the code would look like this:

 
mab = Markaby::Builder.new
  mab.html do
    mab.head { mab.title "Boats.com" }
    mab.body do
      mab.h1 "Boats.com has great deals"
    end
  end
  puts mab.to_s

So, the advantage is the cleanliness of your code. The disadvantage is that the block will run inside the Markaby::Builder object’s scope. This means that inside these blocks, self will be your Markaby::Builder object. When you use instance variables in these blocks, they will be instance variables of the Markaby::Builder object.

This doesn’t effect Rails users, but when used in regular Ruby code, it can be a bit disorienting. You are recommended to put your Markaby code in a module where it won’t mix with anything.

A Note About Rails Helpers
When used in Rails templates, the Rails helper object is passed into Markaby::Builder. When you call helper methods inside Markaby, the output from those methods will be output to the stream. This is incredibly handy, since most Rails helpers output HTML tags.

 
head do
    javascript_include_tag 'prototype'
    autodiscovery_link_tag
  end

However, some methods are designed to give back a String which you can use elsewhere. Call the @helpers object with the method and you’ll get the String back and nothing will be output.

  p "Total is: #{@helper.number_to_human_size @file_bytes}"
Conversely, you may call instance variables from your controller by using a method and its value will be returned, nothing will be output.

 
# Inside imaginary ProductController
  def list
    @products = Product.find :all
  end

  # Inside app/views/product/list.mab
  products.each do |product|
    p product.title
  end

A Quick Tour
If you dive right into Markaby, it’ll probably make good sense, but you’re likely to run into a few kinks. Keep these pointers in mind and everything will be fine.

Element Classes
Element classes may be added by hooking methods onto container elements:

 div.entry do
    h2.entryTitle 'Son of WebPage'
    div.entrySection %{by Anthony}
    div.entryContent 'Okay, once again, the idea here is ...'
  end

Which results in:
  <div class="entry">
    <h2 class="entryTitle">Son of WebPage</h2>
    <div class="entrySection">by Anthony</div>
    <div class="entryContent">Okay, once again, the idea here is ...</div>
  </div>

Element IDs
IDs may be added by the use of bang methods:

 
div.page!
    div.content!
      h1 "A Short Short Saintly Dog"
    end
  end

Which results in:

 
<div id="page">
    <div id="content">
      <h1>A Short Short Saintly Dog</h1>
    </div>
  </div>

Markaby assumes XHTML 1.0 Transitional
Output defaults to XHTML 1.0 Transitional. To do XHTML 1.0 Strict, try this:

 
xhtml_strict do
    # innerds
  end

The capture Method
Want to catch a block of HTML as a string and play with it a bit? Use the capture method.

Commonly used to join HTML blocks together:

 
div.menu! \
    ['5.gets', 'bits', 'cult', 'inspect', '-h'].map do |category|
      capture { link_to category }
    end.
    join( " | " )

The tag! Method
If you need to force a tag at any time, call tag! with the tag name followed by the possible arguments and block. The CssProxy won’t work with this technique.

 
tag! :select, :id => "country_list" do
    countries.each do |country|
      tag! :option, country
    end
  end

Credits
Markaby is a work of immense hope by Tim Fletcher and why the lucky stiff. Thankyou for giving it a whirl.

Markaby is inspired by the HTML library within cgi.rb. Hopefully it will turn around and take some cues.
分享到:
评论

相关推荐

    Markup.h和cpp读取xml

    在编程领域,XML(eXtensible Markup Language)是一种用于存储和传输数据的标记语言,它具有结构化、可扩展性和平台无关性等优点。在C++编程中,处理XML文件通常需要借助特定的库来解析和操作XML文档。本文将深入...

    HTML5 Quick Markup Reference

    This book is a condensed reference for HTML5 markup. It presents the essential HTML5 elements and attributes in a well-organized format that can be used as a handy reference. HTML5 Quick Markup ...

    XML解析类 MarkUp

    XML(eXtensible Markup Language)是一种用于标记数据的语言,广泛应用于数据交换、配置文件、文档存储等领域。在IT行业中,处理XML数据是常见的任务,而`MarkUp`类可能是为简化这一过程而设计的一个工具。 `...

    Markup(vc操作XML)

    在IT行业中,XML(eXtensible Markup Language)是一种用于存储和传输数据的标准化格式,尤其在软件开发中被广泛使用。VC++,全称Visual C++,是Microsoft开发的一款强大的C++集成开发环境,它提供了对XML的支持,...

    在VC中通过第三方程序markup实现XML文档处理

    XML(eXtensible Markup Language)作为一种数据交换和存储格式,被广泛应用于配置文件、数据传输、软件接口等场景。为了在VC中处理XML文档,我们可以利用第三方库,比如在本案例中提到的“markup”。 “markup”库...

    Ruby-rabl普通的ruby模板包含jsonbsonxmlplist和msgpack支持

    XML(eXtensible Markup Language)是一种用于标记数据的通用标准,广泛用于数据交换和存储。RABL提供了一种简洁的方式定义XML结构,类似于JSON模板。 ```ruby object @item attribute :title node(:comments) do |...

    Markup(处理vc中XML类库)

    "Markup"是C++中一个用于处理XML的类库,它提供了一种方便的方式来解析和操作XML文档。本文将深入探讨如何使用Markup库在C++中进行XML处理。 XML是一种自描述的、结构化的数据格式,其语法严格,易于机器解析和生成...

    xml之Markup操作类.rar

    XML(eXtensible Markup Language)是一种用于标记数据的语言,广泛应用于数据交换、配置文件、文档存储等领域。在C++编程中,处理XML文件通常需要使用特定的库或类来实现。这里提到的"xml之Markup操作类.rar"提供了...

    操作xml文件Markup类和例子

    XML(eXtensible Markup Language)是一种用于存储和传输数据的标记语言,它以其结构化、自解释性和可扩展性而被广泛应用于软件开发、Web服务以及数据交换等领域。在处理XML文件时,经常会用到各种库或工具,其中一...

    XML操作类简单易用完全源代码Markup

    XML(eXtensible Markup Language)是一种用于标记数据的语言,广泛应用在数据交换、配置文件、文档存储等领域。本文将深入探讨一个名为"Markup"的简单易用的XML操作类库,该库以其轻量级、高效和易于使用的特性受到...

    C++XML工具库(markup库).rar

    在IT行业中,XML(eXtensible Markup Language)是一种被广泛用于存储和传输数据的标记语言,它具有自解释性,结构清晰,易于人和机器阅读。C++,作为一门强大的编程语言,常用于开发高性能的应用程序,但在处理XML...

    MFC Markup库读写XML

    在本Demo中,我们重点探讨如何使用MFC中的Markup库来读写XML文件,这在很多场景下都是非常实用的,比如配置文件的存储、数据交换等。 XML(Extensible Markup Language)是一种自描述性的文本格式,常用于存储结构...

    Markup解析Xml.rar

    在IT领域,XML(eXtensible Markup Language)是一种用于标记数据的标准格式,它强调结构化数据的描述和存储。XML的设计目标是传输和存储数据,而非显示数据,因此它通常与样式表语言如CSS或XSLT结合使用来达到展示...

    Markup 写xml文件

    在IT行业中,XML(eXtensible Markup Language)是一种用于存储和传输数据的标准化格式,尤其在应用程序之间交换数据时非常常见。XML文件是自描述的,这意味着它们包含描述数据结构的信息,使得解析器能够理解并处理...

    Markup_xml解析开源代码_

    XML(eXtensible Markup Language)是一种用于标记数据的语言,广泛应用在软件开发、网络通信、数据存储等领域。本文将深入探讨“Markup_xml解析开源代码”的相关知识点,介绍TinyXML库,一个轻量级且易于使用的XML...

    Class-Markup-instructions.rar_Markup

    "Class-Markup-instructions.rar_Markup"这个压缩包文件提供了一个关于如何在C#或.NET环境中使用Markup类来操作XML的详细教程。这个教程可能涵盖了XML的基础知识,以及如何使用自定义的Markup类进行XML文件的读取和...

    Markup.js, 强大的JavaScript模板.zip

    Markup.js, 强大的JavaScript模板 Markup.js-- 功能强大的JavaScript模板Markup.js 是一个简单而强大的JavaScript模板系统。为什么 Markup.js?Markup.js 将结构化数据转换为HTML标记或者其他文本格式的痛苦。 它

    Application=Code+Markup

    《Application=Code+Markup》是一本专注于Windows Presentation Foundation (WPF) 程序设计的中文书籍。WPF是微软.NET Framework的一部分,它为开发者提供了一种强大的方式来创建具有丰富用户界面的桌面应用程序。这...

    OGC_City_Geography_Markup_Language_CityGML_Encoding_Standard.pdf

    City Geography Markup Language (CityGML) En-coding Standard an OGC Member approved international standard

    markup, 我们用来渲染 README.your_favorite_markup的代码.zip

    markup, 我们用来渲染 README.your_favorite_markup的代码 GitHub标记这个库是存储库中的每个标记文件在 GitHub.com: 上呈现之前的第一步。这里库将原始标记转换为 HTML 。 请参见下面的支持标记标记格式列表。HTML...

Global site tag (gtag.js) - Google Analytics