sample.liquid.txt 512 B

12345678910111213141516
  1. class Random < Liquid::Block
  2. def initialize(tag_name, markup, tokens)
  3. super
  4. @rand = markup.to_i
  5. end
  6. def render(context)
  7. value = rand(@rand)
  8. super.sub('^^^', value.to_s) # calling `super` returns the content of the block
  9. end
  10. end
  11. Liquid::Template.register_tag('random', Random)
  12. text = " {% random 5 %} you have drawn number ^^^, lucky you! {% endrandom %} "
  13. @template = Liquid::Template.parse(text)
  14. @template.render # will return "you have drawn number 1, lucky you!" in 20% of cases