Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

HTML::Template loop problem

Status
Not open for further replies.

audiopro

Programmer
Apr 1, 2004
3,165
GB
I am creating a series of identical popup_menus within a template but I cannot work out how to assign each one a different name. At the moment I have numerous values of the last named menu.
There are other vars in the template which are loaded within the same loop before and after the room values.

I know I could store the values until after the last one has been assigned and then upload all the room vars at once but there must be a simpler way than that.

Code:
$part values = Room1, Room2, Room3, Room4 etc.

$ROOMLIST= $query->popup_menu(-name=>$part,
	-values=>\@RoomTypes,
	-default=>$RoomType);
$template->param(ROOMTYPE => "$ROOMLIST");

Keith
 
Hello,

Not quite sure if this is what you are looking for. However, i've made use of TMPL_LOOP so that you do not have to include a TMPL_VAR for every "roomlist". I've also used a for loop instead of storing each name. I apologise if this has nothing to do with your problem:

Code:
my %hash;
for (my $i = 1; $i <= 10; $i++) {
	push (@{$hash{'Data'}}, {'Before' => "Stuff before"});
	
	my $ROOMLIST = $query->popup_menu(-name=>"Room$i",    
																-values=>\@RoomTypes,
																-default=>$RoomType
	);
	push (@{$hash{'Data'}}, {'Menu' => $ROOMLIST});
	
	push (@{$hash{'Data'}}, {'After' => "Stuff after"});
}

my $template = HTML::Template->new(filename => "Template.htm");
$template->param(ALLDATA => \@{$hash{'Data'}});
print "Content-Type: text/html\n\n", $template->output;

Code:
<TMPL_LOOP NAME=ALLDATA>
     <p><TMPL_VAR NAME=BEFORE></p>
     <p><TMPL_VAR NAME=MENU></p>
     <p><TMPL_VAR NAME=AFTER></p>
</TMPL_LOOP>

Chris
 
Thanks Chris
The data is already in a TMPL_LOOP which iterates through a MySQL table. There are 20 Room names in the middle of the table surrounded by other information to be displayed in the template.
The template forms tabular data in the form of a table and is created line by line. If I used another TMPL_LOOP I would only be able to put all the room information in one row which is something I want to avoid.
I have it working like that at the moment where it stores all the values and only passes them to the template whhen it reaches tha last one, not an ideal solution but it is the best I have come up with so far.

Keith
 
Hello,

I understand better now. I think that the way you currently do it is your only option as I don't think there is a way to pass a value to the template using $template->output multiple times (without obviously printing the entire template multiple times). Maybe somebody else will have a solution or there are possibly alternative templating modules which have better support for your requirements. Goodluck,

Chris
 
Thanks
This job is a bit of a one off so not really worth labouring long and hard on another solution. It works now but I thought I might be able to create a more elegant solution.
I have only recently moved over to Template, having previously rolled my own routines. The annoying thing is I could have done it with my old method.
Having said that, the advantages of Template far outweigh the limitations.

Keith
 
I've used Template::Toolkit (template.toolkit.org) which allows you to pass a complex data structure and then embed templates within templates to iterate over it. It was pretty good five or six years ago, and I guess it will have been enhanced since then.

Steve

[small]"Every program can be reduced by one instruction, and every program has at least one bug. Therefore, any program can be reduced to one instruction which doesn't work." (Object::perlDesignPatterns)[/small]
 
Thanks Steve
I may get a look at it but it is working OK for now.
The problem is that it I am iterating through the database table and need to assign the data to the vars as it passes through.

Keith
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top