html attributes for blocks

html attributes for blocks

by Sebastien M. -
Number of replies: 3
Hi All,

How do we populate html attributes for side blocks.

I want to be able to give each of my blocks a personalized look.

Many thanks
Average of ratings: -
In reply to Sebastien M.

Re: html attributes for blocks

by Sebastien M. -
OK I figured it out!

I used the following steps:

(1) In class CourseBlock_MYBLOCK extends MoodleBlock of my blocks I specified a new html attribute like this:

this->sideblockmain = 'WHATEVERYOUWANT';

(2) In blocks/moddleblock.class.php, play with the "html attributes" function. I set it like this:

function html_attributes() {
return array('id' => 'block_'.$this->name(), 'sideblockmain' => $this->sideblockmain);

THEN, go into lib/weblib.php in function print_side_block_start you can call your new block attribute with this call:

if (isset($attributes['sideblockmain'])) {WHATEVER YOU WANT TO DO}


I'm a newbie. Maybe the experts can comment in this.

Cheers,

seb
In reply to Sebastien M.

Re: html attributes for blocks

by John Papaioannou -
Hi Sebastien,

Sorry to disillusion you, but that's not the way to do it... wide eyes I 'll explain:

The HTML attributes thing allows you to automatically assign just that: HTML attributes (such as id="blabla", align="center;", style="background-color: fuchsia-with-pink-stripes;") etc etc.

In fact anything you put in there will appear in your HTML (you can use view source from your browser to see it).

Now, you talk about "personalizing the look". This might involve two things:

a) Adding stuff to be displayed in a block. That's pretty straightforward, edit the block's get_content() method and get it to show whatever you like.

b) Making a block display differently (for example, giving it a colored border).

That's not evident, I have to admit, but if you know how it's very elegant. If you view source in the front page, for example, you 'll see something like

table style="width: 100%;" id="block_site_main_menu" class="sideblock"

just where your site's "main menu" starts. After this, and knowing some CSS, it's pretty evident. Go to your theme's styles.php and add:

#block_site_main_menu { border: 2px gold solid; }

and observe. smile It's really very easy to do whatever else you want this way.

Jon
Average of ratings: Useful (1)
In reply to John Papaioannou

Re: html attributes for blocks

by Sebastien M. -
Hi Jon:

Thank you for the advice but I don't understand everything. Doesn't seem to work for me.  The styles are not passed through the hole block (e.g. each cell of my list blocks) but limited to this weird style definition in the main table tag.

I did however add the following in my block definition:

function html_attributes()
{
return array('id' => 'block_'.$this->title.'', 'sideblockmain' => 'WHATEVER', 'sideblocklinks' => 'WHATEVER', AND SO ON);
    }

so don't need to modify the moodleblocks.class anymore. 

Finally, I did the necessary ajustments to weblib.php (print_side_block function) to use all my custom attributes through the $attributes array.

Cheers.

seb