Proposal: String#interpolate for safe templating

  • Thread starter Samuel Williams
  • Start date
S

Samuel Williams

Hi,

I'm curious if anyone has considered a method called interpolate:

class String
def interpolate(binding, filename, lineno)
... magic ...
end
end

It would have a similar effect as %Q, but the input would already exist =
as a string rather than being parsed. This would avoid lots of escaping =
issues. i.e.

eval("%Q{" + buffer + "}") is fine as long as buffer doesn't =
contain { or } in the wrong place....

Does this seem useful? I'm personally working on a template system and =
would find such a function very helpful. Not sure where to start if I =
was going to implement it - I downloaded the 1.9.2 source code but =
couldn't really find out where I'd add the code.

Kind regards,
Samuel=
 
R

Robert Klemme

Hi,

I'm curious if anyone has considered a method called interpolate:

class String
=A0 =A0 =A0 =A0def interpolate(binding, filename, lineno)
=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0... magic ...
=A0 =A0 =A0 =A0end
end

It would have a similar effect as %Q, but the input would already exist a=
s a string rather than being parsed. This would avoid lots of escaping issu=
es. i.e.
=A0 =A0 =A0 =A0eval("%Q{" + buffer + "}") is fine as long as buffer doesn=
't contain { or } in the wrong place....

Well, you can do

'\\\\\\&') said:
Does this seem useful? I'm personally working on a template system and wo=
uld find such a function very helpful. Not sure where to start if I was goi=
ng to implement it - I downloaded the 1.9.2 source code but couldn't really=
find out where I'd add the code.

I am not sure I understand how that would resolve your escaping
problem: the method in question cannot know whether you want specific
characters (namely #{ and }) as they are or as meta characters. Am I
missing something?

Kind regards

robert

--=20
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/
 
B

Brian Candler

Samuel Williams wrote in post #980514:
Does this seem useful? I'm personally working on a template system and
would find such a function very helpful.

Of course if this is a programming exercise it's worthwhile for the
learning experience, but I hope you realise there are many templating
systems already out there :)

First hit for "ruby templating" in google:
http://www.hokstad.com/mini-reviews-of-19-ruby-template-engines.html

I thought I had come across one which used #{...} style interpolation,
but I can't remember where now. I might have been imagining it.
 
B

Brian Candler

Brian Candler wrote in post #980620:

... and that's not even complete. It doesn't mention "mustache" for
instance:
https://github.com/defunkt/mustache#readme
I thought I had come across one which used #{...} style interpolation,
but I can't remember where now. I might have been imagining it.

It might have been Ezamar.
https://github.com/manveru/ezamar#readme
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,818
Latest member
Brigette36

Latest Threads

Top