How can I retrieve a list of symbols from any template string? See the example below. I''d like to be able to retrieve [''title'', ''author''] from that template. ''The TV show #{title} was created by #{author}.'' --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Spinoffs" group. To post to this group, send email to rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---
I''m somewhat surprised that the template class doesnt have that function, but Prototype already has a function called String.extractScripts(). If you know regular expressions then it should be easy to write a String.extractTemplateSymbols() function by replacing ScriptFragment with your own SymbolFragment. I''m a little rusty on my regular expressions, but here''s my crack at it: SymbolFragment = "(?:#{)(\w+)(?:})" Hope that helps, RR On Jun 13, 3:41 pm, Les <les.szkla...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> How can I retrieve a list of symbols from any template string? > > See the example below. I''d like to be able to retrieve [''title'', > ''author''] from that template. > > ''The TV show #{title} was created by #{author}.''--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Spinoffs" group. To post to this group, send email to rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---
Hey Les, Les a écrit :> How can I retrieve a list of symbols from any template string?If you wish to work with all Template''s, you''ll need to use their specific syntax regex ("pattern"). The default syntax says #{...}, but it can be pretty much anything, really, as long as you provide an alternative pattern at creation time. So I''d go with something like this: Template.prototype.getSymbols = function() { var result = []; this.template.scan(this.pattern, function(md) { if (md[1] != ''\\'') result.push(md[3]); }); return result; }; Then you could do: var t = new Template( "Hello #{name}, you are #{age}! Syntax is \\#{...}"); t.evaluate({name: ''tdd'', age: 29}) // => "Hello tdd, you are 29! Syntax is #{...}" t.getSymbols() // => ["name", "age"] And this will work with custom patterns, too! -- Christophe Porteneuve aka TDD tdd-x+CfDp/qHev2eFz/2MeuCQ@public.gmane.org --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Spinoffs" group. To post to this group, send email to rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---
Hey there, RighteousRaven a écrit :> I''m somewhat surprised that the template class doesnt have that > function, but Prototype already has a function calledActually it''s the first time we see someone need it. I certainly don''t see frequent use-cases for it.> SymbolFragment = "(?:#{)(\w+)(?:})"Although a valliant try :-), this won''t work all that well on itself, for two reasons: - ''{'' is a special char, you''d need to escape it - This doesn''t account for \-based escaping of the # - This relies on the default pattern only See my previous reply for a working solution. -- Christophe Porteneuve aka TDD tdd-x+CfDp/qHev2eFz/2MeuCQ@public.gmane.org --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Spinoffs" group. To post to this group, send email to rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---