You are viewing a plain text version of this content. The canonical link for it is here.
Posted to modperl@perl.apache.org by Erik Norgaard <no...@locolomo.org> on 2007/01/14 15:14:40 UTC

OT: Really wierd problem with TT2

Hi:

I know this is not modperl specific, but, I use TemplateToolkit2 to 
create pages on my site. My site contains a lot of photos and I want to 
add listings with thumbnail preview. But element attributes magically 
appears when generating the page. The page contains no java script or 
other stuff that can account for this:

In my handler I create a list reference with one entry preview per page 
like this:

   my $item;
   $item->{path}  = getPath($page->{page_id});
   $item->{title} = $page->{title};
   $item->{ctime} = dateStr($page->{ctime});

   my $text = $page->{page};
   if (my ($src) = $text =~ /<img.*?src="(.*?)">/io) { # Get image source
     $src =~ s/\/?photo/photo\/thumb/gio;
     $item->{img} = $src;
   }

   $text =~ s/<object.*?>.*?<\/object>//go; # Remove object-element
   $text =~ s/<h\d id="title">.*?<\/h\d>//gio;
   $text =~ s/<p>\n?<\/p>/\n/go;         # Replace paragraph by newline
   $text =~ s/<.*?>//go;                 # Remove all elements
   $text = substr($text,0,256);          # Get first 256 chars
   $text =~ s/\.[^\.]*?$/./go;           # Chomp to last period
   $text =~ s/\r?\n\r?\n\r?/\n/go;       # Remove double new-lines
   $item->{text} = $text;

So, the item contains the title, extract of the content and possibly 
path to a thumbnail image.

In my template I have this:

[% FOREACH item IN list %]
<div class="item">
[% IF item.img %]
<div class="thumb">
<a href="[% item.path %]" title="[% item.title %]">
   <img src="[% item.img %]"/>
</a>
</div>
[% END %]
<p>
<b><a href="[% item.path %]" title="[% item.title %]">
   [% item.title %]
</a></b>
Posted: [% item.ctime %]<br/>
[% item.text %]
</p>
</div>
[% END %]

Which produces this output:

<div class="item">
<div class="thumb">
<a href="/photos/81_19/" title="Beach of Paris">
   <img src="photo/thumb/81_19.jpg" alt="Beach of Paris" title="Beach of 
Paris" class="photo/thumb"/>
</a>
</div>
<p>
<b><a href="/photos/81_19/" title="Beach of Paris">Beach of Paris</a></b>
Posted: 2007-01-07 00:42:25 UTC+01<br/>
This august in Paris they ...
</p>
</div>

So: where does the alt, title and class attributes in the image element 
come from??? I tried first to set these in the template, and discovered 
the dublicates when I tried to validate the result. Now, removing these 
attributes from the template and restarting Apache several times, I 
still get these extra "ghost" attributes.

If you like to see for yourself without my edits, the generated output 
is here: http://www.locolomo.org/photos/ and the full template is here: 
http://www.locolomo.org/pub/src/xcms/tt2/list.tt2

Thanks, Erik
-- 
Ph: +34.666334818                      web: http://www.locolomo.org

Solved: OT: Really wierd problem with TT2

Posted by Erik Norgaard <no...@locolomo.org>.
Perrin Harkins wrote:
> Erik Norgaard wrote:
>> So: where does the alt, title and class attributes in the image element 
>> come from???
> 
> This is something you will have to figure out by experimenting.  Print 
> the contents of $item->{img} to your error_log.  Take the item.img part 
> out of your template.  Change things until you understand where it's 
> coming from.

Yes .... taking a few hours off the bug is obvious, in the parsing for 
the source path of the image:

   if (my ($src) = $text =~ /<img.*?src="(.*?)">/io) {

Should be:

   if (my ($src) = $text =~ /<img.*?src="([^"]*)".*?>/io) {

The first takes all the attributes of the original image ... DUH!

Well, thanks.

Cheers Erik

-- 
Ph: +34.666334818                      web: http://www.locolomo.org

Re: OT: Really wierd problem with TT2

Posted by Perrin Harkins <pe...@elem.com>.
Erik Norgaard wrote:
> So: where does the alt, title and class attributes in the image element 
> come from???

This is something you will have to figure out by experimenting.  Print 
the contents of $item->{img} to your error_log.  Take the item.img part 
out of your template.  Change things until you understand where it's 
coming from.

- Perrin