Index: /branches/release-35/php/lib/block.mtentrycategories.php
===================================================================
--- /branches/release-35/php/lib/block.mtentrycategories.php (revision 1174)
+++ /branches/release-35/php/lib/block.mtentrycategories.php (revision 1926)
@@ -7,5 +7,5 @@
 
 function smarty_block_mtentrycategories($args, $content, &$ctx, &$repeat) {
-    $localvars = array('_categories', 'category', '_categories_counter');
+  $localvars = array('_categories', 'category', '_categories_counter', '__out');
     if (!isset($content)) {
         $ctx->localize($localvars);
@@ -14,8 +14,10 @@
         $categories = $ctx->mt->db->fetch_categories($args);
         $ctx->stash('_categories', $categories);
+        $ctx->stash('__out', false);
         $counter = 0;
     } else {
         $categories = $ctx->stash('_categories');
         $counter = $ctx->stash('_categories_counter');
+        $out = $ctx->stash('__out');
     }
     if ($counter < count($categories)) {
@@ -24,8 +26,13 @@
         $ctx->stash('_categories_counter', $counter + 1);
         $repeat = true;
-        if (($counter > 0) && isset($args['glue'])) {
-            $content = $content . $args['glue'];
+        if (isset($args['glue']) && !empty($content)) {
+            if ($out)
+                $content = $args['glue'] . $content;
+            else
+                $ctx->stash('__out', true);
         }
     } else {
+        if (isset($args['glue']) && $out && !empty($content))
+            $content = $args['glue'] . $content;
         $ctx->restore($localvars);
         $repeat = false;
Index: /branches/release-35/php/lib/block.mtloop.php
===================================================================
--- /branches/release-35/php/lib/block.mtloop.php (revision 1174)
+++ /branches/release-35/php/lib/block.mtloop.php (revision 1926)
@@ -7,5 +7,5 @@
 
 function smarty_block_mtloop($args, $content, &$ctx, &$repeat) {
-    $localvars = array('__loop_keys', '__loop_values');
+    $localvars = array('__loop_keys', '__loop_values', '__out');
 
     if (!isset($content)) {
@@ -67,4 +67,5 @@
         $counter = 1;
         $ctx->stash('__loop_values', $value);
+        $ctx->stash('__out', false);
     }
     else {
@@ -72,7 +73,10 @@
         $keys = $ctx->stash('__loop_keys');
         $value = $ctx->stash('__loop_values');
+        $out = $ctx->stash('__out');
         if (!isset($keys) || $keys == 0) {
             $ctx->restore($localvars);
             $repeat = false;
+            if (isset($args['glue']) && $out && !empty($content))
+                $content = $args['glue'] . $content;
             return $content;
         }
@@ -97,7 +101,9 @@
         }
     }
-    if (array_key_exists('glue', $args)) {
-        if (1 < $counter)
-            $content = $content . $args['glue'];
+    if (isset($args['glue']) && !empty($content)) {
+        if ($out)
+            $content = $args['glue'] . $content;
+        else
+            $ctx->stash('__out', true);
     }
     if ( 0 === count($keys) )
Index: /branches/release-35/php/lib/block.mtfor.php
===================================================================
--- /branches/release-35/php/lib/block.mtfor.php (revision 1174)
+++ /branches/release-35/php/lib/block.mtfor.php (revision 1926)
@@ -7,5 +7,5 @@
 
 function smarty_block_mtfor($args, $content, &$ctx, &$repeat) {
-    $localvars = array('__for_end', '__for_var');
+    $localvars = array('__for_end', '__for_var', '__out');
 
     if (!isset($content)) {
@@ -30,4 +30,5 @@
         $ctx->stash('__for_end', $end);
         $ctx->stash('__for_var', $var);
+        $ctx->stash('__out', false);
     } else {
         $index = $ctx->__stash['vars']['__index__'] + 1;
@@ -35,4 +36,5 @@
         $end = $ctx->stash('__for_end');
         $var = $ctx->stash('__for_var');
+        $out = $ctx->stash('__out');
     }
 
@@ -46,10 +48,14 @@
         if ($var)
             $ctx->__stash['vars'][$var] = $index;
-        if (array_key_exists('glue', $args)) {
-            if ($index < $end)
-                $content = $content . $args['glue'];
+        if (isset($args['glue']) && !empty($content)) {
+            if ($out)
+                $content = $args['glue'] . $content;
+            else
+                $ctx->stash('__out', true);
         }
         $repeat = true;
     } else {
+        if (isset($args['glue']) && $out && !empty($content))
+            $content = $args['glue'] . $content;
         $ctx->restore($localvars);
         $repeat = false;
Index: /branches/release-35/php/lib/block.mtassettags.php
===================================================================
--- /branches/release-35/php/lib/block.mtassettags.php (revision 1174)
+++ /branches/release-35/php/lib/block.mtassettags.php (revision 1926)
@@ -7,5 +7,5 @@
 
 function smarty_block_mtassettags($args, $content, &$ctx, &$repeat) {
-    $localvars = array('_tags', 'Tag', '_tags_counter', 'tag_min_count', 'tag_max_count','all_tag_count');
+    $localvars = array('_tags', 'Tag', '_tags_counter', 'tag_min_count', 'tag_max_count','all_tag_count', '__out');
     if (!isset($content)) {
         $ctx->localize($localvars);
@@ -32,4 +32,5 @@
         if (!is_array($tags)) $tags = array();
         $ctx->stash('_tags', $tags);
+        $ctx->stash('__out', false);
         
         $counter = 0;
@@ -37,4 +38,5 @@
         $tags = $ctx->stash('_tags');
         $counter = $ctx->stash('_tags_counter');
+        $out = $ctx->stash('__out');
     }
     if ($counter < count($tags)) {
@@ -43,8 +45,13 @@
         $ctx->stash('_tags_counter', $counter + 1);
         $repeat = true;
-        if (($counter > 0) && isset($args['glue'])) {
-            $content = $content . $args['glue'];
+        if (isset($args['glue']) && !empty($content)) {
+            if ($out)
+                $content = $args['glue'] . $content;
+            else
+                $ctx->stash('__out', true);
         }
     } else {
+        if (isset($args['glue']) && $out && !empty($content))
+            $content = $args['glue'] . $content;
         $ctx->restore($localvars);
         $repeat = false;
Index: /branches/release-35/php/lib/block.mtentryadditionalcategories.php
===================================================================
--- /branches/release-35/php/lib/block.mtentryadditionalcategories.php (revision 1174)
+++ /branches/release-35/php/lib/block.mtentryadditionalcategories.php (revision 1926)
@@ -7,5 +7,5 @@
 
 function smarty_block_mtentryadditionalcategories($args, $content, &$ctx, &$repeat) {
-    $localvars = array('_categories', 'category', '_categories_counter');
+    $localvars = array('_categories', 'category', '_categories_counter', '__out');
     if (!isset($content)) {
         $ctx->localize($localvars);
@@ -23,8 +23,10 @@
         }
         $ctx->stash('_categories', $categories);
+        $ctx->stash('__out', false);
         $counter = 0;
     } else {
         $categories = $ctx->stash('_categories');
         $counter = $ctx->stash('_categories_counter');
+        $out = $ctx->stash('__out');
     }
     if ($counter < count($categories)) {
@@ -33,8 +35,13 @@
         $ctx->stash('_categories_counter', $counter + 1);
         $repeat = true;
-        if (($counter > 0) && isset($args['glue'])) {
-            $content = $content . $args['glue'];
+        if (isset($args['glue']) && !empty($content)) {
+            if ($out)
+                $content = $args['glue'] . $content;
+            else
+                $ctx->stash('__out', true);
         }
     } else {
+        if (isset($args['glue']) && $out && !empty($content))
+            $content = $args['glue'] . $content;
         $ctx->restore($localvars);
         $repeat = false;
Index: /branches/release-35/php/lib/block.mtentrytags.php
===================================================================
--- /branches/release-35/php/lib/block.mtentrytags.php (revision 1174)
+++ /branches/release-35/php/lib/block.mtentrytags.php (revision 1926)
@@ -7,5 +7,5 @@
 
 function smarty_block_mtentrytags($args, $content, &$ctx, &$repeat) {
-    $localvars = array('_tags', 'Tag', '_tags_counter', 'tag_min_count', 'tag_max_count','all_tag_count');
+    $localvars = array('_tags', 'Tag', '_tags_counter', 'tag_min_count', 'tag_max_count','all_tag_count', '__out');
     if (!isset($content)) {
         $class = 'entry';
@@ -37,4 +37,5 @@
         if (!is_array($tags)) $tags = array();
         $ctx->stash('_tags', $tags);
+        $ctx->stash('__out', false);
         
         $counter = 0;
@@ -42,4 +43,5 @@
         $tags = $ctx->stash('_tags');
         $counter = $ctx->stash('_tags_counter');
+        $out = $ctx->stash('__out');
     }
     if ($counter < count($tags)) {
@@ -48,8 +50,13 @@
         $ctx->stash('_tags_counter', $counter + 1);
         $repeat = true;
-        if (($counter > 0) && isset($args['glue'])) {
-            $content = $content . $args['glue'];
+        if (isset($args['glue']) && !empty($content)) {
+            if ($out)
+                $content = $args['glue']. $content;
+            else
+                $ctx->stash('__out', true);
         }
     } else {
+        if (isset($args['glue']) && $out && !empty($content))
+            $content = $args['glue']. $content;
         $ctx->restore($localvars);
         $repeat = false;
Index: /branches/release-35/php/lib/block.mtparentcategories.php
===================================================================
--- /branches/release-35/php/lib/block.mtparentcategories.php (revision 1800)
+++ /branches/release-35/php/lib/block.mtparentcategories.php (revision 1926)
@@ -21,5 +21,5 @@
 
 function smarty_block_mtparentcategories($args, $content, &$ctx, &$repeat) {
-    $localvars = array('_categories', 'category', '_categories_counter','glue');
+    $localvars = array('_categories', 'category', '_categories_counter','glue', '__out');
     if (!isset($content)) {
         $ctx->localize($localvars);
@@ -39,4 +39,5 @@
         $ctx->stash('_categories', $parents);
         $ctx->stash('glue', $glue);
+        $ctx->stash('__out', false);
         $counter = 0;
     } else {
@@ -44,4 +45,5 @@
         $counter = $ctx->stash('_categories_counter');
         $glue = $ctx->stash('glue');
+        $out = $ctx->stash('__out');
     }
 
@@ -50,10 +52,18 @@
         $ctx->stash('_categories_counter', $counter + 1);
         $repeat = true;
+        if (!empty($glue) && !empty($content)) {
+            if ($out)
+                $content = $glue . $content;
+            else
+                $ctx->stash('__out', true);
+        }
     } else {
+        if (!empty($glue) && $out && !empty($content))
+            $content = $glue . $content;
         $repeat = false;
         $glue = '';
         $ctx->restore($localvars);
     }
-    return $content.$glue;
+    return $content;
 }
 ?>
Index: /branches/release-35/php/lib/block.mtcategories.php
===================================================================
--- /branches/release-35/php/lib/block.mtcategories.php (revision 1174)
+++ /branches/release-35/php/lib/block.mtcategories.php (revision 1926)
@@ -9,5 +9,5 @@
     // status: incomplete
     // parameters: show_empty
-    $localvars = array('_categories', '_categories_counter', 'category', 'inside_mt_categories', 'entries', '_categories_glue', 'blog_id', 'blog');
+    $localvars = array('_categories', '_categories_counter', 'category', 'inside_mt_categories', 'entries', '_categories_glue', 'blog_id', 'blog', '__out');
     if (!isset($content)) {
         $ctx->localize($localvars);
@@ -19,4 +19,5 @@
         $ctx->stash('inside_mt_categories', 1);
         $ctx->stash('show_empty', isset($args['show_empty']) ? $args['show_empty'] : '0');
+        $ctx->stash('__out', false);
         $counter = 0;
     } else {
@@ -24,4 +25,5 @@
         $counter = $ctx->stash('_categories_counter');
         $glue = $ctx->stash('_categories_glue');
+        $out =$ctx->stash('__out');
     }
     if ($counter < count($categories)) {
@@ -35,7 +37,14 @@
         $ctx->stash('blog',
             $ctx->mt->db->fetch_blog($category['category_blog_id']));
-        if ($counter > 0) $content = $content . $glue;
+        if (!empty($glue) && !empty($content)) {
+            if ($out)
+                $content = $glue . $content;
+            else
+                $ctx->stash('__out', true);
+        }
         $repeat = true;
     } else {
+        if (!empty($glue) && $out && !empty($content))
+            $content = $glue . $content;
         $ctx->restore($localvars);
         $repeat = false;
Index: /branches/release-35/php/lib/block.mttags.php
===================================================================
--- /branches/release-35/php/lib/block.mttags.php (revision 1174)
+++ /branches/release-35/php/lib/block.mttags.php (revision 1926)
@@ -7,5 +7,5 @@
 
 function smarty_block_mttags($args, $content, &$ctx, &$repeat) {
-    $localvars = array('_tags', 'Tag', '_tags_counter', 'tag_min_count', 'tag_max_count', 'all_tag_count', 'include_blogs', 'exclude_blogs', 'blog_ids');
+  $localvars = array('_tags', 'Tag', '_tags_counter', 'tag_min_count', 'tag_max_count', 'all_tag_count', 'include_blogs', 'exclude_blogs', 'blog_ids', '__out');
     if (!isset($content)) {
         $ctx->localize($localvars);
@@ -54,8 +54,10 @@
         $ctx->stash('all_tag_count', $all_count);
         $ctx->stash('_tags', $tags);
+        $ctx->stash('__out', false);
         $counter = 0;
     } else {
         $tags = $ctx->stash('_tags');
         $counter = $ctx->stash('_tags_counter');
+        $out = $ctx->stash('__out');
     }
 
@@ -65,8 +67,15 @@
         $ctx->stash('_tags_counter', $counter + 1);
         $repeat = true;
-        if (($counter > 0) && isset($args['glue'])) {
-            $content = $content . $args['glue'];
+        if (isset($args['glue'])) {
+            if ($out && !empty($content))
+                $content = $args['glue'] . $content;
+            if (!$out && !empty($content))
+              $ctx->stash('__out', true);
         }
     } else {
+        if (isset($args['glue'])) {
+            if ($out && !empty($content))
+                $content = $args['glue'] . $content;
+        }
         $ctx->restore($localvars);
         $repeat = false;
Index: /branches/release-35/php/lib/block.mtentries.php
===================================================================
--- /branches/release-35/php/lib/block.mtentries.php (revision 1335)
+++ /branches/release-35/php/lib/block.mtentries.php (revision 1926)
@@ -7,5 +7,5 @@
 
 function smarty_block_mtentries($args, $content, &$ctx, &$repeat) {
-    $localvars = array('entry', '_entries_counter','entries','current_timestamp','modification_timestamp','_entries_lastn', 'current_timestamp_end', 'DateHeader', 'DateFooter', '_entries_glue', 'blog', 'blog_id', 'conditional', 'else_content');
+    $localvars = array('entry', '_entries_counter','entries','current_timestamp','modification_timestamp','_entries_lastn', 'current_timestamp_end', 'DateHeader', 'DateFooter', '_entries_glue', 'blog', 'blog_id', 'conditional', 'else_content', '__out');
     if (isset($args['sort_by']) && $args['sort_by'] == 'score' && !isset($args['namespace'])) {
         return $ctx->error($ctx->mt->translate('sort_by="score" must be used in combination with namespace.'));
@@ -29,7 +29,9 @@
         $lastn = $args['lastn'];
         $ctx->stash('_entries_lastn', $lastn);
+        $ctx->stash('__out', false);
     } else {
         $lastn = $ctx->stash('_entries_lastn');
         $counter = $ctx->stash('_entries_counter');
+        $out = $ctx->stash('__out');
     }
     if (!isset($args['class'])) {
@@ -119,7 +121,15 @@
         $_REQUEST['entry_ids_published'][$entry['entry_id']] = 1;
         $glue = $ctx->stash('_entries_glue');
-        if ($glue != '') $content = $content . $glue;
+        if (isset($glue) && !empty($content)) {
+            if ($out)
+                $content = $glue . $content;
+            else
+                $ctx->stash('__out', true);
+        }
         $repeat = true;
     } else {
+        $glue = $ctx->stash('_entries_glue');
+        if (isset($glue) && $out && !empty($content))
+            $content = $glue . $content;
         $ctx->restore($localvars);
         $repeat = false;
Index: /branches/release-35/php/lib/MTUtil.php
===================================================================
--- /branches/release-35/php/lib/MTUtil.php (revision 1800)
+++ /branches/release-35/php/lib/MTUtil.php (revision 1926)
@@ -891,4 +891,6 @@
         if ($ctx->stash('entry')) {
             $entry = $ctx->stash('entry');
+            if (empty($entry['placement_category_id']))
+                return null;
             if ($class == 'folder')
                 $cat = $ctx->mt->db->fetch_folder($entry['placement_category_id']);
Index: /branches/release-35/lib/MT/Template/ContextHandlers.pm
===================================================================
--- /branches/release-35/lib/MT/Template/ContextHandlers.pm (revision 1911)
+++ /branches/release-35/lib/MT/Template/ContextHandlers.pm (revision 1926)
@@ -1324,5 +1324,4 @@
     my $vars = $ctx->{__stash}{vars} ||= {};
     my $glue = $args->{glue};
-    $glue = '' unless defined $glue;
     my $var = $args->{var};
     for (my $i = $start; $i <= $end; $i += $incr) {
@@ -1336,9 +1335,8 @@
         my $res = $builder->build($ctx, $tokens, $cond);
         return $ctx->error($builder->errstr) unless defined $res;
-        if ($res ne '') {
-            $out .= $glue if $cnt > 1;
-            $out .= $res;
-            $cnt++;
-        }
+        $out .= $glue
+            if defined $glue && $cnt > 1 && length($out) && length($res);
+        $out .= $res;
+        $cnt++;
     }
     return $out;
@@ -1590,5 +1588,4 @@
     my $vars = $ctx->{__stash}{vars} ||= {};
     my $glue = $args->{glue};
-    $glue = '' unless defined $glue;
     foreach my $item (@$var) {
         local $vars->{__first__} = $i == 1;
@@ -1625,5 +1622,5 @@
         return $ctx->error($builder->errstr) unless defined $res;
         if ($res ne '') {
-            $out .= $glue if $i > 1;
+            $out .= $glue if defined $glue && $i > 1 && length($out) && length($res);
             $out .= $res;
             $i++;
@@ -1916,5 +1913,5 @@
     my $tokens = $ctx->stash('tokens');
     my $needs_entries = (($ctx->stash('uncompiled') || '') =~ /<MT:?Entries/i) ? 1 : 0;
-    my $glue = $args->{glue} || '';
+    my $glue = $args->{glue};
     my $res = '';
     local $ctx->{__stash}{all_tag_count} = undef;
@@ -1974,5 +1971,5 @@
         defined(my $out = $builder->build($ctx, $tokens, $cond))
             or return $ctx->error( $builder->errstr );
-        $res .= $glue if $res ne '';
+        $res .= $glue if defined $glue && length($res) && length($out);
         $res .= $out;
     }
@@ -2097,5 +2094,5 @@
     my $entry = $ctx->stash('entry');
     return '' unless $entry;
-    my $glue = $args->{glue} || '';
+    my $glue = $args->{glue};
 
     local $ctx->{__stash}{tag_max_count} = undef;
@@ -2113,5 +2110,5 @@
         defined(my $out = $builder->build($ctx, $tokens, $cond))
             or return $ctx->error( $builder->errstr );
-        $res .= $glue if $res ne '';
+        $res .= $glue if defined $glue && length($res) && length($out);
         $res .= $out;
     }
@@ -4229,5 +4226,5 @@
         return $ctx->error( $builder->errstr ) unless defined $out;
         $last_day = $this_day;
-        $res .= $glue if defined $glue && $i;
+        $res .= $glue if defined $glue && $i && length($res) && length($out);
         $res .= $out;
         $i++;
@@ -4739,13 +4736,14 @@
     my $builder = $ctx->stash('builder');
     my $tokens = $ctx->stash('tokens');
-    my @res;
+    my $res;
+    my $glue = $args->{glue};
     for my $cat (@$cats) {
         local $ctx->{__stash}->{category} = $cat;
         defined(my $out = $builder->build($ctx, $tokens, $cond))
             or return $ctx->error( $builder->errstr );
-        push @res, $out if $out ne '';
-    }
-    my $sep = $args->{glue} || '';
-    join $sep, @res;
+        $res .= $glue if defined $glue && length($res) && length($out);
+        $res .= $out;
+    }
+    $res;
 }
 
@@ -6314,5 +6312,5 @@
     my $builder = $ctx->stash('builder');
     my $tokens = $ctx->stash('tokens');
-    my $glue = exists $args->{glue} ? $args->{glue} : '';
+    my $glue = $args->{glue};
     ## In order for this handler to double as the handler for
     ## <MTArchiveList archive_type="Category">, it needs to support
@@ -6356,5 +6354,5 @@
               ArchiveListFooter => $last }))
             or return $ctx->error( $builder->errstr );
-        $res .= $glue if $res ne '';
+        $res .= $glue if defined $glue && length($res) && length($out);
         $res .= $out;
         $cat = $next_cat;
@@ -6637,5 +6635,6 @@
     my $builder = $ctx->stash('builder');
     my $tokens = $ctx->stash('tokens');
-    my @res;
+    my $res;
+    my $glue = $args->{glue};
     for my $cat (@$cats) {
         next if $e->category && ($cat->label eq $e->category->label);
@@ -6643,8 +6642,8 @@
         defined(my $out = $builder->build($ctx, $tokens, $cond))
             or return $ctx->error( $builder->errstr );
-        push @res, $out;
-    }
-    my $sep = $args->{glue} || '';
-    join $sep, @res; 
+        $res .= $glue if defined $glue && length($res) && length($out);
+        $res .= $out if length($out);
+    }
+    $res;
 }
 
@@ -7189,5 +7188,5 @@
     # Get the arguments
     my $exclude_current = $args->{'exclude_current'};
-    my $glue = $args->{'glue'} || '';
+    my $glue = $args->{'glue'};
 
     # Get the current category
@@ -7196,5 +7195,5 @@
     return '' if ($cat eq '');
 
-    my @res;
+    my $res;
 
     # Put together the list of parent categories
@@ -7211,9 +7210,8 @@
             $out = 'cat-' . $c->id;
         }
-        push @res, $out;
-    }
-
-    # Slap them all together with some glue
-    return join $glue, @res;
+        $res .= $glue if defined $glue && length($res) && length($out);
+        $res .= $out if length($out);
+    }
+    $res;
 }
 
@@ -7922,5 +7920,5 @@
     my $asset = $ctx->stash('asset');
     return '' unless $asset;
-    my $glue = $args->{glue} || '';
+    my $glue = $args->{glue};
 
     local $ctx->{__stash}{tag_max_count} = undef;
@@ -7943,5 +7941,5 @@
         defined(my $out = $builder->build($ctx, $tokens, $cond))
             or return $ctx->error( $builder->errstr );
-        $res .= $glue if $res ne '';
+        $res .= $glue if defined $glue && length($res) && length($out);
         $res .= $out;
     }
@@ -8850,5 +8848,4 @@
 
     my $glue = $args->{glue};
-    $glue = q() unless defined $glue;
 
     my $output = q();
@@ -8868,5 +8865,5 @@
             }
             )) or return $ctx->error( $build->errstr );
-        $output .= $glue if $i > 1;
+        $output .= $glue if defined $glue && $i > 1 && length($output) && length($out);
         $output .= $out;
     }
