Goal: Demonstrate looping in rules
ruleset a60x53 {
meta {
name "Looping using foreach"
author "Mike Grace"
description <<
looping!!!
>>
logging on
}
global {
arrayOfAwesomeness = [
"MikeGrace",
"mikefarmer",
"telegramsam",
"alexkolson",
"JessieAMorris",
"Jesse",
"snay2",
"edorcutt",
"1password",
"Kynetx",
"OneTrueFan"
];
}
rule show_my_list_of_awesomeness {
select when pageview ".*"
foreach arrayOfAwesomeness setting (twitterHandle)
pre {
newContent =<<
<p>I think <a href="http://twitter.com/#{twitterHandle}">@#{twitterHandle}</a> is awesome!</p>
>>;
}
{
append("body", newContent);
}
}
rule other_foreacher {
select when pageview ".*"
foreach [0,1,2,3] setting (num)
{
notify("I love counting!","#{num}") with sticky = true;
notify("Yes I do",num) with sticky = true;
}
}
}
- 11-25 This is the global block – The global block is just like a pre block with a few extra features
- the global block runs once for every event execution cycle
- variables set in the global block are available to every rule in the ruleset
- everything you can do in a pre block can be done in the global block
- more global block awesomeness will be shared later
- 12-24 plain array of strings in the global block
- 29 foreach runs everything in the rule for each item in the array given which happens to be ‘arrayOfAwesomeness’ in this case – the current item being iterated on is set to a variable using the ‘setting ()’ syntax
- 31-33 building HTML to append to the page using here doc and current array item
- 42 foreach statements can use inline arrays or any expression that results in an array
- 44 using *beesting string replacement to contrast line 45 (* heard ‘#{}’ called a ‘beesting’ around the office but not sure if that’s what its official name is)
App run on example.com using bookmarklet
Get the bookmarklet to try it out yourself!
Gratuitous day 7 crazy face

