How does contextual menus work in Drupal 7? I've been trying to understand this for a while now, but I find the documentation either to lacking, or maybe I just don't understand what I'm looking for. Could anyone enlighten me on how to implement them?

One thing I'm trying to do is to be able to have contextual menus for each node in a list of nodes (loaded using node_load_multiple) that I'm sending along to a theme() function and looping through inside the .tpl.php
I'll put my temporary solution here for now anyway in case it will help someone.

What I did was just creating a new theme function (?) in my module

$themes['contextual'] = array(
    'template' =&gt; 'contextual',
    'variables' =&gt; array('links' =&gt; array(), 'destination' =&gt; null)
);


Then I defined my contextual.tpl.php

&lt;div class="contextual-links-wrapper"&gt;
    &lt;ul class="contextual-links"&gt;
        &lt;?php foreach($links as $url =&gt; $title): ?&gt;
        &lt;li&gt;
            &lt;?php echo l($title, $url, 
                array('query' =&gt; array('destination' =&gt; $destination))) ?&gt;
        &lt;/li&gt;
        &lt;?php endforeach; ?&gt;
    &lt;/ul&gt;
&lt;/div&gt;


Finally, to use it, wherever there is a need for a contextual menu, add the class contextual-links-region to the container div of the node (or whatever), and inside the div I use my function, for example: 

&lt;?php echo theme('contextual', array('destination' =&gt; 'node/'.arg(1),
    'links' =&gt; array('node/'.$somenode-&gt;nid.'/edit', t('Edit node')))); ?&gt;