#[1]gnikyt feed [2]gnikyt Code ramblings. ActiveRecord and Liquid Drops with Sinatra /* Oct 15, 2015 — 3.7KB */ Normally I use ERB when doing templates but there was a special use-case recently where I needed to allow the app user to modify a specific template. [3]Liquid templates (what Jekyll and Shopify use) was perfect for this situation because it has a simple syntax for anyone to pick up and it’s also safe due to its scope limited to what’s passed to the template. Let’s suppose you have two models in your Sinatra application – Student and Book. # app/models/student_model.rb module MyApp module Model class Student has_many :books end end end # app/models/book_model.rb module MyApp module Model class Book belongs_to :student end end end Now, Liquid has what they call a [4]Drop, where you can easily build a plain class which Liquid can then use directly when rendering the template. As the [5]Drop Introduction states, it acts “like” a hash so the methods you create can be accessed with dot-notation in the template. Example: {% raw %}{{ student.full_name }}{% endraw %} So let’s create a Student drop and a Book drop and give the templates some basic variables from the model to access. # app/drops/student_drop.rb module MyApp module Drop class Student def initialize(student) @student = student end def full_name "#{@student.first_name} {@student.last_name}" end def allergies @student.allergies.split ',' end def books @student.books end end end end # app/drops/book_drop.rb module MyApp module Drop class Book def initialize(book) @book = book end def title @book.title end def barcode @book.barcode end def date_printed @book.date_printed end end end end Ok so now we have our models and drops setup. As you can see above we’ve set up methods for accessing the student’s name, their books, the book title, barcode, etc. All of these methods can be used in a Liquid template now. But now, how do we easily combine the two? Easy, we utilize to_liquid method by adding it to our model which Liquid calls when rendering the template. We then return the proper drop for the model inside the new to_liquid method. # app/models/student_model.rb module MyApp module Model class Student has_many :books def to_liquid Drop::Student.new self end end end end # app/models/book_model.rb module MyApp module Model class Book belongs_to :student def to_liquid Drop::Book.new self end end end end That’s pretty much it. We’ve now: 1. Created our models 2. Created our drops 3. Modified our models to return a drop With all this combined we can then use it all together as such: # app/controllers/front_controller.rb ... student = Model::Student.find params[:id] liquid :'front/test', layout: :layout_front, locals: {'student' => student} {% raw %} # app/views/front/test.liquid My name is {{ student.full_name }}. I have {{ student.books.size }} books! {% endraw %} That’s it for this little how-to. I will note there are other ways to expose your ActiveRecord models to work automatically with Liquid but this article just shows you a more expanded and controlled version. [6]MD | [7]TXT | [8]CC-4.0 This post is 9 years old and may contain outdated information. __________________________________________________________________ [9]Ty King Ty King A self-taught, seasoned, and versatile developer from Newfoundland. Crafting innovative solutions with care and expertise. See more [10]about me. [11]Github [12]LinkedIn [13]CV [14]RSS * * * * * * * * * * References Visible links: 1. /rss.xml 2. / 3. http://liquidmarkup.org/ 4. https://github.com/Shopify/liquid/wiki/Introduction-to-Drops 5. https://github.com/Shopify/liquid/wiki/Introduction-to-Drops 6. /activerecord-and-liquid-drops-with-sinatra/index.md 7. /activerecord-and-liquid-drops-with-sinatra/index.txt 8. https://creativecommons.org/licenses/by/4.0/ 9. /about 10. /about 11. https://github.com/gnikyt 12. https://linkedin.com/in/gnikyt 13. /assets/files/cv.pdf 14. /rss.xml Hidden links: 16. localhost/tmp/lynxXXXXxKSMtn/L769536-597TMP.html#cb1-1 17. localhost/tmp/lynxXXXXxKSMtn/L769536-597TMP.html#cb1-2 18. localhost/tmp/lynxXXXXxKSMtn/L769536-597TMP.html#cb1-3 19. localhost/tmp/lynxXXXXxKSMtn/L769536-597TMP.html#cb1-4 20. localhost/tmp/lynxXXXXxKSMtn/L769536-597TMP.html#cb1-5 21. localhost/tmp/lynxXXXXxKSMtn/L769536-597TMP.html#cb1-6 22. localhost/tmp/lynxXXXXxKSMtn/L769536-597TMP.html#cb1-7 23. localhost/tmp/lynxXXXXxKSMtn/L769536-597TMP.html#cb1-8 24. localhost/tmp/lynxXXXXxKSMtn/L769536-597TMP.html#cb1-9 25. localhost/tmp/lynxXXXXxKSMtn/L769536-597TMP.html#cb1-10 26. localhost/tmp/lynxXXXXxKSMtn/L769536-597TMP.html#cb1-11 27. localhost/tmp/lynxXXXXxKSMtn/L769536-597TMP.html#cb1-12 28. localhost/tmp/lynxXXXXxKSMtn/L769536-597TMP.html#cb1-13 29. localhost/tmp/lynxXXXXxKSMtn/L769536-597TMP.html#cb1-14 30. localhost/tmp/lynxXXXXxKSMtn/L769536-597TMP.html#cb1-15 31. localhost/tmp/lynxXXXXxKSMtn/L769536-597TMP.html#cb1-16 32. localhost/tmp/lynxXXXXxKSMtn/L769536-597TMP.html#cb1-17 33. localhost/tmp/lynxXXXXxKSMtn/L769536-597TMP.html#cb2-1 34. localhost/tmp/lynxXXXXxKSMtn/L769536-597TMP.html#cb2-2 35. localhost/tmp/lynxXXXXxKSMtn/L769536-597TMP.html#cb2-3 36. localhost/tmp/lynxXXXXxKSMtn/L769536-597TMP.html#cb2-4 37. localhost/tmp/lynxXXXXxKSMtn/L769536-597TMP.html#cb2-5 38. localhost/tmp/lynxXXXXxKSMtn/L769536-597TMP.html#cb2-6 39. localhost/tmp/lynxXXXXxKSMtn/L769536-597TMP.html#cb2-7 40. localhost/tmp/lynxXXXXxKSMtn/L769536-597TMP.html#cb2-8 41. localhost/tmp/lynxXXXXxKSMtn/L769536-597TMP.html#cb2-9 42. localhost/tmp/lynxXXXXxKSMtn/L769536-597TMP.html#cb2-10 43. localhost/tmp/lynxXXXXxKSMtn/L769536-597TMP.html#cb2-11 44. localhost/tmp/lynxXXXXxKSMtn/L769536-597TMP.html#cb2-12 45. localhost/tmp/lynxXXXXxKSMtn/L769536-597TMP.html#cb2-13 46. localhost/tmp/lynxXXXXxKSMtn/L769536-597TMP.html#cb2-14 47. localhost/tmp/lynxXXXXxKSMtn/L769536-597TMP.html#cb2-15 48. localhost/tmp/lynxXXXXxKSMtn/L769536-597TMP.html#cb2-16 49. localhost/tmp/lynxXXXXxKSMtn/L769536-597TMP.html#cb2-17 50. localhost/tmp/lynxXXXXxKSMtn/L769536-597TMP.html#cb2-18 51. localhost/tmp/lynxXXXXxKSMtn/L769536-597TMP.html#cb2-19 52. localhost/tmp/lynxXXXXxKSMtn/L769536-597TMP.html#cb2-20 53. localhost/tmp/lynxXXXXxKSMtn/L769536-597TMP.html#cb2-21 54. localhost/tmp/lynxXXXXxKSMtn/L769536-597TMP.html#cb2-22 55. localhost/tmp/lynxXXXXxKSMtn/L769536-597TMP.html#cb2-23 56. localhost/tmp/lynxXXXXxKSMtn/L769536-597TMP.html#cb2-24 57. localhost/tmp/lynxXXXXxKSMtn/L769536-597TMP.html#cb2-25 58. localhost/tmp/lynxXXXXxKSMtn/L769536-597TMP.html#cb2-26 59. localhost/tmp/lynxXXXXxKSMtn/L769536-597TMP.html#cb2-27 60. localhost/tmp/lynxXXXXxKSMtn/L769536-597TMP.html#cb2-28 61. localhost/tmp/lynxXXXXxKSMtn/L769536-597TMP.html#cb2-29 62. localhost/tmp/lynxXXXXxKSMtn/L769536-597TMP.html#cb2-30 63. localhost/tmp/lynxXXXXxKSMtn/L769536-597TMP.html#cb2-31 64. localhost/tmp/lynxXXXXxKSMtn/L769536-597TMP.html#cb2-32 65. localhost/tmp/lynxXXXXxKSMtn/L769536-597TMP.html#cb2-33 66. localhost/tmp/lynxXXXXxKSMtn/L769536-597TMP.html#cb2-34 67. localhost/tmp/lynxXXXXxKSMtn/L769536-597TMP.html#cb2-35 68. localhost/tmp/lynxXXXXxKSMtn/L769536-597TMP.html#cb2-36 69. localhost/tmp/lynxXXXXxKSMtn/L769536-597TMP.html#cb2-37 70. localhost/tmp/lynxXXXXxKSMtn/L769536-597TMP.html#cb2-38 71. localhost/tmp/lynxXXXXxKSMtn/L769536-597TMP.html#cb2-39 72. localhost/tmp/lynxXXXXxKSMtn/L769536-597TMP.html#cb2-40 73. localhost/tmp/lynxXXXXxKSMtn/L769536-597TMP.html#cb2-41 74. localhost/tmp/lynxXXXXxKSMtn/L769536-597TMP.html#cb2-42 75. localhost/tmp/lynxXXXXxKSMtn/L769536-597TMP.html#cb2-43 76. localhost/tmp/lynxXXXXxKSMtn/L769536-597TMP.html#cb2-44 77. localhost/tmp/lynxXXXXxKSMtn/L769536-597TMP.html#cb2-45 78. localhost/tmp/lynxXXXXxKSMtn/L769536-597TMP.html#cb3-1 79. localhost/tmp/lynxXXXXxKSMtn/L769536-597TMP.html#cb3-2 80. localhost/tmp/lynxXXXXxKSMtn/L769536-597TMP.html#cb3-3 81. localhost/tmp/lynxXXXXxKSMtn/L769536-597TMP.html#cb3-4 82. localhost/tmp/lynxXXXXxKSMtn/L769536-597TMP.html#cb3-5 83. localhost/tmp/lynxXXXXxKSMtn/L769536-597TMP.html#cb3-6 84. localhost/tmp/lynxXXXXxKSMtn/L769536-597TMP.html#cb3-7 85. localhost/tmp/lynxXXXXxKSMtn/L769536-597TMP.html#cb3-8 86. localhost/tmp/lynxXXXXxKSMtn/L769536-597TMP.html#cb3-9 87. localhost/tmp/lynxXXXXxKSMtn/L769536-597TMP.html#cb3-10 88. localhost/tmp/lynxXXXXxKSMtn/L769536-597TMP.html#cb3-11 89. localhost/tmp/lynxXXXXxKSMtn/L769536-597TMP.html#cb3-12 90. localhost/tmp/lynxXXXXxKSMtn/L769536-597TMP.html#cb3-13 91. localhost/tmp/lynxXXXXxKSMtn/L769536-597TMP.html#cb3-14 92. localhost/tmp/lynxXXXXxKSMtn/L769536-597TMP.html#cb3-15 93. localhost/tmp/lynxXXXXxKSMtn/L769536-597TMP.html#cb3-16 94. localhost/tmp/lynxXXXXxKSMtn/L769536-597TMP.html#cb3-17 95. localhost/tmp/lynxXXXXxKSMtn/L769536-597TMP.html#cb3-18 96. localhost/tmp/lynxXXXXxKSMtn/L769536-597TMP.html#cb3-19 97. localhost/tmp/lynxXXXXxKSMtn/L769536-597TMP.html#cb3-20 98. localhost/tmp/lynxXXXXxKSMtn/L769536-597TMP.html#cb3-21 99. localhost/tmp/lynxXXXXxKSMtn/L769536-597TMP.html#cb3-22 100. localhost/tmp/lynxXXXXxKSMtn/L769536-597TMP.html#cb3-23 101. localhost/tmp/lynxXXXXxKSMtn/L769536-597TMP.html#cb3-24 102. localhost/tmp/lynxXXXXxKSMtn/L769536-597TMP.html#cb3-25 103. localhost/tmp/lynxXXXXxKSMtn/L769536-597TMP.html#cb4-1 104. localhost/tmp/lynxXXXXxKSMtn/L769536-597TMP.html#cb4-2 105. localhost/tmp/lynxXXXXxKSMtn/L769536-597TMP.html#cb4-3 106. localhost/tmp/lynxXXXXxKSMtn/L769536-597TMP.html#cb4-4