Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Views, from the top

Tim Riley
February 08, 2019

Views, from the top

/Veni, vidi, view-ci/ – I came, I saw, I left the views in a mess.

With server-rendered HTML still delivering most of the web, our views deserve more than a grab bag of helpers.

Come along and learn the tools and techniques to conquer the design challenges that a complex view layer presents.

Tim Riley

February 08, 2019
Tweet

More Decks by Tim Riley

Other Decks in Technology

Transcript

  1. class Show < Dry::View config.template = "articles/show" expose :article do

    |slug:| article_repo.find_by_slug(slug) end end view = Show.new(article_repo: repo)
  2. class Show < Dry::View config.template = "articles/show" expose :article do

    |slug:| article_repo.find_by_slug(slug) end end view = Show.new(article_repo: repo)
  3. class Show < Dry::View config.template = "articles/show" expose :article do

    |slug:| article_repo.find_by_slug(slug) end end view = Show.new(article_repo: repo)
  4. dry-view requirements • Views as objects • Explicit template locals

    • Simple templates • View logic on decorated values
  5. dry-view requirements • Views as objects • Explicit template locals

    • Simple templates • View logic on decorated values • View facilities are automatic
  6. dry-view requirements • Views as objects • Explicit template locals

    • Simple templates • View logic on decorated values • View facilities are automatic • View facilities are integrated
  7. class Parts::Article < Dry::View::Part def share_widget render( :share_widget, url: url,

    title: title, preview_text: body_preview_text, ) end end
  8. dry-view requirements • Views as objects • Explicit template locals

    • Simple templates • View logic on decorated values • View facilities are automatic • View facilities are integrated • View logic on specific templates
  9. - show_author = \ defined?(show_author) ? show_author : false -

    link_prefix = \ defined?(link_prefix) ? link_prefix : "Related: " .related-article a href=article.url = "#{link_prefix} #{article.title}" - if show_author .author …
  10. - show_author = \ defined?(show_author) ? show_author : false -

    link_prefix = \ defined?(link_prefix) ? link_prefix : "Related: " .related-article a href=article.url = "#{link_prefix} #{article.title}" - if show_author .author …
  11. - show_author = \ defined?(show_author) ? show_author : false -

    link_prefix = \ defined?(link_prefix) ? link_prefix : "Related: " .related-article a href=article.url = "#{link_prefix} #{article.title}" - if show_author .author … 4c391850cabe4809a6b6ab1a155529… 16 March 2016 Totally wrote this code. FIXME!
  12. class Scopes::RelatedArticle < Dry::View::Scope def show_author? locals.fetch(:show_author, false) end def

    link_text prefix = locals.fetch(:link_prefix, "Related:") "#{prefix} #{article.title}” end end
  13. class Scopes::RelatedArticle < Dry::View::Scope def show_author? locals.fetch(:show_author, false) end def

    link_text prefix = locals.fetch(:link_prefix, "Related:") "#{prefix} #{article.title}” end end
  14. class Scopes::RelatedArticle < Dry::View::Scope def show_author? locals.fetch(:show_author, false) end def

    link_text prefix = locals.fetch(:link_prefix, "Related:") "#{prefix} #{article.title}” end end
  15. class Scopes::RelatedArticle < Dry::View::Scope def show_author? locals.fetch(:show_author, false) end def

    link_text prefix = locals.fetch(:link_prefix, "Related:") "#{prefix} #{article.title}" end end
  16. dry-view requirements • Views as objects • Explicit template locals

    • Simple templates • View logic on decorated values • View facilities are automatic • View facilities are integrated • View logic on specific templates
  17. dry-view requirements • Views as objects • Explicit template locals

    • Simple templates • View logic on decorated values • View facilities are automatic • View facilities are integrated • View logic on specific templates • Common helpers
  18. class Context < Dry::View::Context def initialize(assets:, **args) @assets = assets

    super(**args) end def asset_path(asset_name) @assets[asset_name] end end
  19. class Context < Dry::View::Context def initialize(assets:, **) @assets = assets

    super end def asset_path(asset_name) @assets[asset_name] end end
  20. class Context < Dry::View::Context def initialize(assets:, **) @assets = assets

    super end def asset_path(asset_name) @assets[asset_name] end end
  21. class Context < Dry::View::Context def initialize(assets:, **) @assets = assets

    super end def asset_path(asset_name) @assets[asset_name] end end
  22. dry-view requirements • Views as objects • Explicit template locals

    • Simple templates • View logic on decorated values • View facilities are automatic • View facilities are integrated • View logic on specific templates • Common helpers
  23. View concepts • View - config, dependencies, exposures • Exposures

    - prepare values • Template & partials - markup
  24. View concepts • View - config, dependencies, exposures • Exposures

    - prepare values • Template & partials - markup • Parts - behaviour on values
  25. View concepts • View - config, dependencies, exposures • Exposures

    - prepare values • Template & partials - markup • Parts - behaviour on values • Scopes - behaviour for templates
  26. View concepts • View - config, dependencies, exposures • Exposures

    - prepare values • Template & partials - markup • Parts - behaviour on values • Scopes - behaviour for templates • Context - baseline rendering environment