Gossamer Forum
Home : Products : Links 2.0 : Customization :

template parser hack (untested)

Quote Reply
template parser hack (untested)
An idea for a quick hack...

This will add the following operators to the template parser: ne, !=, and, or, =~, !~.

Assuming you already have the enhanced template support mod installed...

in sub parse, replace
Code:
$line =~ s/((.*?)$begin\s*if(not)?\s+(.+?)\s*((<|>|lt|gt|eq|=)\s*["']?(.+?)["']?)?$end(.*))/
with:
Code:
$line =~ s/((.*?)$begin\s*if(not)?\s+(.+?)\s*((<|>|lt|gt|eq|=|ne|!=|=~|!~|and|or)\s*["']?(.+?)["']?)?$end(.*))/
and after:
Code:
($comp eq '<') and ($result = ($self->{'vars'}{$var} < $val));
add:
Code:
($comp eq 'ne') and ($result = ($self->{'vars'}{$var} ne $val));
($comp eq '!=') and ($result = ($self->{'vars'}{$var} != $val));
($comp eq '=~') and ($result = ($self->{'vars'}{$var} =~ $val));
($comp eq '!~') and ($result = ($self->{'vars'}{$var} !~ $val));
($comp eq 'and') and ($result = ($self->{'vars'}{$var} and $self->{'vars'}{$val}));
($comp eq 'or') and ($result = ($self->{'vars'}{$var} or $self->{'vars'}{$val}));
--Drew
Free, hot camel soup for Links hackers...
http://www.camelsoup.com
Quote Reply
Re: template parser hack (untested) In reply to
Looks good and simple - will give it a try.

Installations:http://www.wiredon.net/gt/
Favicon:http://www.wiredon.net/favicon/

Quote Reply
Re: template parser hack (untested) In reply to
Minor point. I was trying out your hack and there is a typo in this part
In Reply To:
add:


($comp eq 'ne') and ($result = ($self->{'vars'}{$var} ne $val));
($comp eq '!=') and ($result = ($self->{'vars'}{$var} != $val));
($comp eq '=~') and ($result = ($self->{'vars'}{$var} =~ $val));
($comp eq '!~) and ($result = ($self->{'vars'}{$var} !~ $val));
($comp eq 'and') and ($result = ($self->{'vars'}{$var} and $self->{'vars'}{$val}));
($comp eq 'or') and ($result = ($self->{'vars'}{$var} or $self->{'vars'}{$val}));
it's missing a single quote here:

add:


($comp eq 'ne') and ($result = ($self->{'vars'}{$var} ne $val));
($comp eq '!=') and ($result = ($self->{'vars'}{$var} != $val));
($comp eq '=~') and ($result = ($self->{'vars'}{$var} =~ $val));
($comp eq #HERE--> '!~' ) <--#HERE and ($result = ($self->{'vars'}{$var} !~ $val));
($comp eq 'and') and ($result = ($self->{'vars'}{$var} and $self->{'vars'}{$val}));
($comp eq 'or') and ($result = ($self->{'vars'}{$var} or $self->{'vars'}{$val}));

Might save somebody one less error code or two, otherwise thanks for the extremely helpful and simple idea.

(Edited by Intheroom - thanx for the heads up)
Quote Reply
Re: template parser hack (untested) In reply to
oops...I've made the change to the my original post. BTW, it's nearly imposible to read perl code when its jumbled together...

Here's a hack to add variables from within the template files themselves:

in Template.pm, above:
Code:
# Now go line by line and strip out the unwanted stuff looking for
add:
Code:
#now set any local variables for this template
$temp =~ s/<%set\s*(\w+)\s*%>(.+?)<%endset%>/
($self->{'vars'}{$1} = $2) and ''
/sge;
In your templates, you can now add tags like this:
Code:
<%set color%>blue<%endset%>
Usefull if you create a 'globals' include file with table headers, font colors, etc.

And this IS tested. I'm using it in Links' parser and in the one I wrote myself.

Code:
s;[\d\$&(\^)];;g+s;\.; ;g+s;(.)(..);$2$1;g+print,if$_='&61k4I.)l678il.edn7(K2e^ny$';
Quote Reply
Re: template parser hack (untested) In reply to
Hi Junko,
Thank you for the mod. Now I am a real newbie to perl. Can you please tell me what these commands will do. I'm learning as we speak but I'm not there yet.
Thank You,
Pawel Kowalski

Hot Web Hosting
Low cost web hosting:
http://www.thehotweb.net/webhosting
Quote Reply
Re: template parser hack (untested) In reply to
In Reply To:
Can you please tell me what these commands will do.
Do you mean this:
Code:
$temp =~ s/<%set\s*(\w+)\s*%>(.+?)<%endset%>/
($self->{'vars'}{$1} = $2) and ''
/sge;
here's a commented version:
Code:
$temp =~ s/ #begin a search
<%set #start of the 'set' tag
\s* #0 or more spaces
(\w+) #name of the variable
\s* #0 or more spaces
%> #end of the 'set' tag
(.+?) #value of the variable
<%endset%> #end of the 'set' block
/
($self->{'vars'}{$1} = $2) and '' #add the name/value to the vars hash
#and return an empty value
/sgex; #s=whitespace g=all occurences
#e=evaluate x=extended regexp
if you mean the code at the bottom of my signature, run it from telnet...

--Drew

Code:
s;[\d\$&(\^)];;g+s;\.; ;g+s;(.)(..);$2$1;g+print,if$_='&61k4I.)l678il.edn7(K2e^ny$';
Quote Reply
Re: template parser hack (untested) In reply to
Thanks Drew,
But what I meant was what do these mean/what action they perform. I'd be greatful if you or someone else could explain it to me. I am learning perl but have not gotten that far from reading my resources, all I have are the basics and this looks rather complicated.
Thank You,
Pawel Kowalski

Hot Web Hosting
Low cost web hosting:
http://www.thehotweb.net/webhosting
Quote Reply
Re: template parser hack (untested) In reply to
If you're asking about the original post in this thread what has been added are additional string and numeric operators that can be used to perform <%if this equals that%> then do something <%endif%> statements in your templates in order to give more specific dynamic content to your pages. The original mod did not have those operators which Drew has attempted to add above. You might try here
http://www.extropia.com/...rl_faq.html#equality
or any other of self-help sites on Perl.

On the otherhand I thought the <%set%> stuff was pretty well explained.