# Template/variable confusion

**URL:** <https://www.ruby-forum.com/t/template-variable-confusion/52837>\
**Category:** Rails\
**Created:** [January 19, 2006, 5:15pm UTC](https://www.ruby-forum.com/t/template-variable-confusion/52837 "2006-01-19T17:15:04Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![tachekent](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/7993a0/32.png) [@tachekent](https://www.ruby-forum.com/u/tachekent)\
**Post date:** [January 19, 2006, 5:15pm UTC](https://www.ruby-forum.com/t/template-variable-confusion/52837/1 "2006-01-19T17:15:04Z")

</div>

Hi there,  
I’m having some trouble working out where/when I can define template  
variables.

I have a standard.rhtml layout template which is used across the  
application as the main xhtml wrapper.  
Inside there is some code like this:

```
<div id="<%= @layout_style %>">
<%= @content_for_layout %>
</div>

```

Now content\_for\_layout always works fine, but @layout\_style seems to  
only work if I set it either within a method in a specific controller  
thus:

```
@layout_style = 'content'

```

or when I define it at the top of the appropriate @content\_for\_layout  
template thus:

```
<% @layout_style = 'index' %>

```

I was hoping that I would be able to set variables such as this globally  
within the application.rb controller.  
Or, am I just going about this in completely the wrong way 🙂 ?

I am also having trouble writing (what I think are called) overload  
methods within the model. I have a TIME column named running\_time in my  
Film model and I only ever want it to look like this e.g.: ‘5:32’ rather  
than the fully rendered datetime that is returned by default. So I  
define a method in the model to override the default:

```
def running_time
  # strftime code here
end

```

but whenever I try and reference the value of running\_time from withing  
the method I get a stack overload.  
What would be the best way to do what I initially though should be done  
like this:

```
self.running_time.strftime("%M:%S")

```

?

It’s taking me a while to get the idioms and the heuristics for RoR…  
Thanks in advance for any help available!

dorian

## – I do things for love or money

## +44 (0)7941 219 501

aim:oulalipo | yahoo:tachekent

---

<div class="post-metadata">

**Author:** ![tachekent](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/7993a0/32.png) [@tachekent](https://www.ruby-forum.com/u/tachekent)\
**Post date:** [January 19, 2006, 5:35pm UTC](https://www.ruby-forum.com/t/template-variable-confusion/52837/2 "2006-01-19T17:35:30Z")

</div>

I would create multiple layouts (put them in  
app/views/layouts/???.rhtml), and set them in the controller:

class TestController \< ApplicationController  
layout ‘content’  
end

would use content.rhtml.

Maybe:

def running\_time  
super.strftime("%M:%S")  
end

or:

def running\_time  
read\_attribute(:running\_time).strftime("%M:%S")  
end

Jules

---

<div class="post-metadata">

**Author:** ![tachekent](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/7993a0/32.png) [@tachekent](https://www.ruby-forum.com/u/tachekent)\
**Post date:** [January 19, 2006, 5:50pm UTC](https://www.ruby-forum.com/t/template-variable-confusion/52837/3 "2006-01-19T17:50:10Z")

</div>

Dorian M. wrote:

> but whenever I try and reference the value of running\_time from withing  
> the method I get a stack overload.  
> What would be the best way to do what I initially though should be done  
> like this:
> 
> self.running\_time.strftime("%M:%S")

I think you need to create an alias to the original method.

alias :old\_running\_time :running\_time

def running\_time  
self.send(:old\_running\_time).strftime("%M:%S")  
end

That way it won’t try to keep calling itself.

\_Kevin

---

<div class="post-metadata">

**Author:** ![tachekent](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/7993a0/32.png) [@tachekent](https://www.ruby-forum.com/u/tachekent)\
**Post date:** [January 19, 2006, 5:51pm UTC](https://www.ruby-forum.com/t/template-variable-confusion/52837/4 "2006-01-19T17:51:22Z")

</div>

Thanks for that,

> I would create multiple layouts (put them in  
> app/views/layouts/???.rhtml), and set them in the controller:
> 
> class TestController \< ApplicationController  
> layout ‘content’  
> end
> 
> would use content.rhtml.

I understand that bit, but the layout is so similar that it seems a bit  
unnecessary and against the whole DRY principle to repeat it.  
I know I can use partials but I need foolproof access to the main  
display template.

> end  
> actually I sorted it by using:

self[:running\_time].strftime("%M:%S")

is that bad?

> Jules

thanks again

dorian

## – I do things for love or money

## +44 (0)7941 219 501

aim:oulalipo | yahoo:tachekent
