[Rg 115] Transferring data from a controller to a template

Lars Olsson lasso at lassoweb.se
Wed Dec 12 14:49:56 EST 2007


Hi list!

I find it somewhat strange that only the instance variables of a 
controller are available to the templates that get rendered by the very 
same controller. This may of course be by design, but it would be nice 
if somebody could explain why the class variables and constants of the 
controller should be left out.

A small example:
Lets say we have the following Controller. The controller does not use 
any templates at all.

class TestController < Ramaze::Controller

	FOO = 'I am the constant FOO.'

	def initialize
		@@bar = 'I am the class variable @@bar.'
		@baz = 'I am the instance variable @baz.'
	end

	def index
		# Nothing to see here, move along...
	end

	def test_const_val()
		FOO
	end

	def test_class_var()
		@@bar
	end

	def test_instance_var()
		@baz
	end

end

Now, if we start Ramaze, we can visit all pages without any problems. 
All constants, class variables and instance variables gets printed 
correctly.

Lets add some templates:
[test_class_var.xhtml]
<html>

	<head>
		<title></title>
	</head>

	<body style="background-color: silver;">

		#{@@bar}

	</body>

</html>

[test_const_val.xhtml]
<html>

	<head>
		<title></title>
	</head>

	<body style="background-color: silver;">

		#{FOO}

	</body>

</html>

[test_instance_var.xhtml]
<html>

	<head>
		<title></title>
	</head>

	<body style="background-color: silver;">

		#{@baz}

	</body>

</html>

All of a sudden the pages that are displaying the constant and the class 
variable stop working. Ramaze complains that Ramaze::Action doesn't know 
of FOO and @@bar. This seems a little strange to me...Is there any 
reason why class variables and constants available to the controller 
shouldn't be available to the template as well?


Sincerely

/lasso

-- 
________________________________________
Lars Olsson
lasso at lassoweb.se
http://www.lassoweb.se/


More information about the Ramaze-general mailing list