Same block, different color.

Permalink 1 user found helpful
Hi!
I've made a custom block with a small table.
I've put 6 blocks in one page, and I want every block having his own color.
The problem is that all blocks take the color of the last one.. I think it's because block's ids are the same, and the last block overwrites css rules of previous blocks.

I've done this workaround, but I'm curious if someone knows a better way to do it:
- I've added two variables in block's editor: 'TableId' and 'Color'.
- I give the id of the table with php:
<table id="<?=$TableId?>"> … </table>

- I set the color mixing php and css, like:
#<?=$TableId?> td{
   background-color:rgb(<?=$Color?>);
}

To make it works, I have to place this rule in view.php, while I prefer to keep all the rules in view.css file.
Is it possible? Thanks for any help!

 
JohntheFish replied on at Permalink Best Answer Reply
JohntheFish
You can generate a unique identifier from the block ID. You have to take special care if a block is copied or in page defaults.
See
http://www.concrete5.org/documentation/how-tos/developers/obtain-a-...

The code examples from the howto should be in the block controller.

I usually modify it to return from a function and use that to set() in the view() method. That way it is also available in on_page_view() and anywhere else I want it.

You will still need to create the css snippet in the view.php, or add it as a header item in the block controller on_page_view() method.
T30 replied on at Permalink Reply
Thanks John!
The method you provide save us the trouble of handle manually the different ID for each blocks.

I still dislike to use php for the IDs, but I think I'll have to accept it!