{"action":"created","issue":{"url":"https://api.github.com/repos/Shopify/liquid/issues/558","labels_url":"https://api.github.com/repos/Shopify/liquid/issues/558/labels{/name}","comments_url":"https://api.github.com/repos/Shopify/liquid/issues/558/comments","events_url":"https://api.github.com/repos/Shopify/liquid/issues/558/events","html_url":"https://github.com/Shopify/liquid/pull/558","id":75334100,"number":558,"title":"Allow for a block to be passed on liquid_methods that can transform…","user":{"login":"rovermicrover","id":1587624,"avatar_url":"https://avatars.githubusercontent.com/u/1587624?v=3","gravatar_id":"","url":"https://api.github.com/users/rovermicrover","html_url":"https://github.com/rovermicrover","followers_url":"https://api.github.com/users/rovermicrover/followers","following_url":"https://api.github.com/users/rovermicrover/following{/other_user}","gists_url":"https://api.github.com/users/rovermicrover/gists{/gist_id}","starred_url":"https://api.github.com/users/rovermicrover/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/rovermicrover/subscriptions","organizations_url":"https://api.github.com/users/rovermicrover/orgs","repos_url":"https://api.github.com/users/rovermicrover/repos","events_url":"https://api.github.com/users/rovermicrover/events{/privacy}","received_events_url":"https://api.github.com/users/rovermicrover/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":3,"created_at":"2015-05-11T19:37:47Z","updated_at":"2015-05-12T18:50:01Z","closed_at":null,"pull_request":{"url":"https://api.github.com/repos/Shopify/liquid/pulls/558","html_url":"https://github.com/Shopify/liquid/pull/558","diff_url":"https://github.com/Shopify/liquid/pull/558.diff","patch_url":"https://github.com/Shopify/liquid/pull/558.patch"},"body":"…the allowed methods return value. Useful for sanitization and dynamic content.\r\n\r\nAdded comment explains it best.\r\n\r\nYou may also pass a block that can be used to transform the method's returned value.\r\n\r\n```ruby\r\nclass SomeClass\r\n  liquid_methods :an_allowed_method do |object, method, value|\r\n    if value.is_a?(String)\r\n      value += ' that has been transformed'\r\n    else\r\n      value\r\n    end\r\n  end\r\n\r\n  def an_allowed_method\r\n    'this comes from an allowed method'\r\n  end\r\n\r\nend\r\n```\r\nusage:\r\n```ruby\r\n@something = SomeClass.new\r\n```\r\ntemplate:\r\n```ruby\r\n{{something.an_allowed_method}}\r\n```\r\noutput:\r\n```ruby\r\n'this comes from an allowed method that has been transformed'\r\n```\r\n@boourns, @fw42, @camilo, @dylanahsmith, or @arthurnn"},"comment":{"url":"https://api.github.com/repos/Shopify/liquid/issues/comments/101384699","html_url":"https://github.com/Shopify/liquid/pull/558#issuecomment-101384699","issue_url":"https://api.github.com/repos/Shopify/liquid/issues/558","id":101384699,"user":{"login":"nickpearson","id":1329273,"avatar_url":"https://avatars.githubusercontent.com/u/1329273?v=3","gravatar_id":"","url":"https://api.github.com/users/nickpearson","html_url":"https://github.com/nickpearson","followers_url":"https://api.github.com/users/nickpearson/followers","following_url":"https://api.github.com/users/nickpearson/following{/other_user}","gists_url":"https://api.github.com/users/nickpearson/gists{/gist_id}","starred_url":"https://api.github.com/users/nickpearson/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/nickpearson/subscriptions","organizations_url":"https://api.github.com/users/nickpearson/orgs","repos_url":"https://api.github.com/users/nickpearson/repos","events_url":"https://api.github.com/users/nickpearson/events{/privacy}","received_events_url":"https://api.github.com/users/nickpearson/received_events","type":"User","site_admin":false},"created_at":"2015-05-12T18:50:01Z","updated_at":"2015-05-12T18:50:01Z","body":"What about using procs in the `to_liquid` hash?\r\n\r\n```ruby\r\ndef to_liquid\r\n  %w( name title ).inject({}) do |hash, col|\r\n    hash[col] = proc{ |context| Sanitizer.sanitize(col) }\r\n    hash\r\n  end\r\nend\r\n```\r\n\r\nEach proc will only be called if it is referenced in the Liquid template. Here's a quick example using `upcase` instead of sanitization:\r\n\r\n```ruby\r\nclass Person\r\n  def name;  puts 'called name';  'Jon'; end\r\n  def title; puts 'called title'; 'Dev'; end\r\n  def to_liquid\r\n    %w( name title ).inject({}) do |hash, col|\r\n      hash[col] = proc{ |context| send(col).upcase }\r\n      hash\r\n    end\r\n  end\r\nend\r\n\r\nLiquid::Template.parse('Hi, {{ person.name }}').render('person' => Person.new)\r\n# => called name\r\n# => \"Hi, John\"\r\n```\r\n\r\nHope this helps."}}