Index: branches/release-35/lib/MT/Template/ContextHandlers.pm
===================================================================
--- branches/release-35/lib/MT/Template/ContextHandlers.pm (revision 1926)
+++ branches/release-35/lib/MT/Template/ContextHandlers.pm (revision 1949)
@@ -2328,4 +2328,5 @@
     my $blog = $ctx->stash('blog') || MT->model('blog')->load($blog_id);
 
+    my %include_recipe;
     my $use_ssi = $blog && $blog->include_system
         && ($arg->{ssi} || $tmpl->include_with_ssi) ? 1 : 0;
@@ -2334,5 +2335,16 @@
         # easiest way to determine this is from the variable
         # space setting.
-        $use_ssi = 0 if $ctx->var('system_template');
+        if ($ctx->var('system_template')) {
+            $use_ssi = 0;
+        } else {
+            my $extra_path = $arg->{cache_key} ? $arg->{cache_key}
+                : $tmpl->cache_path ? $tmpl->cache_path
+                    : '';
+           %include_recipe = (
+                name => $tmpl_name,
+                id   => $tmpl->id,
+                path => $extra_path,
+            );
+        }
     }
 
@@ -2342,10 +2354,10 @@
       && $blog->include_cache
       && ( ( $arg->{cache} && $arg->{cache} > 0 )
-        || $arg->{key}
+        || $arg->{cache_key}
         || ( exists $arg->{ttl} )
         || $tmpl->use_cache ) ? 1 : 0;
     my $cache_key =
-        $arg->{key}
-      ? $arg->{key}
+        $arg->{cache_key}
+      ? $arg->{cache_key}
       : 'blog::' . $blog_id . '::template_' . $type . '::' . $tmpl_name;
     my $ttl =
@@ -2362,6 +2374,6 @@
                 if ($use_ssi) {
                     # base cache expiration on physical file timestamp
-                    my $include_name = $arg->{key} || $tmpl_name;
-                    my $mtime = (stat($blog->include_path($include_name)))[9];
+                    my $include_file = $blog->include_path(\%include_recipe);
+                    my $mtime = (stat($include_file))[9];
                     if ($mtime && (MT::Util::ts2epoch(undef, $latest) > $mtime ) ) {
                         $ttl = 1; # bound to force an update
@@ -2388,10 +2400,9 @@
             return $cache_value if !$use_ssi;
 
-            my $include_name = $arg->{key} || $tmpl_name;
             # The template may still be cached from before we were using SSI
             # for this template, so check that it's also on disk.
-            my ($path, $file_path) = $blog->include_path($include_name);
-            if ($blog->file_mgr->exists($file_path)) {
-                return $blog->include_statement($include_name);
+            my $include_file = $blog->include_path(\%include_recipe);
+            if ($blog->file_mgr->exists($include_file)) {
+                return $blog->include_statement(\%include_recipe);
             }
         }
@@ -2424,7 +2435,7 @@
 
     if ($use_ssi) {
-        my $include_name = $arg->{key} || $tmpl_name;
+        my ($include_file, $path, $filename) =
+            $blog->include_path(\%include_recipe);
         my $fmgr = $blog->file_mgr;
-        my ($path, $file_path) = $blog->include_path($include_name);
         if (!$fmgr->exists($path)) {
             if (!$fmgr->mkpath($path)) {
@@ -2433,15 +2444,16 @@
             }
         }
-        defined($fmgr->put_data($ret, $file_path))
+        defined($fmgr->put_data($ret, $include_file))
             or return $ctx->error(MT->translate("Writing to '[_1]' failed: [_2]",
-                $file_path, $fmgr->errstr));
+                $include_file, $fmgr->errstr));
 
         MT->upload_file_to_sync(
-            url  => $blog->include_url($include_name),
-            file => $file_path,
+            url  => $blog->include_url(\%include_recipe),
+            file => $include_file,
             blog => $blog,
         );
 
-        return $blog->include_statement($include_name);
+        my $stat = $blog->include_statement(\%include_recipe);
+        return $stat;
     }
 
