Inserting code into .liquid
Forum » Help / Support » Inserting code into .liquid
Started by: Anonymous (90.209.119.x)
On: 1201277043|%e %b %Y, %H:%M %Z|agohover
Number of posts: 17
rss icon RSS: New posts
Summary:
A quick question about the functionalities of liquid.
Inserting code into .liquid
Anonymous (90.209.119.x) 1201277043|%e %b %Y, %H:%M %Z|agohover

Hi JW,
I am hoping to install an image processing script so that when people upload photos to go with articles they can be automatically resized. I am planning ot use ImageMagick (and RMagick2) but it requires some code to be placed in the view.

Code such as this:
[code]
<%= imagemagick_tag 'first_photo.jpg', 'resize(100x100)' %>
[/code]

How can code be placed in the view since you are using .liquid?

James

unfold Inserting code into .liquid by Anonymous (90.209.119.x), 1201277043|%e %b %Y, %H:%M %Z|agohover
Re: Inserting code into .liquid
JW_00000JW_00000 1201550344|%e %b %Y, %H:%M %Z|agohover

James,

I take it you are using the "ImageMagick for Rails" plugin from here? If so, first of all, you will have to add "include ActionView::Helpers::ImageMagickMacroHelper" to app/helpers/application_helper.rb, so it becomes:

module ApplicationHelper
  include ActionView::Helpers::ImageMagickMacroHelper
end

In the templates, you call the plugin functions like this:

{{ 'first_photo.jpg' | imagemagick_tag }}

However, this returns an error in the plugin. So, you'll have to edit vendor/plugins/imagemagick_tag/lib/image_magick_macro_helper.rb, around line 124, to read:

def imagemagick_controller(options)
  options[:controller] ? (options[:controller].camelize + "Controller").constantize.new : @context.registers[:controller]
end

This should make the plugin work correctly with Liquid and Chameleon. To use the tag you gave in your post, you'd write:

{{ 'first_photo.jpg' | imagemagick_tag: 'resize(100x100)' }}

If this wouldn't work, let me know.

JW

unfold Re: Inserting code into .liquid by JW_00000JW_00000, 1201550344|%e %b %Y, %H:%M %Z|agohover
Re: Inserting code into .liquid
Anonymous (90.211.16.x) 1203369103|%e %b %Y, %H:%M %Z|agohover

Worked Perfectly!
Thanks you're a genius!
However as Chameleon is most likely gonna be used by folk who understand rails i've got to question why use .liquid if rhtml tags can't be added easily?

Cheers once again,
James

unfold Re: Inserting code into .liquid by Anonymous (90.211.16.x), 1203369103|%e %b %Y, %H:%M %Z|agohover
Re: Inserting code into .liquid
Anonymous (90.211.16.x) 1203371739|%e %b %Y, %H:%M %Z|agohover

Hi JW, i'm sorry i spoke too soon,
I am getting an error labeled 'Liquid error: undefined method ‘image_tag’ for #' any ideas?

I also added this to the blog controller 'imagemagick_for '/public/blog/images' '.

Thanks,
James

unfold Re: Inserting code into .liquid by Anonymous (90.211.16.x), 1203371739|%e %b %Y, %H:%M %Z|agohover
Re: Inserting code into .liquid
JW_00000JW_00000 1203866638|%e %b %Y, %H:%M %Z|agohover

Hi James,

It seems the ImageMagick plugin depends on a helper that's by default included in Rails. I made some changes to the code to make it easier to use Rails helpers in Chameleon in the future, could you try to download this file, and replace lib/chameleon/liquid/rails_integration.rb with it. Then, in app/helpers/blog_helper.rb, write this:

module BlogHelper
  include ActionView::Helpers::AssetTagHelper
end

Normally, everything should work fine then.

To answer your first question, why not use RHTML instead of Liquid, the answer is because of security. My goal is to make it easy for people to create custom themes for Chameleon in the future, and Liquid ensures that when you download and install a theme made by someone else, it can't harm your data. So, themes shouldn't be able to (accidentally or on purpose) delete your entries. You can however still use RHTML, by simply changing the extension of a theme file from ".liquid" to ".rhtml". This makes Rails switch from using Liquid to using the default Rails template parser. However, then you lose the security Liquid provides, and some of the shortcuts built in into Liquid (for example the "comment_form" tag or the "url_for" filters).

Greetings,
JW

unfold Re: Inserting code into .liquid by JW_00000JW_00000, 1203866638|%e %b %Y, %H:%M %Z|agohover
Re: Inserting code into .liquid
Anonymous (90.198.65.x) 1203964234|%e %b %Y, %H:%M %Z|agohover

Hi JW,
Sorry still have an error. I followed all your changes and now get the error 'Liquid error: undefined method ‘url_for_imagemagick’ for #'.

Any suggestions?

James

unfold Re: Inserting code into .liquid by Anonymous (90.198.65.x), 1203964234|%e %b %Y, %H:%M %Z|agohover
Re: Inserting code into .liquid
JW_00000JW_00000 1204045416|%e %b %Y, %H:%M %Z|agohover

James,

When using that ImageMagick library, you have to add this to app/controllers/blog_controller.rb:

class BlogController < ApplicationController
  imagemagick_for '/var/lib/photos'

where /var/lib/photos is the directory where the images you'll be using are stored. Did you do this?

JW

unfold Re: Inserting code into .liquid by JW_00000JW_00000, 1204045416|%e %b %Y, %H:%M %Z|agohover
Re: Inserting code into .liquid
Anonymous (90.198.65.x) 1204127413|%e %b %Y, %H:%M %Z|agohover

