I created a new block that I wish to be displayed on every page. I would like to style this block differently then all other blocks. I have created a new template and named it block-raq.tpl.php (raq being the custom part). How do I how tell the new block to use this template?

Comments

nevets’s picture

You want to use block-{module}.tpl.php or block-{module}-{delta}.tpl.php where you replace {module} with the actual module name and {delta} with the actual delta value. Use the form block-{module}.tpl.php to theme all blocks from a given module, use block-{module}-{delta}.tpl.php to theme a particular block from a module. Note you can also use block-{region}.tpl.php to apply common look to all blocks in a particular region.

For example the login block comes from the user module and has a delta of '0' so you could create a block-user-0.tpl.php file to theme just that block.

Johnboat’s picture

I want to do the same thing, but I'm a little confused with your explanation. Any chance you can clear this up a bit?

nevets’s picture

Visit "Administer" -> "Site buidling" -> "Blocks". Click on 'configure' for the 'Navigation' block, the path (last part of URL) will read 'dmin/build/block/configure/user/1', where 'user' is the module, '1' is the delta. In general the path is dmin/build/block/configure/{module}/{delta}. So continueing with the 'Navigation' block by default it is themed with 'block.tpl.php'. If you want to theme all blocks from the user module the same (but different from other blocks) you could create a block-user.tpl.php and that fill would be used for all block from the user module. To theme the 'Navigation' block uniquely we would use block-user-1.tpl.php. The order the system looks for them is block-user-1.tpl.php, block-user.tpl.php, block.tpl.php.

So you can find the module and delta for any block you want to theme differently by click the 'configure' link for that block and looking at the last two parts of the path and using them to construct the tpl.php file.

stephtek’s picture

Thanks, i got it working now!

fnikola’s picture

Does anyone know how to get the $delta for a block that is created from o view?

nevets’s picture

You can always look at the generated html for the block (the firebug extenstion for firefox is nice for this). The outer div will have an id like block-{module}-{delta}, so I would expect the views module to produce blocks with ids like block-views-{delta} (ex: block-views-1, 1 would be the delta).

You can also configure the block, the path for that page will have move the module and delta at the end.

fnikola’s picture

Thanks for the direction and yes you are correct as the outer div does have an id that contains the information needed to call these "view" blocks using PHP code from inside any page. Please note that these blocks that are created from views are a little different as they do not really have a $delta number, but the view name instead. For example, instead of the usual block-{module}-{delta} format it reads block-views-{view name} and the {view name} is not a number, but the actual view name.

nevets’s picture

One thing alot of people do not reliize is that delta does not have to be a number but can be a string.

asb’s picture

Hi,

so far I do understand the differences between normal blocks and blocks created by views. However I still don't see how I can access those blocks created by a view in a node template. Example for the "Backlinks" module, which provides a block; the content's of this block I'd like to access from a node template.

The generated HTML of the block looks like follows (D5.7):

<div class="block block-views" id="block-views-backlinks_block">
<div class="title1"><div class="title2">
<h2>Backlinks</h2>
</div></div>
<div class="content clear-block"><div class="view view-backlinks-block"><div class="view-content view-content-backlinks-block"><div class="item-list"><ul><li><div class="view-item view-item-backlinks-block"><div class="view-field view-data-node-title">...</div></div>
</li></ul></div></div></div>
</div>
</div>

Now, I'd like to embed this output into a node template, e.g.:

<?php
$block = module_invoke('backlinks_block', 'block', 'view', block-views-backlinks_block);
  if ($block['content']) {
    $output = "<div class=\"front-page-block\">\n";
    $output .= "<div id=\"front-block-title\"><h2>".$block['subject']."</h2></div>\n";
    $output .= "<div class=\"content\">".$block['content']."</div>\n";
    $output .= "</div>\n";
    print $output;
  } 
?>

This does not work at all, most probable because the $delta value is bogus; the following values do not work:

* block-views-backlinks_block
* block-views
* views-backlinks_block
* backlinks_block

So how is the backlinks_block provided by the "backlinks" view correctly invoked??

Thanks & greetings,
-asb

nevets’s picture

I think you want to call module_invoke like this

$block = module_invoke('view', 'block', 'view', 'backlinks_block');
asb’s picture

Hi nevets,

I immediately tried this, but it doesn't work (nothing is diesplayed below the node). Maybe something else is wrong with the code snippet,

Thank you anyway,
-asb

stephtek’s picture

I am also a little confused. I created a new block, so is the module name the block description, or where do I define the module name?

nevets’s picture

If you create the block with a custom module the module name would be whatever you gave the module.

If you create a block from the block administrative page the module name will be 'block'.

The delta is also determined by the module and for certain type blocks will vary with the setup.

Johnboat’s picture

I figured out a different way. When you create a new block it will create a id="block-block-X", the X being whatever number the block is. I simply styled that Id. So in my case I styled id="block-block-4". Is this a safe way of doing it?

nevets’s picture

Works fine, X is the delta in this case 4

elpino’s picture

You can create a block.tpl.php file in your theme folder (this will apply to every block regardless of position, module, delta, etc.) as described here http://drupal.org/node/60538 and you'll be able to use $block->subject instead of the default $block->delta.

This would be useful if you would like more meaningful or descriptive id attributes instead of relying on the system assigned delta. You could also use instead any of the available block variables listed here http://drupal.org/node/11813

The only thing is that a block description variable is not available (used in the block admin page list), this would be useful to use instead of the subject when blocks don't have one assigned. I'd be great if anyone reading this could find a way to access it.

Anyway, hope this helps.

-learning everyday

orthoducks’s picture

It would be helpful to have some insight into the rationale for this rule.

I want to create a block template file for a particular block. It seems to me that when developers create custom block templates, that will be the case most of the time.

But it seems to be inordinately hard. If I don't specify the delta, my template will apply to my block and any other block that just happens to have the same module name. That's a source of complexity, and I can't imagine how it could be useful. When I'm writing a block template, why would I even care what the module name is?

If I do specify the delta, I buy myself a permanent source of disruption when the delta changes. It's not clear to me whether a block's delta is invariant when the block is moved from one region to another, used in different themes, etc. At the very least, if I move block templates from one Drupal site to another, am I not going to have to figure out each block's delta for the new site and rename each template? And then do it again every time the templates are updated?

It seems to me that someone went to a great deal of trouble to create a scheme that's very hard to use, with no clear benefit. I hope that understanding why it was done this way will help me deal with it.

couloir007’s picture

I've created views that the block delta is the view name, and a random id. Question is, why? There seems to no rhyme or reason as to which is used. How did I create a view block that used the view name as the delta? Thanks!

neeshpal’s picture

subscribe