How to use Campaign ID (as a token) in Contribution Invoice/Email Receipt Template
First off, I realize I am extremely late to the party - but I just read this question in the midst of trying to resolve a similar issue where I needed to know what template variables are available to a PDF template.

What I did to find template variables when {debug} isn't working for a direct download etc was:

In /CRM/Core/Smarty.php, Line 192 temporarily change

$this-&gt;security = TRUE;


to

$this-&gt;security = FALSE;


Let me emphasize that this is for a development environment ONLY. Please please please under no circumstances do this on a production site (you are turning off very important security features, I cannot emphasize that enough).

Now we can call protected functions in our smarty templates to print out all template variables in several ways. I did it this way:

&lt;pre&gt;
{php}print_r(CRM_Core_Smarty::get_template_vars());{/php}
&lt;/pre&gt;


From a quick glance at that, it didn't look to me like campaign information is available to this template already, and it looked like contribution_id  is only available under 'line_item'... so I would put something like this in the message template (check out /civicrm/api/explorer for more examples):

{foreach from=$lineItem item=value key=priceset}
  {crmAPI var='result' entity='Contribution' action='getsingle' sequential=0 return='campaign_id' id=`$value.contribution_id`}
    {foreach from=$result item=youritem}
      {$youritem.campaign_id}
      {$youritem.contribution_campaign_title}
  {/foreach}
{/foreach}