Comments (4)
You should be able to use #[]
. You need to consider very carefully if you want to do this, as you are doing lookups inline in the template based on user input, which can have a huge security and performance impact.
class CollectionsDrop < Liquid::Drop
def [](name)
Collection.find_by_name(name)
end
end
class Collection
Collections = {
'foo' => 'bar'
}
def self.find_by_name(name)
Collections[name]
end
end
irb(main):001:0> Liquid::Template.parse("{{ collection.foo }}").render('collection' => CollectionsDrop.new)
=> "bar"
from liquid.
I understand what you're saying about security, though I wonder, why the before_method isn't working? You can see what I'm trying to accomplish, by looking at this code I'm using from locomotive cms as a guide.
Now, I'm not using a global context, such as :site, but I feel that that shouldn't really stop me from achieving similar functionality, without sacrificing security.
module Locomotive
module Liquid
module Drops
class AssetCollections < ::Liquid::Drop
def before_method(meth)
collection = @context.registers[:site].asset_collections.where(:slug => meth.to_s).first
AssetCollectionProxy.new(collection)
end
end
class AssetCollectionProxy < ::Liquid::Drop
def initialize(collection)
@collection = collection
end
def first
@collection.ordered_assets.first
end
def last
@collection.ordered_assets.last
end
def each(&block)
@collection.ordered_assets.each(&block)
end
def paginate(options = {})
paginated_collection = @collection.ordered_assets.paginate(options)
{
:collection => paginated_collection,
:current_page => paginated_collection.current_page,
:previous_page => paginated_collection.previous_page,
:next_page => paginated_collection.next_page,
:total_entries => paginated_collection.total_entries,
:total_pages => paginated_collection.total_pages,
:per_page => paginated_collection.per_page
}
end
def size
@collection.assets.size
end
def before_method(meth)
return '' if @collection.nil?
@collection.send(meth)
end
end
end
end
end
from liquid.
#before_method
is working fine as well:
class CollectionsDrop < Liquid::Drop
def before_method(name)
Collection.find_by_name(name)
end
end
class Collection
Collections = {
'foo' => 'bar'
}
def self.find_by_name(name)
Collections[name]
end
end
irb(main):001:0> Liquid::Template.parse("{{ collection.foo }}").render('collection' => CollectionsDrop.new)
=> "bar"
from liquid.
facepalm alright, not sure what I was doing wrong... Thanks for the help.
from liquid.
Related Issues (20)
- Possible to get full object & variable name with `strict_variables: true`?
- video_tag fully disable poster HOT 2
- Expose Liquid::Condition in public API
- alt attribute not set for media.preview_image with image_tag filter HOT 1
- Running rake example throws error
- Liquid rendering is unexpectedly changing the "context" attribute on non-Drop classes
- Liquid shopify skórki
- Add WISMOlabs to "Who uses Liquid?" wiki section
- App Block Missing Data On Theme Previews When Proceeded by a Hidden Section Containing an @app Block
- Sort orders by 'created_at' in paginate?
- Regarding deprecation of the total_discount
- Add limit to split filter, like split method in Ruby
- Liquid - Product Description Null Check Not Working
- Should strip_newlines also 'strip' LSEP ?
- Case statement evaluation renders multiple when conditions if a value is repeated
- String to Object? HOT 1
- ع
- Can't get key/value from hash HOT 6
- Allow overriding blank? method in drops HOT 1
- image_tag filter, incorrect width and height attribute values for SVG
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from liquid.