$30 off During Our Annual Pro Sale. View Details »

Dynamiczne szablony - Liquid Markup

Dynamiczne szablony - Liquid Markup

Krzysztof Wawer

March 18, 2014
Tweet

More Decks by Krzysztof Wawer

Other Decks in Programming

Transcript

  1. Dynamiczne szablony -
    Liquid Markup
    Krzysztof Wawer!
    !
    github.com/wafcio
    [email protected]

    View Slide

  2. Co to jest Liquid?
    github.com/Shopify/liquid

    View Slide

  3. Renderowanie szablonu
    Liquid::Template.parse('Hello {{who}}').
    render({ 'who' => 'world' })
    => "Hello world"

    View Slide

  4. Zmienne i tagi
    "{{title}}"
    "{% post %}"
    - atrybut, zmienna
    - tag

    View Slide

  5. Filtry w Liquid
    "Hello {{ 'tobi' | capitalize}}"
    "Today is {{ 'now' | date: "%Y %h"}}"

    View Slide

  6. Własne filtry
    module TextFilter
    def textilize(input)
    RedCloth.new(input).to_html
    end
    end
    !
    Liquid::Template.register_filter(TextFilter)
    !
    Liquid::Template.parse(" {{ '*hi*' | textilize }} ").
    render({}, filters: [TextFilter])
    # => "hi"

    View Slide

  7. Liquid Tag
    class ExampleTag < Liquid::Tag
    def initialize(name, markup, token)
    !
    end
    !
    def render(context)
    end
    end
    !
    Liquid::Template.register_tag('example_menu', ExampleTag)
    !
    Liquid::Template.parse("{% example_tag key1=value1%}").render

    View Slide

  8. Liquid Drop
    class PostDrop < Liquid::Drop
    def initialize(post)
    @post = post
    end
    !
    def title
    @post.title
    end
    end
    Liquid::Template.parse(„{{post.title}}, {{post.content}}”).
    render({ 'post' => PostDrop.new(Post.find(id)) })
    => "Post title, "

    View Slide