Using HTML Tags as “Glue”


Published on October 3, 2004 at 12:38 PM EST
Last updated on October 9, 2006 at 12:02 AM EST
In the Tutorials category.

In creating templates for your Movable Type installation, you’ll find that the <MTEntryCategories>, <MTCategories>, and <MTParentCategories> container tags have a “glue” attribute. The glue attribute is used to separate items with a string. For example, joining category/subcategory names together with…

In creating templates for your Movable Type installation, you’ll find that the <MTEntryCategories>, <MTCategories>, and <MTParentCategories> container tags have a “glue” attribute. The glue attribute is used to separate items with a string. For example, joining category/subcategory names together with a comma or slash to create breadcrumbs (a path tracing your entry’s hierarchy). A simple example of breadcrumbs might look like this:

Category \ Subcategory \ Entry

Often it is desirable to use an HTML tag as the “glue,” though quickly trying it you’ll find it doesn’t work. The extra greater-than and less-than signs cause it to not work, as in <MTEntryCategories glue="<br />">.... With a little use of PHP, though, this problem can be overcome, allowing you to use the <br />, <img />, or any other HTML tag as your glue!

First, an example of creating breadcrumbs with a simple (non-HTML) string as the glue. A comma is the glue for the <MTEntryCategories> tag and a greater-than sign is the glue for the <MTParentCategories> tag.

<MTEntryCategories glue=", ">
<MTParentCategories glue=" &#62; ">
<a href="<MTCategoryArchiveLink>"><MTCategoryLabel></a>
</MTParentCategories>&br /> </MTEntryCategories> &#62;
<b><MTEntryTitle></b>

Which creates breadcrumbs that look like this:

Category 1 > Subcategory A, Category 2 > Subcategory B > Entry Title

But you probably already knew how to do that, and want to use an HTML entity as the glue. Keep reading.

PHP, HTML and Glue

With some PHP code, HTML tags can be substituted as the glue for the simple comma and greater-than sign strings, as in the breadcrumb example below. A comma and a <br /> tag was used with <MTEntryCategories> (in the $MTEntryCategoriesGlue variable) and a greater-than sign was used with <MTParentCategories> (in the $MTParentCategoriesGlue variable).

<?php

  $MTEntryCategoriesGlue = ',<br />';
  $MTParentCategoriesGlue = ' &#62; ';

  echo '<MTEntryCategories glue=",' . $MTEntryCategoriesGlue . '">
<MTParentCategories glue="' . $MTParentCategoriesGlue . '">
<a href="<MTCategoryArchiveLink>"><MTCategoryLabel></a>
</MTParentCategories>
</MTEntryCategories>' . $MTParentCategoriesGlue . '<b><MTEntryTitle></b>';

?>

Which results in the following breadcrumbs:

Category 1 > Subcategory A,
Category 2 > Subcategory B > Entry Title

By changing the $MTEntryCategoriesGlue and $MTParentCategoriesGlue variables you can change the glue attribute:

Category 1 divider Subcategory A divider
Category 2 divider Subcategory B divider Entry Title

Site-Wide Glue Variables

Putting these two variables into a generic PHP Include’d template can allow you to make a site-wide glue change by only changing the variable in one place. For example, create a new index template named “phpitems.inc” and include the below code. Don’t forget to rebuild.

<?php

  $MTEntryCategoriesGlue = ',<br />';
  $MTParentCategoriesGlue = ' &#62; ';

?>

Then, in each template where you want to make use of either variable, add this code, being sure the path to your phpitems.inc file is correct:

<?php

  include('/home/user/domain/html/phpitems.inc');

  echo '<MTEntryCategories glue=",' . $MTEntryCategoriesGlue . '">
<MTParentCategories glue="' . $MTParentCategoriesGlue . '">
<a href="<MTCategoryArchiveLink>"><MTCategoryLabel></a>
</MTParentCategories>
</MTEntryCategories>' . $MTParentCategoriesGlue . '<b><MTEntryTitle></b>';

?>

And there you have it, using HTML tags as “glue” for Movable Type tags!

This article is tagged as: Categories, Glue, HTML, PHP, Template Tags, Tutorials

If you found this article useful, please consider supporting this site through a donation.

Comments

So far, there are 4 comments and Trackbacks on this entry. Add yours!

1

Movable Type > Using HTML Tags as “Glue”” href=”http://www.danandsherree.com/2004/1003/using_html_tags_as_.php”>Using HTML Tags as “Glue”…

2

HI- I was psyched to find these instructions, but as a newbie to all things PHP, i am unclear as to what your words “The above breadcrumb example becomes:” is referring to. I assumed you meant to edit the new phpitems.inc template file, thus replacing:

<?php

$MTEntryCategoriesGlue = ‘,<br />’;
$MTParentCategoriesGlue = ’ > ‘;

?>

with the longer chunk of code. However, when i rebuilt my test site, I got this:

“Build error in template ‘Breadcrumbs’: Error in tag: You used an ‘MTEntryCategories’ tag outside of the context of an entry; perhaps you mistakenly placed it outside of an ‘MTEntries’ container? “

Help, please?

TIA!
Dida

3

Dida,
The code
<?php
$MTEntryCategoriesGlue = ‘,<br />’;
$MTParentCategoriesGlue = ’ > ‘;
?>
goes into the new file, phpitems.inc.

In your existing templates goes the include statement and the breadcrumb code. I made some edits to the entry to try to make it a little clearer for everyone.

4

Thanks Dan!

Post a comment

(If you haven't left a comment here before, you may need to be approved by the site owner before your comment will appear. Until then, it won't appear on the entry. Thanks for waiting.)

 
 
 


TrackBack URL for Using HTML Tags as “Glue”:
http://www.eatdrinksleepmovabletype.com/cgi-bin/mt/mt-tb.cgi/180