etheracideguitarist
Programmer
- Sep 4, 2008
- 5
For my templating system I have a module which converts pre-defined 'tags' into what is supposed to be replaced by those tags. For the most part the tag is replaced by a subroutine in the search pattern and the subroutine is in charge of doing whatever it must to figure out what to print.
I have about 10 tags which are working with no problem and my template is printing everything exactly as it should. Except for the comments portion. The code above runs the subroutine show_comments and that subroutine tells the template module what to print in the place lf %%show_comments%%
Before I post the code below I will mention that partially does what it is supposed to do. But when viewing the cgi created page which uses the template, only the very first line of the flatfile database is shown.
As shown in the following code everything that is being replaced INTO the template is added to $temp and then I run a return on $temp as you will see. This works like a charm for everything else.
This has no problem printing the "no comments" line if the .dat file doesn't exist. But again, when viewing the script only the first comment is shown. Right now I have three appropriately formatted entries in the database.
To test all of this I used it outside the scope of a template system and turned the $temp = qq~ into just print qq~ and took out the return ($temp) line, made a mini test script out of it and ran that script. It printed out all three formatted comments as it should.
I had even tried $temp .= qq~the code~; but that had an even worse effect on the cgi created page.
None of the previous tags required a soubroutine to print something within a foreach loop. Whatever the subroutine printed was printed only once. I believe that this will come up at least a couple more times throughout my program so I'd like to try to solve it. I will gladly offer credit to those that are able to offer a solution that makes this perform as it should.
I greatly appreciate any help. Please let me know if you need any clarification.
Code:
open(TEMP, "$site_path/templates/template.cms");
$template = do { local $/; <TEMP> };
close(TEMP);
$template =~ s/%%show_comments%%/&show_comments;/eg;
I have about 10 tags which are working with no problem and my template is printing everything exactly as it should. Except for the comments portion. The code above runs the subroutine show_comments and that subroutine tells the template module what to print in the place lf %%show_comments%%
Before I post the code below I will mention that partially does what it is supposed to do. But when viewing the cgi created page which uses the template, only the very first line of the flatfile database is shown.
As shown in the following code everything that is being replaced INTO the template is added to $temp and then I run a return on $temp as you will see. This works like a charm for everything else.
Code:
sub show_comments {
if (-e "$site_path/comments/$comments_number.dat") {
open(FILE,"$site_path/comments/$comments_number.dat");
@comments = <FILE>;
close(FILE);
foreach $comment (@comments)
{
chomp($comment);
($date,$commenter,$comment_text)=split(/\|/,$comment);
$temp = qq~
<table>
<tr>
<td>$date</td><td>$commenter</td>
</tr>
<td colspan=2>
$comment_text
</td>
</tr>
</table>
<br />
~;
return ($temp)
}
}else{
$temp = qq~There are currently no comments for this entry.~; return ($temp)
}
}
This has no problem printing the "no comments" line if the .dat file doesn't exist. But again, when viewing the script only the first comment is shown. Right now I have three appropriately formatted entries in the database.
To test all of this I used it outside the scope of a template system and turned the $temp = qq~ into just print qq~ and took out the return ($temp) line, made a mini test script out of it and ran that script. It printed out all three formatted comments as it should.
I had even tried $temp .= qq~the code~; but that had an even worse effect on the cgi created page.
None of the previous tags required a soubroutine to print something within a foreach loop. Whatever the subroutine printed was printed only once. I believe that this will come up at least a couple more times throughout my program so I'd like to try to solve it. I will gladly offer credit to those that are able to offer a solution that makes this perform as it should.
I greatly appreciate any help. Please let me know if you need any clarification.