Author |
Message |
spasticdonkey
RavenNuke(tm) Development Team
![](modules/Forums/images/avatars/48fb116845dfecf66294c.gif)
Joined: Dec 02, 2006
Posts: 1693
Location: Texas, USA
|
Posted:
Sun Jan 23, 2011 5:47 am |
|
Working on a new theme and looking to convert the block title to a CSS ID.. This is working but I thought I would throw it out there if anyone has ideas for improvements; or a situation where it might fail to produce a valid id.
Code:function TitleToID($title) {
if (!empty($title)){
$BlockID = trim(str_replace(array('"', '&', '?', '.', '\''), '', $title));
$BlockID = strtolower($BlockID);
$BlockID = preg_replace('/[^a-z0-9_\-\.]/i', '-', $BlockID);
$BlockID = preg_replace('/[-]{2,}/', '-', $BlockID);
$BlockID = trim($BlockID, '-');
return $BlockID;
}else{
static $notitle;
if (isset($notitle)){$notitle++;}else{$notitle=1;}
$BlockID = 'notitle'.$notitle;
return $BlockID;
}
}
|
basically what this does is if the title is not empty, it strips out some unwanted characters, converts to lowercase, replaces invalid characters with hyphens, replaces multiple hyphens with a single hyphen, and trims hyphens from beginning and end of the string. If the title is empty it will generate notitle1, notitle2, etc; to ensure even untitled blocks receive a unique id. |
|
|
|
![](themes/RavenIce/forums/images/spacer.gif) |
montego
Site Admin
![](modules/Forums/images/avatars/0c0adf824792d6d341ef4.gif)
Joined: Aug 29, 2004
Posts: 9457
Location: Arizona
|
Posted:
Sat Jan 29, 2011 9:22 pm |
|
I am actually curious as to why you would want a unique id on each block? If you are doing this for a jQuery implementation you may still be able to accomplish what you are after by using class names instead, even if you never reference them in your CSS. |
_________________ Only registered users can see links on this board! Get registered or login!
Only registered users can see links on this board! Get registered or login! |
|
|
![](themes/RavenIce/forums/images/spacer.gif) |
spasticdonkey
![](modules/Forums/images/avatars/gallery/blank.gif)
|
Posted:
Sat Jan 29, 2011 10:50 pm |
|
Yes this would be for jQuery or styling targeted toward a specific block.. and I agree most times a class would do the job. Working with a few features that require a specific ID, like a block that does not display if JS is disabled, or hiding a block that can then be opened in a modal window inline, or a bottom-side block that has the cool "fixed-floating" effect: http://static.jqueryfordesigners.com/demo/fixedfloat.html
I can probably think of more examples, but that's as far as I've been able to tweak thus far ![Smile](modules/Forums/images/smiles/icon_smile.gif) |
|
|
|
![](themes/RavenIce/forums/images/spacer.gif) |
montego
![](modules/Forums/images/avatars/gallery/blank.gif)
|
Posted:
Tue Feb 01, 2011 8:37 pm |
|
Ok, so you definitely need control over the entire block and not just the content. Makes sense.
I suppose you could do it this way. You could also used the "bid" field in the table as you know with 100% certainty that it is unique, just need a consistent suffix in front that starts with a letter instead of a number, like "block" followed by the ID, so like "block5".
My thinking here is that you can then change the block title to anything you want at any point in time and the CSS reference wouldn't change. It is always unique. Even if you use the titles, if you want to change a title, you have to tweak your CSS, so not much added value in the descriptive id names. In addition, if you end up having some kind of configuration page, you'll be reading the nuke_blocks table anyways to get both bid and title.
Just some thoughts since you asked. Of course, in the end, you need to do what makes the most sense to you. |
|
|
|
![](themes/RavenIce/forums/images/spacer.gif) |
spasticdonkey
![](modules/Forums/images/avatars/gallery/blank.gif)
|
Posted:
Tue Feb 01, 2011 9:05 pm |
|
montego wrote: | Ok, so you definitely need control over the entire block and not just the content. Makes sense.
I suppose you could do it this way. You could also used the "bid" field in the table as you know with 100% certainty that it is unique, just need a consistent suffix in front that starts with a letter instead of a number, like "block" followed by the ID, so like "block5".
My thinking here is that you can then change the block title to anything you want at any point in time and the CSS reference wouldn't change. It is always unique. Even if you use the titles, if you want to change a title, you have to tweak your CSS, so not much added value in the descriptive id names. In addition, if you end up having some kind of configuration page, you'll be reading the nuke_blocks table anyways to get both bid and title.
Just some thoughts since you asked. Of course, in the end, you need to do what makes the most sense to you. |
thanks for the input
I had similar thoughts originally, as building the ID from the title is not the optimum solution. But I was pretty much working with what was available in the default themesidebox($title, $content) function. It didn't seem very efficient to query the blocks table every time that function is called, so I figured I would deal with the title. I also learned that some modules call themesidebox so in those cases there would be no block id.
In a perfect world I suppose it would be
themesidebox($title, $content, $id)
but whatcha gonna do? ![Smile](modules/Forums/images/smiles/icon_smile.gif) |
|
|
|
![](themes/RavenIce/forums/images/spacer.gif) |
montego
![](modules/Forums/images/avatars/gallery/blank.gif)
|
Posted:
Wed Feb 02, 2011 4:15 am |
|
There you go again confusing the issue with facts. Didn't even realize that. So there you go, what he said... |
|
|
|
![](themes/RavenIce/forums/images/spacer.gif) |
|