- 浏览: 337333 次
- 性别:
- 来自: 北京
文章分类
最新评论
Markaby is a very short bit of code for writing HTML pages in pure Ruby
- 博客分类:
- Ruby On Rails
闲逛github...发现这个东东,why出品,有点像haml
URL:http://github.com/why/markaby/tree/master[url][/url]
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:
== Using Markaby as a Ruby class
Markaby is flaming easy to call from your Ruby classes.
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 <tt>instance_eval</tt>
The Markaby::Builder class is different from the normal Builder class,
since it uses <tt>instance_eval</tt> when running blocks. This cleans
up the appearance of the Markaby code you write. If <tt>instance_eval</tt>
was not used, the code would look like this:
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, <tt>self</tt> 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 affect 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.
= The Six Steps of Markaby
If you dive right into Markaby, it'll probably make good sense, but you're
likely to run into a few kinks. Why not review these six steps and commit
them memory so you can really *know* what you're doing?
== 1. Element Classes
Element classes may be added by hooking methods onto container elements:
Which results in:
== 2. Element IDs
IDs may be added by the use of bang methods:
Which results in:
== 3. Validate Your XHTML 1.0 Output
If you'd like Markaby to help you assemble valid XHTML documents,
you can use the <tt>xhtml_transitional</tt> or <tt>xhtml_strict</tt>
methods in place of the normal <tt>html</tt> tag.
This will add the XML instruction and the doctype tag to your document.
Also, a character set meta tag will be placed inside your <tt>head</tt>
tag.
Now, since Markaby knows which doctype you're using, it checks a big
list of valid tags and attributes before printing anything.
>> div :styl => "padding: 10px" do
>> img :src => "samorost.jpg"
>> end
InvalidHtmlError: no such attribute `styl'
Markaby will also make sure you don't use the same element ID twice!
== 4. Escape or No Escape?
Markaby uses a simple convention for escaping stuff: if a string
is an argument, it gets escaped. If the string is in a block, it
doesn't.
This is handy if you're using something like RedCloth or
RDoc inside an element. Pass the string back through the block
and it'll skip out of escaping.
But, if we have some raw text that needs escaping, pass it in
as an argument:
One caveat: if you have other tags inside a block, the string
passed back will be ignored.
The final div above won't appear in the output. You can't mix
tag modes like that, friend.
== 5. Auto-stringification
If you end up using any of your Markaby "tags" as a string, the
tag won't be output. It'll be up to you to add the new string
back into the HTML output.
This means if you call <tt>to_s</tt>, you'll get a string back.
But, when you're adding strings in Ruby, <tt>to_s</tt> happens automatically.
Interpolation works fine.
And any other operation you might perform on a string.
== 6. The <tt>tag!</tt> Method
If you need to force a tag at any time, call <tt>tag!</tt> with the
tag name followed by the possible arguments and block. The CssProxy
won't work with this technique.
= 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.
However, some methods are designed to give back a String which you can use
elsewhere. That's okay! Every method returns a Fragment object, which can
be used as a string.
Also see the Quick Tour above, specifically the stuff about auto-stringification.
If for any reason you have trouble with fragments, you can just
call the <tt>@helpers</tt> object with the method and you'll get
the String back and nothing will be output.
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
= 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.
URL:http://github.com/why/markaby/tree/master[url][/url]
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 <tt>instance_eval</tt>
The Markaby::Builder class is different from the normal Builder class,
since it uses <tt>instance_eval</tt> when running blocks. This cleans
up the appearance of the Markaby code you write. If <tt>instance_eval</tt>
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, <tt>self</tt> 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 affect 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.
= The Six Steps of Markaby
If you dive right into Markaby, it'll probably make good sense, but you're
likely to run into a few kinks. Why not review these six steps and commit
them memory so you can really *know* what you're doing?
== 1. 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>
== 2. Element IDs
IDs may be added by the use of bang methods:
div.page! { div.content! { h1 "A Short Short Saintly Dog" } }
Which results in:
<div id="page"> <div id="content"> <h1>A Short Short Saintly Dog</h1> </div> </div>
== 3. Validate Your XHTML 1.0 Output
If you'd like Markaby to help you assemble valid XHTML documents,
you can use the <tt>xhtml_transitional</tt> or <tt>xhtml_strict</tt>
methods in place of the normal <tt>html</tt> tag.
xhtml_strict do head { ... } body { ... } end
This will add the XML instruction and the doctype tag to your document.
Also, a character set meta tag will be placed inside your <tt>head</tt>
tag.
Now, since Markaby knows which doctype you're using, it checks a big
list of valid tags and attributes before printing anything.
>> div :styl => "padding: 10px" do
>> img :src => "samorost.jpg"
>> end
InvalidHtmlError: no such attribute `styl'
Markaby will also make sure you don't use the same element ID twice!
== 4. Escape or No Escape?
Markaby uses a simple convention for escaping stuff: if a string
is an argument, it gets escaped. If the string is in a block, it
doesn't.
This is handy if you're using something like RedCloth or
RDoc inside an element. Pass the string back through the block
and it'll skip out of escaping.
div.comment { RedCloth.new(str).to_html }
But, if we have some raw text that needs escaping, pass it in
as an argument:
div.comment raw_str
One caveat: if you have other tags inside a block, the string
passed back will be ignored.
div.comment { div.author "_why" div.says "Torpedoooooes!" "<div>Silence.</div>" }
The final div above won't appear in the output. You can't mix
tag modes like that, friend.
== 5. Auto-stringification
If you end up using any of your Markaby "tags" as a string, the
tag won't be output. It'll be up to you to add the new string
back into the HTML output.
This means if you call <tt>to_s</tt>, you'll get a string back.
div.title { "Rock Bottom" + span(" by Robert Wyatt").to_s }
But, when you're adding strings in Ruby, <tt>to_s</tt> happens automatically.
div.title { "Rock Bottom" + span(" by Robert Wyatt") }
Interpolation works fine.
div.title { "Rock Bottom #{span(" by Robert Wyatt")}" }
And any other operation you might perform on a string.
div.menu! \ ['5.gets', 'bits', 'cult', 'inspect', '-h'].map do |category| link_to category end. join( " | " )
== 6. The <tt>tag!</tt> Method
If you need to force a tag at any time, call <tt>tag!</tt> 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
= 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. That's okay! Every method returns a Fragment object, which can
be used as a string.
p { "Total is: #{number_to_human_size @file_bytes}" }
Also see the Quick Tour above, specifically the stuff about auto-stringification.
If for any reason you have trouble with fragments, you can just
call the <tt>@helpers</tt> object with the method and you'll get
the String back and nothing will be output.
p { "Total is: #{@helpers.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
= 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.
发表评论
-
新博客
2012-04-23 20:47 1778https://db-china.org -
Draper: View Models for Rails
2011-10-07 01:19 2285Draper是一个Ruby gem,它让Rails model ... -
Active Record batch processing in parallel processes
2011-10-07 01:20 2280Active Record 提供 find_each来分批处理 ... -
答复: Sinatra:一个可以作为Rails有益补充的框架. 简洁而不简单
2010-04-07 18:21 1665既然是这么简单的事情,用rack写也比较有趣: 一共5个文件, ... -
mass-assignment protection【?】
2010-03-18 18:31 101Attributes named in this ma ... -
基于jquery和mini_magick的图片裁剪
2009-12-04 19:32 3840jquery imgAeraSelect插件地址:http:/ ... -
Showing SQL statements in the Rails console
2009-10-23 13:29 19881.windows下创建_irbrc文件,并设置环境变量 2. ... -
Security Tips
2009-10-01 17:28 987Hackers Love Mass Assignment 对于 ... -
HTTP request 相关
2009-09-25 13:52 1268>> app.request.query_para ... -
可定制的Rails错误回显
2009-09-16 09:30 2851通常rails页面的错误信息提示都是放在首部用 error_m ... -
用户注册邮件激活
2009-09-04 17:26 50341.网站用户相关表中有一个字段用来记录用户帐号是否激活。 ... -
REST in Rails资料收集
2009-08-25 13:08 1262LetRails的一系列介绍: ... -
check-if-record-was-just-destroyed-in-rails
2009-08-20 11:02 1031问题: So there is record.new_ ... -
Using indexes in rails: Index your associations
2009-08-19 14:23 1161Many rails developers are g ... -
委托 in rails
2009-08-09 01:16 1174Delegation is a feature Rails ... -
还是习惯用法好啊。。。
2009-08-08 09:00 1229在看ASCIICast的rails示例里面看到这样的代码: ... -
弄了个小论坛...
2009-08-07 05:27 1094http://github.com/hooopo/Rails- ... -
I18n for Rails之hello world
2009-08-01 04:52 1194平台:rails 2.3.2 ruby 1.8.6 1.rai ... -
Using with_scope
2009-07-29 01:57 868In this episode we’ll talk abou ... -
Move Find Into Model
2009-07-29 01:52 994下面是在控制器里面通过model查找未完成的任务: c ...
相关推荐
Writing and Script_ A Very Short Introduction (Very Short Introductions) - Robinson, Andrew.mobi
Oxford Very Short Introduction系列,History of Mathematics by The - Stedall, Jacqueline
A Very Short Introduction
《应用数学:非常简短的介绍》是一本旨在为初学者和感兴趣的人提供数学应用领域基础知识的书籍。作为“非常简短的介绍”系列的一部分,这本书由专家撰写,旨在用简洁明了的语言来阐述复杂的数学概念,使得读者能够...
Ancient Egypt: A Very Short Introduction, Ian Shaw 系列 pdf
We are now at a point where the collection and storage of data is growing at a rate unimaginable only a few decades ago but, as we will see in this book, new data analysis techniques are transforming...
ruby interpreter 原理探討 At first glance, learning how to use Ruby can seem fairly simple. Developers around the world find Ruby’s syntax to be graceful and straight...Ruby is a very complex tool.
This is a very good tic tac toe game for 2 players with the option to select who goes first. Also has good sound effects and graphics. Good for beginners to learn.
The Dictionary of Pure and Applied Physics (DPAP) is one of three physics dictionaries being published by CRC Press LLC, the other two being the Dictionary of Material Science and High En- ergy ...
DataGridView is a very powerful tool to display data in tabular format; However, there are no provider which has been thought to print it easily. The Library provides the developer with a seamless ...
But in actual process, it is very difficult to attain desired result, since it depends on various factors. Communication is vital factor which affect the performance of system. UART is a kind of ...
This book is for beginning programmers, programmers new to Ruby, and web developers interested in learning and knowing the foundations of the Ruby programming language. Table of Contents Part 1: ...
用matlab编写有限元程序The goal of this document is to give a very brief overview and direction in the writing of nite element code using Matlab. It is assumed that the reader has a basic familiarity ...
This, combined with the vast amount of dependencies in the kernel and that it is not easy to see all the consequences of a kernel change, demands developers with a relative full understanding of the ...
this is a very very file
In a 32-bit operation system, there is normally a linear array of 2^32 addresses representing 4,294,967,269 byte addresses. Physical Memory A series of physical locations, with unique addresses, that...
A CLR module is a byte stream, typically stored as a file in the local file system or on a Web server. As shown in Figure 2.1, a CLR module uses an extended version of the PE/COFF executable file ...
Indeed, in programming, the idea that you can have abstractions for different components in a large system is a very powerful concept. It means that instead of having to understand every single line ...
The goal of this guide is to manage this complexity by describing in detail the dos and don'ts of writing C++ code. These rules exist to keep the code base manageable while still allowing coders to ...