Wrap First Word of a Block Subject in a <span> or <strong> Tag
I'm working on a theme implementation at the moment and our front end designer calls for a strong tag to be wrapped around only the first word of the subject for every block in our sidebar. I thought I would share this problem and the solution I am using.
I created a function called strongReplace in my template.php file. I called it from the subject line within the block.tpl.php file like this:
print strongReplace($block->subject)
The code and documentation for my strongReplace function is here:
<?php
function strongReplace($subject){ // receives $block->subject as the local variable $subject
$subject = "<strong>" . $subject; // prepends a <strong> tag before the $block->subject
$space_location = strpos($subject," "); // finds the location of the first space, if there is one
if($space_location){ // if there is a space (a second word exists in the subject)
$subject = substr($subject, 0, $space_location) . "</strong>" . substr($subject, $space_location);
} // take the location between first position and space and contcatenate
// both a closing </strong> tag and the rest of the original subject
else{
$subject .= "</strong>"; // if there's not a space (no second word) just close the </strong> tag
}
return $subject; // return the correct $block->subject string to the browser
}
?>It's a pretty simple problem but I'm pretty rusty with my programming skills and I figured this sort of thing might come up and it would help someone else.. This can easily be modified for things like a different tag or different character in the string, et cetera.
-=- christopher

=-=
the acquia slate theme does this you may want to look into how they do it. They wrap a spam called first.word and it increases font weight and changes the color.
our designer has some really
our designer has some really elegant code that uses xhtml elements and as few divs, spans, classes, and id's as possible to make our design, which has turned out really well. Using the strong element in our case serves to add semantic meaning to the first word, which tends to be (but not always is) the most important word in the block's subject, but definitely should be in our design. So in our case this method works best for our needs without the need for an additional class.
-=- christopher
=-=
Ah I see, this should actually be placed in the documentation area rather than in the forums where it will essentially disappear into the ether.
okay sure... Would you
okay sure... Would you recommend a place for me to put it and I will put there :)
-=- christopher
=-=
theme developers guide, in the snippets area somewhere.
...
Well to be fair the wordlimit function and the way its used in Acquia slate is a tad more powerful than the code above - it takes three params, $string, $length and $ellipsis, so in block.tpl.php its called with $block->subject, 1, ""; and the code in block template allows you to add whatever html you want, span, strong, em, its easily editable.
I can also do this in 3 lines of jQuery when I want it purely for design - http://3rdworldthemes.org/snippets/wrap-block-title-first-word-span-tags
Professional Drupal Design and Theme Services
so are you saying use the
so are you saying use the Acquia theme or take our parts of its functionality and appropriate it as you need? Hacking/subtheming another theme is just not a good solution for most of the stuff I'm involved in.. We do our own theme to precisely implement the design, and just as importantly, the semantic code, which a jQuery solution isn't going to help with at all, unfortunately. At least this is stand-alone code that will work as-is without having to take apart someone else's work to figure out what ya need. anyways... worked for me, hope it helps someone.
-=- christopher
...
Cool, do what work for you, however my point was that the wordlimit function is an elegant and more flexible approach, because it does not have to be the first word that gets wrapped, it can be any word in a string of any length. To be frank when I really want to get serious with the code I simply code every block by hand, each one is individually tailored to its context and content.
My apologies if it sounded like I was telling you to do something etc, I was merely leaving additional options for other readers.
Professional Drupal Design and Theme Services