Neato Tricks That Aren't Documented Anywhere

Warren Smith <wsmith-KfBRzk3UKwrYd54FQh9/[email protected]>
Newsgroups gmane.comp.lang.perl.modules.petal
Message-ID <[email protected]>
Up until now, if I wanted to use petal to pre-select and item in a
selectbox, I would have to do each item twice, like so:

$VAR1 = [
	{ value => 1, label => 'Option 1', selected => 1 },
	{ value => 2, label => 'Option 2', selected => 0 },
	{ value => 4, label => 'Option 3', selected => 0 },
];


<select>
<div petal:repeat="option options">
<option petal:condition="true: option/selected" petal:attributes="value
option/value" petal:content="option/label" selected="selected">Option
1</option>
<option petal:condition="false: option/selected" petal:attributes="value
option/value" petal:content="option/label">Option 2</option>
</div>
</select>


After reading the Petal source, I found that if you use petal:attributes
to assign an attribute an undefined value, the attribute gets omitted,
thusly:


$VAR1 = [
	{ value => 1, label => 'Option 1', selected => 1 },
	{ value => 2, label => 'Option 2' },
	{ value => 4, label => 'Option 3' },
];

<select>
<option petal:attributes="value option/value; selected option/selected"
petal:content="option/label">Option 1</option>
</select>




Secondly, I developed a decode: modifier that works similar to Oracle's
decode statement. It should be making it into the next release of
Petal::Utils. So, making even/odd rows of a table different classes is
relatively easy.

Example:

<table>
<tr class="decode: repeat/even 1 'even' 'odd'">...</tr>
</table>




Finally, correct me if I'm wrong, but with stock Petal, you cannot
include a file with a variable filename. So Jean-Michel helped me create
an include: modifier that allows you to use variable names as include
files.

Example:

<html>
<head>
<title>My Site!</title>
</head>
<body>
<div petal:replace="structure include: main_content" />
</body>
</html>

This allows me to have a main site layout page, and then just set
main_content to whatever I want the content to be.


Yeah, yeah. I'm going to get a lot of "you didn't know that?" looks, but
I didn't (wasn't documented) and thought somebody else may be having the
same problem.
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.