Multilevel-tags in sidebar - Smooth Sailing

Aug 24, 2007 14:27

two alternative ways,

Both versions overwrite the function Page::lay_print_sidebar_tags()
I think the first version is the better one, so if you can you should use this one:




function Page::lay_print_sidebar_tags() {
# Specify your delimiter. One character only -- extra chars get truncated.
var string delimiter = ":";

# Do you want to show the tag use counts?
var bool show_count = true;

# Specify the text to show just before the use count, if any
var string pre_count = "(";

# Specify the text to show just after the use count, if any
var string post_count = ")";

### Don't change below unless you know what you are doing!

var Page p = get_page();
var string list = "";

if (size $p->visible_tag_list() > 0) {
if ($delimiter->length() > 1) {
$delimiter = $delimiter->substr(0, 1);
}

var string[] closing_html;
var string[] prev_tags;
var int tag_list_pos = 0;
var string tier_code = "";
$closing_html[0] = "";
$prev_tags[0] = "";
foreach var TagDetail t ($p->visible_tag_list()) {
var string[] tags;

if ($t.name) {
var int array_counter = 0;
var string buffer = "";
foreach var string char ($t.name) {
if($char == $delimiter) {
$tags[$array_counter] = $buffer;
$array_counter = $array_counter + 1;
$buffer = "";
}
else {
$buffer = $buffer + $char;
}
}
$tags[$array_counter] = $buffer;

var int pos = 0;
foreach var string tier($tags) {
if (size $closing_html <= $pos) {
$closing_html[$pos] = "";
}

if (size $prev_tags <= $pos) {
$prev_tags[$pos] = "";
}

if (size $tags == ($pos + 1)) {
$tier_code = """$tier""";
if ($show_count) {
$tier_code = $tier_code + """ ${pre_count}${t.use_count}${post_count}""";
}
}
else {
$tier_code = """$tier""";
}

if ($prev_tags[$pos] == "") {
$list = $list + """
  • $tier_code""";
    $closing_html[$pos] = "
";
}
elseif ($tags[$pos] != $prev_tags[$pos]) {
var int i = 0;
foreach var string html ($closing_html) {
if ($i > $pos) {
$list = $list + $closing_html[$i];
$closing_html[$i] = "";
}
$i++;
}

if ($closing_html[$pos] == "") {
$list = $list + """
  • $tier_code""";
    $closing_html[$pos] = "
";
}
else {
$list = $list + """
  • $tier_code""";
    }
    }
    else {
    }
    $pos++;
    }
    $prev_tags = $tags;
    }
    $tag_list_pos++;
    }

    var int i = 0;
    var string remaining_html = "";
    foreach var string html ($closing_html) {
    if ($html != "") {
    $remaining_html = $html + $remaining_html;
    $closing_html[$i] = "";
    }
    $i++;
    }
    $list = $list + $remaining_html;
    }

    if ($list != ""){
    $this->lay_print_sidebox_top("Navigation");
    print $list;
    $this->lay_print_sidebox_bottom();
    }
    }



    function Page::lay_print_sidebar_tags() {
    var Tag[] pagetags = $this->visible_tag_list();

    ##### Config #####

    # Specify your delimiter! One char only -- extra chars get truncated.
    # Making the delimiter an empty string will result in an un-tiered list,
    # which may be what you prefer, but this code is serious overkill for
    # that purpose.
    var string delimiter = ":";
    var bool show_count = true;
    var string pre_count = "(";
    var string post_count = ")";

    # Specify the title of your tag box!
    var string tag_title = "Navigation";

    ##### End Config #####

    var Page p = get_page();
    var string list = "";

    # mt:20050627
    # Replaced erroneous return code with if statement (otherwise
    # entire sidebar not printed at all if no visible tags!).
    if (size $p->visible_tag_list() > 0)
    {
    # mt:20050624
    # Can't use delimiter longer than one char, so truncate if necessary.
    if ($delimiter->length() > 1)
    {
    $delimiter = $delimiter->substr(0, 1);
    }

    var bool list_started = false;
    var string list_item = "";
    var string[] prev_tags = ["", ""];

    # mt:20050624: Start the list.
    $list = """
      """;

      foreach var TagDetail t ($p->visible_tag_list())
      {
      var string[] tags;

      if ($t.name)
      {
      # mt:20050624
      # Split tags into a 1- or 2-element array on delimiter. Oh god, my
      # kingdom for a function. Adapted from lj-user rane500's explode
      # function to only care about first instance of the delimiter.
      var int array_counter = 0;
      var string buffer = "";
      var bool found_delimiter = false;
      foreach var string char ($t.name)
      {
      if (($found_delimiter == false) and ($char == $delimiter))
      {
      $found_delimiter = true;
      $tags[$array_counter] = $buffer;
      $array_counter = $array_counter + 1;
      $buffer = "";
      }
      else
      {
      $buffer = $buffer + $char;
      }
      }
      $tags[$array_counter] = $buffer;

      # mt:20050624: Now examine the tags array to determine how to display the tag.
      if (size $tags == 1)
      {
      # mt:20050624: This tag has no subtag.

      if ($list_started)
      {
      # mt:20050624: Previous tag had a subtag, so must close its outstanding list.
      $list = $list + """
    """;
    $list_started = false;
    }
    if ($prev_tags[0] != "")
    {
    # mt:20050624: This is not the very first tag in the list, so close off previous tag.
    $list = $list + """
  • """;
    }
    # mt:20050624: Now add the new tag.
    $list = $list + """
  • $tags[0] ($t.use_count)""";
    }
    else
    {
    # mt:20050624: This tag has a subtag.

    $list_item = """
  • $tags[1] ($t.use_count)
  • """;

    if (($tags[0] == $prev_tags[0]) and ($list_started))
    {
    # mt:20050624
    # This tag fits under the previous tag's tier, and it is not the first item in that tier.
    $list = $list + $list_item;
    }
    elseif (($tags[0] == $prev_tags[0]) and ($list_started == false))
    {
    # mt:20050624
    # This tag fits under the previous tag's tier, and it is the very first item in that tier.
    $list = $list + """
      """ + $list_item;
      $list_started = true;
      }
      elseif (($tags[0] != $prev_tags[0]) and ($list_started))
      {
      # mt:20050624: This tag initializes a new tier and must also close off the previous tag's list.
      # $list_started retains its true state.
      $list = $list + """
  • $tags[0]
      """ + $list_item;
      }
      elseif (($tags[0] != $prev_tags[0]) and ($list_started == false))
      {
      # mt:20050624: This tag initializes a new tier but does not have to close off a list.

      if ($prev_tags[0] != "")
      {
      # mt:20050624: This is not the very first tag in the list, so close off previous tag.
      $list = $list + """""";
      }

      # mt:20050624: Now add the new tag.
      $list = $list + """
    • $tags[0]
        """ + $list_item;
        $list_started = true;
        }
        }
        $prev_tags = $tags;
        }
        # mt:20050623: Next tag in the list!
        }

        # mt:20050624: Close any outstanding lists.
        if ($list_started)
        {
        $list = $list + """
      """;
      }
      $list = $list + """
    """;

    # mt:20050625: Enclose the entire list in an li tag as required by the layout.
    # $list = """
  • """ + $list + """
  • """;

    # mt:20050623: Add styling to box title.
    # $tag_title = """
  • $tag_title
  • """;
    }

    ##### Specify Box Order #####

    # mt:20050627
    # Okay, now that's done, just print out all your sidebar boxes in the order you
    # want, including a call for this one -- print_sidebar_box($tag_title, $list) --
    # nested within an if statement to prevent tags box from printing in the event
    # of no visible tags.
    # print_userpic();
    # print_sidebar_blurb();
    # if ($list != ""){print_sidebar_box($tag_title, $list);}
    # print_sidebar_linklist();
    # print_sidebar_calendar();

    ##### End Box Order #####

    if ($list != ""){
    $this->lay_print_sidebox_top($tag_title);
    print $list;
    $this->lay_print_sidebox_bottom();
    }
    }

    smooth sailing

    Previous post
    Up