# Getting PHP/Smarty template functionality from Ruby or Ruby

**URL:** <https://www.ruby-forum.com/t/getting-php-smarty-template-functionality-from-ruby-or-ruby/72184>\
**Category:** Ruby\
**Created:** [October 15, 2006, 5:45am UTC](https://www.ruby-forum.com/t/getting-php-smarty-template-functionality-from-ruby-or-ruby/72184 "2006-10-15T05:45:33Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Irving\_Fried](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/d07c76/32.png) [@Irving\_Fried](https://www.ruby-forum.com/u/Irving_Fried)\
**Post date:** [October 15, 2006, 5:45am UTC](https://www.ruby-forum.com/t/getting-php-smarty-template-functionality-from-ruby-or-ruby/72184/1 "2006-10-15T05:45:33Z")

</div>

I’ve read about Canny, but it doesn’t seem to enjoy a lot of versions or  
users since it’s been released. I’ve read that Ruby natively supplies  
template functionality. I need to know how easy it will be with Ruby to  
re-skin a webapp. Also, whether Ruby or Ruby on Rails would be  
preferable in this situation?

Note, I’m not looking for simple JSP functionality, but the ability to  
go beyond that by having Ruby select a specific template at runtime, the  
way I can do with Smarty. I want to abstract the template away so that  
the end user will get the same URL regardless of the skin that’s used to  
display it.

I’d appreciate whatever advice you can give.

---

<div class="post-metadata">

**Author:** ![Irving\_Fried](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/d07c76/32.png) [@Irving\_Fried](https://www.ruby-forum.com/u/Irving_Fried)\
**Post date:** [October 15, 2006, 7:31am UTC](https://www.ruby-forum.com/t/getting-php-smarty-template-functionality-from-ruby-or-ruby/72184/2 "2006-10-15T07:31:40Z")

</div>

Hi Irving,

It’s fairly simple to get the same functionality as Smarty.

In your application controller, just add:

before\_filter :set\_theme

private

def set\_theme  
ActionController::Base.template\_root = File.join(RAILS\_ROOT, ‘app/  
views’, theme) if theme  
end

def theme  
session[:theme] = params[:theme] if params[:theme]  
session[:theme]  
end

That would allow you to place a folder in your app/views directory  
with the name of your theme, and Rails will automatically load the  
views from that folder instead.

If you’d like to place them in a separate folder, just change this:

def set\_theme  
ActionController::Base.template\_root = File.join(RAILS\_ROOT,  
‘themes’, theme) if theme  
end

This will allow you to create theme directories in the root of your  
application.

Note that this will move the theme routing, so you will need to have  
duplicates of all the theme files in the theme folder. There are  
other ways to do it so that it only uses the themed version if the  
file exists, which allows you to set themed portions and leave the  
bulk of your app alone.

As far as a template language goes, check out liquid: http://  
[home.leetsoft.com/liquid](http://home.leetsoft.com/liquid)

Best of luck,  
Jeremy Hubert

---

<div class="post-metadata">

**Author:** ![Irving\_Fried](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/d07c76/32.png) [@Irving\_Fried](https://www.ruby-forum.com/u/Irving_Fried)\
**Post date:** [October 15, 2006, 1:36pm UTC](https://www.ruby-forum.com/t/getting-php-smarty-template-functionality-from-ruby-or-ruby/72184/3 "2006-10-15T13:36:03Z")

</div>

Brilliant. Thank you. So it sounds like Canny is unnecessary. I  
wonder why Canny was created.

By the way, can I get that same functionality from Ruby and Ruby on  
Rails?
