Here is a simple example of a dynamic menu using $submenu and is related to channels so you get a better idea of whats going on.
Code:
menu channel {
  demonstration
  .$submenu($demo($1,$chan))
}
alias demo {
  //echo -st demo $1-
  if ($1 == begin) { return - } 
  if ($1 == end)   { return - }
  if ($chan($1)) {
    if ($chan($1) == $2) { return Users $right($calc(10000 + $nick($chan($1),0)),4) $chan($1) $chr(9) Active Channel : democommand $chan($1) }
    else                 { return Users $right($calc(10000 + $nick($chan($1),0)),4) $chan($1) : democommand $chan($1) }
  }
}
alias democommand {
  //echo -t $1 DemoCommand tripped for this channel
}


what happens when $submenu($demo($1,$chan)) is encountered during the right click menu building process is the following
(1) $demo($1,$chan) is called as an identifier, $1has been loaded with the word "begin" $chan is the channel you called it from
(2) $demo($1,$chan) well then be called with $1 containing "1" then "2" then "3" etc up to 250 OR untill $demo returns nothing.
(3) $demo($1,$chan) is lastely called with $1 containing "end"
Now what ever you return from this identifier is used as the menu name and command
Please note that I included the active $chan to be passed to the $demo identifier becuase I wanted to, not becuase it was needed to be, only the $1 is needed

ok so whats the $demo do?
if its "begin" or "end" i return "-" because I want line seprators in my menu, if u dont want them then return $null
if its a number first i check if $chan(number) is a channel, and if it is, then i check if that channel name is $2 (aka $chan i passed it), and if so i return the users in channel the channel name, a tab and the words active channel THEN the : and THEN the command im going to call and the parameters to the command
if it wasnt the active channel i return the same just minus the tab and words active channel

just so your clear heres an example assuming 4 channels #qwerty #asdfgh #zxcvbn #poiuyt and that im in #asdfgh when i right click.
Code:
$demo($1,$chan) => $demo(begin,#asdfgh) => return -
$demo($1,$chan) => $demo(1,#asdfgh)     => return Users 0123 #qwerty : democommand #qwerty
$demo($1,$chan) => $demo(2,#asdfgh)     => return Users 0987 #asdfgh <tab> Active Channel : democommand #asdfgh
$demo($1,$chan) => $demo(3,#asdfgh)     => return Users 0234 #zxcvbn : democommand #zxcvbn
$demo($1,$chan) => $demo(4,#asdfgh)     => return Users 0876 #poiuyt : democommand #poiuyt
$demo($1,$chan) => $demo(5,#asdfgh)     => return
$demo($1,$chan) => $demo(end,#asdfgh)   => return -


PS: i left the line //echo -st demo $1- in so when ever you right click you can see in the status window exactly what is being sent to $demo for evaluation into a reply :-)

So just to sume up a $submenu($id($1)) well called $id with $1 in it being "begin" "1" "2" "3" upto "250" then "end", what you return from that $id is your submenu, it might be about channles it might be about $snick could be anything really.