[Petal] [rfc] if: ... then: ... else: ... modifier

Steve Purkis spurkis at mkdoc.com
Thu Sep 11 13:18:44 BST 2003


Hi all,

If there are no objections, I'll add an 'if:' modifier to the 
Petal-Utils packages.  Here's an example of its use:

	<div petal:set="search_page like: request/uri ^/search.html"
	     petal:set="editor_page like: request/uri ^/edit/">

		<a petal:attributes="class if: search_page then: string: nav-selected 
else: string: nav-unselected"
		   href="/search.html" class="nav">Search</a>
		<a petal:attributes="class if: editor_page then: string: nav-selected 
else: string: nav-unselected"
		   href="/edit/index.html" class="nav">Search</a>

	</div>

The code for this is below.

-Steve

BEGIN{
package Petal::Utils::If;

use Carp;

use base qw( Petal::Utils::Base );
use constant name    => 'if';
use constant aliases => qw();

use Petal::Utils qw();
$Petal::Utils::DEBUG = 1;
push @{$Petal::Utils::PLUGIN_SET{':logic'}}, 'If';

sub process {
     my $class = shift;
     my $hash  = shift;
     my $args  = shift || confess( "'if' expects args of the form 'if: 
... then: ... [else: ...]' (got nothing)!" );

     my @args = $args =~ /\A(.+?)\sthen:\s+(.+?)(?:\s+else:\s+(.+?))?\z/;
     confess( "'if' expects arguments of the form: 'if: ... then: ... 
[else: ...]', not 'if: $args'!" ) unless @args;
     $args[0] || confess( "1st arg to 'if' should be an expression (got 
nothing)!" );
     $args[1] || confess( "2nd arg to 'if' (after then:) should be an 
expression (got nothing)!" );

     return $hash->fetch($args[1]) if $hash->fetch($args[0]);
     return $hash->fetch($args[2]) if $args[2];
     return '';
}

}



More information about the Petal mailing list