Is there a simple way to format your data in the .rhtml file? I come from a PHP/Smarty background, and in the templates, I''ve used what is called "modifiers" to format data. {$price|currency_format} # displays in currency format {$description|truncate:"100"} # Truncates the description after 100 characters It seems like the template (rhtml) would be the ideal place to play with the display format. I saw a display_currency function in one of the other posts, and I tried including it in my application controller. It runs fine if you call it from within the controller, but it won''t let me call it from the rhtml file. in application controller: (or any controller) def display_currency(number) unless number.nil? "$" + sprintf("%01.2f",number) end end in .rhtml file: <% display_currency(@price) %> Error: undefined method `display_currency'' for #<#<Class:0x39381e8>:0x3938110> Do I have to write my own functions for things like this or are there some built in? Either way, how do I use them in the .rhtml files? Thank you, Brandon Pearce -- Posted via http://www.ruby-forum.com/.
<%= number_to_currency(@price) %> http://api.rubyonrails.org/classes/ActionView/Helpers/NumberHelper.html <%= truncate(@description, 100) %> http://api.rubyonrails.org/classes/ActionView/Helpers/TextHelper.html View the Helpers of ActiveView to find more. Bob Silva http://www.railtie.net/> -----Original Message----- > From: rails-bounces@lists.rubyonrails.org [mailto:rails- > bounces@lists.rubyonrails.org] On Behalf Of Brandon Pearce > Sent: Friday, January 27, 2006 1:28 PM > To: rails@lists.rubyonrails.org > Subject: [Rails] Formatting Data in rhtml > > Is there a simple way to format your data in the .rhtml file? > I come from a PHP/Smarty background, and in the templates, I''ve used > what is called "modifiers" to format data. > > {$price|currency_format} # displays in currency format > {$description|truncate:"100"} # Truncates the description after 100 > characters > > It seems like the template (rhtml) would be the ideal place to play with > the display format. I saw a display_currency function in one of the > other posts, and I tried including it in my application controller. It > runs fine if you call it from within the controller, but it won''t let me > call it from the rhtml file. > > in application controller: (or any controller) > > def display_currency(number) > unless number.nil? > "$" + sprintf("%01.2f",number) > end > end > > in .rhtml file: > > <% display_currency(@price) %> > > Error: > > undefined method `display_currency'' for #<#<Class:0x39381e8>:0x3938110> > > Do I have to write my own functions for things like this or are there > some built in? Either way, how do I use them in the .rhtml files? > > Thank you, > > Brandon Pearce > > > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails
Hi, For currency you can use the number_to_currency() helper. If you want to write your own helpers you can put them in your application helper, if you want to use them everywhere, or any controller helper. All the functions that you write in these will be accessable from the corresponding view. Eric Brandon Pearce wrote:> Is there a simple way to format your data in the .rhtml file? > I come from a PHP/Smarty background, and in the templates, I''ve used > what is called "modifiers" to format data. > > {$price|currency_format} # displays in currency format > {$description|truncate:"100"} # Truncates the description after 100 > characters > > It seems like the template (rhtml) would be the ideal place to play with > the display format. I saw a display_currency function in one of the > other posts, and I tried including it in my application controller. It > runs fine if you call it from within the controller, but it won''t let me > call it from the rhtml file. > > in application controller: (or any controller) > > def display_currency(number) > unless number.nil? > "$" + sprintf("%01.2f",number) > end > end > > in .rhtml file: > > <% display_currency(@price) %> > > Error: > > undefined method `display_currency'' for #<#<Class:0x39381e8>:0x3938110> > > Do I have to write my own functions for things like this or are there > some built in? Either way, how do I use them in the .rhtml files? > > Thank you, > > Brandon Pearce > > >-- Eric Goodwin http://www.ericgoodwin.com