Giter VIP home page Giter VIP logo

Comments (4)

titanous avatar titanous commented on August 20, 2024

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.

codezomb avatar codezomb commented on August 20, 2024

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.

titanous avatar titanous commented on August 20, 2024

#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.

codezomb avatar codezomb commented on August 20, 2024

facepalm alright, not sure what I was doing wrong... Thanks for the help.

from liquid.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.