tag:blogger.com,1999:blog-5941325.post-89387208417489605282007-07-02T20:10:00.000+01:002007-07-04T21:51:09.820+01:00Blogger, PHP, and LabelsI'm in the process of updating my Blogger template so that my new <a href="http://help.blogger.com/bin/topic.py?topic=10275">Blogger labels</a> and their corresponding label pages have a reasonable user interface and are actually useful. My goals are to: <ul><li>Make it clear when a label page is being displayed by including the label name in the page title and in any relevant headers & sidebar sections.
</li><li>On the page for a label named <i>label</i>, display <a href="http://del.icio.us/Deflexion.com">my bookmarks</a> that are tagged with <i>label</i> on del.icio.us (and, of course, also display my blog items that are labeled with <i>label</i>).
</li></ul>The first step is to use PHP to get the label name into a variable, which I call $label. Here's how I do this: <pre><a href="http://help.blogger.com/bin/answer.py?answer=42059&topic=8930"><MainPage></a>
<?php
<i> </i> $label = <a href="http://php.net/manual/en/function.strtoupper.php">strtoupper</a>(<a href="http://php.net/manual/en/function.basename.php">basename</a>(<a href="http://php.net/reserved.variables">$_SERVER['PHP_SELF']</a>));
<i> </i> if ($label == 'INDEX') {
<i> </i> <i> </i> $label = <a href="http://php.net/manual/en/language.types.null.php">NULL</a>;
<i> </i> }
<i> </i> // Note: This assumes no label will be named index
?>
</MainPage>
</pre>This sets the $label variable to the upper-cased label name if a label page is being displayed and to NULL if the top page (deflexiοn.com/index) is being displayed. Determining the label name is as simple as determining the page's filename (basename) because Blogger names each label page label.extension and <a href="http://deflexion.com/2005/11/blogger-permalinks-seem-permanent-but">I've set up my blog to use extensionless URLs</a>.
Once the variable $label is set, I use code such as the following to unconditionally process statements:
<pre><?php
<i> </i> echo "$label Deflexions (bookmarks)";
?></pre>Note that this does the right thing on label pages and on the the top page (index). For example, on a label page this resolves to: <pre>LABEL Deflexions (bookmarks)</pre> And on the top page, this resolves to: <pre>Deflexions (bookmarks)</pre> To conditionally process statements, I use code like the following:
<pre><?php
<i> </i> if ($label) {
<i> </i> <i> </i> echo "↓ deflexions & reflexions labeled $label";
<i> </i> }
?></pre>Note that on the top page $label is <a href="http://php.net/manual/en/language.types.boolean.php">NULL, which is considered FALSE</a>, and statements inside the squiggly brackets will not be run.
I'm still updating my template so things are kind of a mess at the moment. I'm posting this now because I'd love to get feedback on my PHP code and my template. Please post your thoughts and tips as a comment to this blog item - thank you!NMnoreply@blogger.com