25
Liquor, Liquid и другие безопасные языки разметки в RoR Тимофей Цветков, EvilMartians

Liquor, Liquid и другие безопасные языки разметки в RoR

  • Upload
    -

  • View
    99

  • Download
    3

Embed Size (px)

Citation preview

Page 1: Liquor, Liquid и другие безопасные языки разметки в RoR

Liquor, Liquidи другие безопасные языки разметки в

RoR

Тимофей Цветков, EvilMartians

Page 2: Liquor, Liquid и другие безопасные языки разметки в RoR

Safe templates

То, что не страшно разрешить

редактировать пользователю

Page 3: Liquor, Liquid и другие безопасные языки разметки в RoR

Safe Templates?

Shopify

Page 4: Liquor, Liquid и другие безопасные языки разметки в RoR

Safe Templates?

multi site app with custom user design

email templates

Page 5: Liquor, Liquid и другие безопасные языки разметки в RoR

Solutions•Radius http://radius.rubyforge.org

•Ruty http://ruty.rubyforge.org

•Laminate http://github.com/scottpersinger/laminate

•cs/Template http://cstemplate.rubyforge.org

Page 6: Liquor, Liquid и другие безопасные языки разметки в RoR

Radius

context = Radius::Context.new do |c| c.define_tag 'repeat' do |tag| number = (tag.attr['times'] || '1').to_i result = '' number.times { result << tag.expand } result end end

Page 7: Liquor, Liquid и другие безопасные языки разметки в RoR

Ruty

Похож на Liquid

Page 8: Liquor, Liquid и другие безопасные языки разметки в RoR

Rutyclass YourClass def foo 42 end def bar 23 end def delete # delete object here, not possible to do from the # template because not safe end def ruty_safe? name return [:foo, :bar].include?(name) endend

Page 9: Liquor, Liquid и другие безопасные языки разметки в RoR

Laminate

‘Laminate is a system for executing user-written

templates built using the Lua language’

Page 10: Liquor, Liquid и другие безопасные языки разметки в RoR

cs/Template

‘cs/Template is a fast, generic template engine

for Ruby, written in C’

Page 11: Liquor, Liquid и другие безопасные языки разметки в RoR

Liquid

Shopify, Mephisto and many others

Page 12: Liquor, Liquid и другие безопасные языки разметки в RoR

Liquid<ul id="products"> {% for product in products %} <li> <h2>{{product.title}}</h2> Only {{product.price | format_as_money }} <p>{{product.description | prettyprint | truncate: 200 }}</p> </li> {% endfor %} </ul>

Page 13: Liquor, Liquid и другие безопасные языки разметки в RoR

Liquid::Dropclass Post < ActiveRecord::Base

liquid_methods :title, :body end

class Post < ActiveRecord::Basedef to_liquid

PostDrop.new selfend

end

Page 14: Liquor, Liquid и другие безопасные языки разметки в RoR

Liquid::Dropclass Liquid::Drop

alias :[], :invoke_dropdef invoke_drop

methods = self.class.public_instance_methods.map{ |m| m.to_s }

if methods.include? method.to_ssend(method.to_sym)

elsebefore_method(method)

endend

def before_method(method)nil

endend

Page 15: Liquor, Liquid и другие безопасные языки разметки в RoR

Liquid suxx

Drops suxx

methods? named_scopes? associations?

Page 16: Liquor, Liquid и другие безопасные языки разметки в RoR
Page 17: Liquor, Liquid и другие безопасные языки разметки в RoR

Liquor

http://github.com/evilmartians/liquor

Page 18: Liquor, Liquid и другие безопасные языки разметки в RoR

Liquor

class PostDrop < Liquor::Dropliquor_attributes << :title << :bodyliquor_names_scopes << :recent << :for_tag

belongs_to :bloghas_many :commentshas_one :author

end

Page 19: Liquor, Liquid и другие безопасные языки разметки в RoR

Liquor

Filters, content_for and yield tags

Page 20: Liquor, Liquid и другие безопасные языки разметки в RoR

Liquor. Expressions

{% assign playlists = site.playlists|by_name:artist.name %}

{% for artist in site.artists.active|scoped_to:genre %}

Page 21: Liquor, Liquid и другие безопасные языки разметки в RoR

Liquorclass ActiveRecord::NamedScope

def to_liquorself

endend

But only array and .paginate methods are allowed

Page 22: Liquor, Liquid и другие безопасные языки разметки в RoR

Liquor

We use it in production:kasta.rurespectproduction.com (comming soon)

Page 23: Liquor, Liquid и другие безопасные языки разметки в RoR

Красный быстрый

96 капель.Горящий куантро и еще кое-что...

Да, мы будем компилировать

Page 24: Liquor, Liquid и другие безопасные языки разметки в RoR

ror2ru

Накуси выкуси

Page 25: Liquor, Liquid и другие безопасные языки разметки в RoR

Троллинг