Hi JW,
I placed the code into the clog controller and got the following error:
Liquid error: undefined method ‘tag’ for #

Thanks for the help so far,
James

unfold Re: Inserting code into .liquid by Anonymous (90.198.65.x), 1204127413|%e %b %Y, %H:%M %Z|agohover
Re: Inserting code into .liquid
JW_00000JW_00000 1204133454|%e %b %Y, %H:%M %Z|agohover

Hi James,

It seems you have to add another line to app/helpers/blog_helper.rb, it becomes:

module BlogHelper
  include ActionView::Helpers::TagHelper
  include ActionView::Helpers::AssetTagHelper
end

I hope this works.
JW

unfold Re: Inserting code into .liquid by JW_00000JW_00000, 1204133454|%e %b %Y, %H:%M %Z|agohover
Re: Inserting code into .liquid
Anonymous (90.198.65.x) 1204384886|%e %b %Y, %H:%M %Z|agohover

Hi JW,
Still getting the same old 'Liquid error: undefined method ‘tag’ for #'… i inserted the above and reset the server and nothing…

Any other ideas?
James

unfold Re: Inserting code into .liquid by Anonymous (90.198.65.x), 1204384886|%e %b %Y, %H:%M %Z|agohover
Re: Inserting code into .liquid
JW_00000JW_00000 1204400324|%e %b %Y, %H:%M %Z|agohover

Hi James,

Could you use the "View Source" option in your browser, to find what's behind the #. Normally there should be something between a < and a > there. Maybe that could help us get a clue.

JW

unfold Re: Inserting code into .liquid by JW_00000JW_00000, 1204400324|%e %b %Y, %H:%M %Z|agohover
Re: Inserting code into .liquid
Anonymous (90.210.130.x) 1204484024|%e %b %Y, %H:%M %Z|agohover

Hi JW,
In the source is says:
Liquid error: undefined method ‘tag’ for #<Liquid::Strainer:0x2fecf20>
Any idea about what this means?

James

unfold Re: Inserting code into .liquid by Anonymous (90.210.130.x), 1204484024|%e %b %Y, %H:%M %Z|agohover
Re: Inserting code into .liquid
JW_00000JW_00000 1204568250|%e %b %Y, %H:%M %Z|agohover

James,

Could you add the following Liquid code to your template, and then copy what it returns from the HTML source code?

{{ 'br' | tag }}
{{ 'test.png' | image_tag }}
{{ 'test.png' | imagemagick_tag }}

Normally, it should return something like this:

<br />
<img alt="Test" src="/images/test.png?" />
<img alt="Imagemagick?id=test" src="http://0.0.0.0:3000/blog/imagemagick?id=test.png" />

JW

unfold Re: Inserting code into .liquid by JW_00000JW_00000, 1204568250|%e %b %Y, %H:%M %Z|agohover
Re: Inserting code into .liquid
Manuel (guest) 1242946157|%e %b %Y, %H:%M %Z|agohover

i have a similar problem,

i have many users in a website and i need restrict my image_tag tag to only find the asset for the present user.

i have this code:

module TextFilters
include ActionView::Helpers::TagHelper

def image_path(name, size = 'original')
@user.assets.find_by_name(name).asset.url(size)
end

end

logically this returns an error, since to @user it does not exist, i need to execute a method in the User model, for example: User.get_current_user

class User < ActiveRecord::Base

def get_current_user
User.find_by_subdomain(current_subdomain)
end

end

Liquid error: undefined method ‘get_current_user’ for #
<class:0x2f8c648/>

they have an idea?

i try with session[:user] but don't works too

unfold Re: Inserting code into .liquid by Manuel (guest), 1242946157|%e %b %Y, %H:%M %Z|agohover
Re: Inserting code into .liquid
Manuel (guest) 1242946264|%e %b %Y, %H:%M %Z|agohover

sorry, i say image_tag tag, but i returns the url with the image_path tag

unfold Re: Inserting code into .liquid by Manuel (guest), 1242946264|%e %b %Y, %H:%M %Z|agohover
Re: Inserting code into .liquid
JW_00000JW_00000 1243112661|%e %b %Y, %H:%M %Z|agohover

Manuel,

As you may have already seen on the homepage, Chameleon is not under active development anymore. I stopped working on it more than a year ago. Meanwhile, I forgot a lot about how it works, so I might not be able to help you completely.

As for your problem, in the .liquid templates you're not using the User model (which is in app/models/user.rb), but the UserDrop (which is in lib/chameleon/liquid/user_drop.rb). So, you need to add you 'get_current_user' method to the UserDrop.

(If that doesn't work, you might need to add the 'get_current_user' method to the User model, and afterwards add a new 'get_current_user' to UserDrop which simply calls the other one.)

I hope this helps,
JW

unfold Re: Inserting code into .liquid by JW_00000JW_00000, 1243112661|%e %b %Y, %H:%M %Z|agohover
Re: Inserting code into .liquid
skirti (guest) 1245393581|%e %b %Y, %H:%M %Z|agohover

Hey,
I am not able to use rails helpers in liquid code (like form_for, check_box_tag)..
It gives an error saying Unknown tag 'form_for'

Has anybody used it before?

unfold Re: Inserting code into .liquid by skirti (guest), 1245393581|%e %b %Y, %H:%M %Z|agohover
New post
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution 2.5 License.