URL generation

327 views
Skip to first unread message

Rue

unread,
Feb 13, 2009, 11:37:36 AM2/13/09
to Liquid Templates
Hi,

After some considerable search I've still been unable to find any way
to generate links and tags from within drops. (I'm using the plugin
version on Rails 2.3)

Is there a way to generate tags and URLs within a drop in the same
manner one would in a view? Surely this is what drops are for or am I
missing something really obvious?

thanks

Rue

unread,
Feb 13, 2009, 11:43:14 AM2/13/09
to Liquid Templates
Of course, I meant generating URLs in filters - drops are just for
data (right?)

jbalvig

unread,
Feb 16, 2009, 12:50:08 AM2/16/09
to Liquid Templates
I have the same problem... :( I tried putting

include ActionView::Helpers::UrlHelper
include ActionController::UrlWriter

at the top of my filter class but am met with an error

Liquid error: undefined method `default_url_options’ for
Liquid::Strainer:Class

when trying to use url_helpers.

fturtle

unread,
Feb 16, 2009, 4:25:16 AM2/16/09
to Liquid Templates
Hello,

Don't know if this helps but I'm doing something like this at the
moment...

# file app/filters/url_filters.rb
module UrlFilters

include ActionView::Helpers::TagHelper
include ActionView::Helpers::AssetTagHelper

def link_to_post(link_text, post)
link_for(link_text, post_path(post))
end

def post_path(post)
url_for :controller => 'posts', :action => 'show', :id => post.id
end

def link_for(link_text, url)
content_tag :a, link_text, { :href => url, :title => link_text }
end

private

def url_for(options = {})
controller.url_for options
end

def controller()
@context.registers[:controller]
end

end

##

Additionally, I'm adding the filters in an initializer and passing the
current controller as a register to the liquid templates. It's a long
winded approach but I'm new to ruby/rails and it works for now :)
Nearly there with an approach that allows you to use the autogenerated
restful path/url helpers though.

fturtle

Tobias Lütke

unread,
Feb 16, 2009, 1:48:11 PM2/16/09
to liquid-t...@googlegroups.com
There is no standard way of doing this. In Shopify we the liquid
accessible area is a pretty distinct piece of software and doesn't
quite follow normal rails conventions.

The way we do it in Shopify is that every "linkable" object (product,
collection, article etc) has a url attribute.

So to display a link to a article you would use

<a href="{{ article.url }}">{{ article.title }}</a>

This is very easy to understand for new comers.

We also provide a filter that provides a shorthand for the above:

{{ article.title | link_to: article.url }}

The filter checks for the existence of a url method on the object
passed in and calls it if it's there. We chose not to use this in our
standard themes because explicitly really helps people starting out.

That being said, we should probably provide a standard way of
accessing url_for and friends from liquid when liquid is invoked
through the rails plugin view renderer. I'd definitely accept a patch
for that.


Regards
-- tobi

jbalvig

unread,
Feb 16, 2009, 11:19:24 PM2/16/09
to Liquid Templates
fturtle,

Thanks a lot! Your method allowed me to at least work around this
limitation for the time being. Tobias, thanks a lot for replying.
Would love to contribute a patch but will probably need a better
understanding of the internals of Liquid before endeavoring on such a
task ;)

- Jens

Rue

unread,
Feb 17, 2009, 5:46:33 AM2/17/09
to Liquid Templates
Hi Tobi,

Many thanks for that - that was a real barrier to entry.

I was going to look at offering a patch once I found out how to get it
to see the Helper modules but it looks like fturtle if much further
down the line than I am so will try not to double the effort. However
I will be happy to try and work to make that available.

It sound like fturtle is hand rendering so I will try to integrate
that with the plugin if fturtle wants to share results.

Tag builders and content helpers would be a huge help to liquid
template building.


On Feb 16, 6:48 pm, Tobias Lütke <tobias.lue...@gmail.com> wrote:
> There is no standard way of doing this. In Shopify we the liquid
> accessible area is a pretty distinct piece of software and doesn't
> quite follow normal rails conventions.
>
> The way we do it in Shopify is that every "linkable" object (product,
> collection, article etc) has a url attribute.
>
> So to display a link to a article you would use
>
> <a href="{{ article.url }}">{{ article.title }}</a>
>
> This is very easy to understand for new comers.
>
> We also provide a filter that provides a shorthand for the above:
>
> {{ article.title | link_to: article.url }}
>
> The filter checks for the existence of a url method on the object
> passed in and calls it if it's there. We chose not to use this in our
> standard themes because explicitly really helps people starting out.
>
> That being said, we should probably provide a standard way of
> accessing url_for and friends from liquid when liquid is invoked
> through the rails plugin view renderer. I'd definitely accept a patch
> for that.
>
> Regards
> -- tobi
>

Tobias Lütke

unread,
Feb 17, 2009, 7:39:27 PM2/17/09
to liquid-t...@googlegroups.com
> Tag builders and content helpers would be a huge help to liquid
> template building.

This is sort of off topic but I absolutely agree. I'm unhappy with the
way new tags need to be build for liquid. It just seems like something
the implementor of the library doesn't really want you to do ( which
is wrong, you guys need to implement more tags ).

A nice DSL would definitely help. Radius of the Radiant project did a
much better job then liquid on this front.


Regards
-- tobi
Reply all
Reply to author
Forward
0 new messages