Day 7 – Looping Using Foreach

Goal: Demonstrate looping in rules

ruleset a60x53 {
  meta {
    name "Looping using foreach"
    author "Mike Grace"
    description <<
    logging on

  global {
    arrayOfAwesomeness = [

  rule show_my_list_of_awesomeness {
    select when pageview ".*"
    foreach arrayOfAwesomeness setting (twitterHandle)
    pre {
      newContent =<<
        <p>I think <a href="{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 using bookmarklet

Get the bookmarklet to try it out yourself!

Gratuitous day 7 crazy face

I’m staring at you through your screen… muahahaha!
This entry was posted in Kynetx and tagged . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s