Skip to content

Instantly share code, notes, and snippets.

@seivan
Created March 25, 2009 19:47
Show Gist options
  • Save seivan/85669 to your computer and use it in GitHub Desktop.
Save seivan/85669 to your computer and use it in GitHub Desktop.
class Post < ActiveRecord::Base
require 'RedCloth'
has_many :photos
liquid_methods :post, :photo, :photos, :posts #Testing everything
#Works, but I want to be able to call it dynamicly in a textarea.
#SO I can use textile !{% picture 2 %}.
def pic(pos)
photos[pos.to_i].image.url(:medium)
end
#I want to pass in { photo 1 } to the render. Where 1 is an integer
#Look at def render. @photo is that integer. However I get an error
#Liquid error: undefined local variable or method `photos’ for #
class PhotoPicker < Liquid::Tag
def initialize(tag_name, photo_pos, tokens)
super
@photo = photo_pos.to_i
end
def render(context)
photos[@photo].image.url(:medium)
#rand(@photo)
end
end
#Works Just type {% rando 6 %} and it pulls a rand(6)
#Calling in View <%= textilize(Liquid::Template.parse(post.content).render) %>
class Rando < Liquid::Tag
def initialize(tag_name, photo_pos, tokens)
super
@photo = photo_pos.to_i
end
def render(context)
#photos[@photo].image.url(:medium)
rand(@photo)
end
end
Liquid::Template.register_tag('rando', Rando)
Liquid::Template.register_tag('picture', PhotoPicker)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment