Validating a liquid template

1,823 views
Skip to first unread message

Randy Schmidt

unread,
Nov 27, 2009, 3:44:23 PM11/27/09
to Liquid Templates
Hi, I'm allowing users to customize sms messages using liquid + a
defined set of variables. I'm adding validations to make sure there
aren't any typos. The first kind, that there aren't any parsing
errors, is simple because Liquid will raise an exception if they don't
close a tag correctly. However, I also want to validate that they used
a valid variable. It looks like liquid will happily replace undefined
variables with an empty string. I'm digging through the source code to
find a way but haven't yet.

Any suggestions?

Thank you!
--
Forge38 LLC
Randy Schmidt
ra...@forge38.com
http://forge38.com
267-334-6833

Jon Crawford

unread,
Dec 1, 2009, 9:24:31 AM12/1/09
to Liquid Templates
Hey Randy-

I've used this pretty successfully: http://github.com/bhauman/validates_proper_liquid_syntax

The syntax looks like this:

validates_proper_liquid_syntax :layout

I don't think it validates for variable names, but it definitely
validates for valid tag names. You might be able to hack it up a bit
to pass it a list of acceptable variable names. Something like...

validates_proper_liquid_syntax :layout, :variables =>
['foo','bar','bacon']

And for bonus points:

validates_proper_liquid_syntax :layout, :required_variables =>
['foo'], :optional_variables => ['bar','bacon']

Hope that helps!

!jon

Randy Schmidt

unread,
Dec 1, 2009, 11:21:06 AM12/1/09
to liquid-t...@googlegroups.com
On Tue, Dec 1, 2009 at 9:24 AM, Jon Crawford <ma...@joncrawford.com> wrote:
> Hey Randy-
>
> I've used this pretty successfully: http://github.com/bhauman/validates_proper_liquid_syntax

Thanks Jon! That is similar to what I am doing now but wrapped in a
nice method :)

I'm looking for a way to make sure the person making the template
doesn't have any typos when they save it in the database. The way I
have it now it just inserts an empty string which means it won't fail
but will also read kind of funny.

I have thought of a couple things to try and will give them a shot. If
I come up with anything "new" I'll report back.

Thanks!

pascal

unread,
Jan 27, 2010, 12:27:28 PM1/27/10
to Liquid Templates
i'm new to liquid but this is what came to my mind:

In addition you could parse the template and see if it raises any
exceptions:

Liquid::Template.parse("{% for p in prods %} {{p.description}}")
Liquid::SyntaxError: for tag was never closed
from /opt/local/lib/ruby/gems/1.8/gems/liquid-2.0.0/lib/liquid/
block.rb:84:in `assert_missing_delimitation!'
Hard to generate a user friendly error message from this though...


Pascal


On Dec 1 2009, 5:21 pm, Randy Schmidt <m...@r38y.com> wrote:

hiddenloop

unread,
Jan 28, 2010, 5:25:28 AM1/28/10
to Liquid Templates
This is pretty much what the plugin does - i've been using it
successfully in a production site for sometime with no issues;

http://github.com/bhauman/validates_proper_liquid_syntax/blob/master/lib/validates_proper_liquid_syntax.rb

m

Reply all
Reply to author
Forward
0 new messages