difficulty with image tag inside of a block

jgulisano - May 8, 2006 - 16:09

Trying to get an image tag to work within a block. I'm using the $base_path function so as to not hard code the path to the image. The images are in a sub-directory off of the "themes" directory. Seems like it should be straight forward but I'm having a hard time getting it to work. The following code is in the block's body, and the input format is set to php.

<?php print
"  <img src=\" " . $base_path . $directory . "/images/about_0.gif\" alt=\"About Us\" name=\"about\" width=\"71\" height=\"19\" border=\"0\"> "
?>

I've also tried, as "full html":

<img src="<?php print $base_path . $directory ?>/images/spacer.gif" width="1" height="19" border="0">

The problem is that the $base_path and $directory evaluate to blank or null. The second example works fine in my template, but not in the block. Since I am new to Drupal, I assume I'm missing something so I'd appreciate any direction.

Thanks.
Jose.

base_path not available to custom block

chromeyellow - May 8, 2006 - 16:47

I had the very same problem and tried the same solutions. The problem (as you've noticed) is that the $bath_path and $directory variables are not available inside blocks, or at least in custom blocks like yours. To fix it:
1) make sure that the input format for the block is set to 'php'
2) put this line at the top of your block: <?php $base_path = base_path(); $directory = path_to_theme(); ?>. This brings the variables back into the block.
3) now you can call the image in the block as follows: <img src="<?php print $base_path . $directory ?>/images/spacer.gif" width="1" height="19" border="0"> - in other words, now it will work as you quite rightly thought it would.

Update: a bit more testing confirms that you can bring $base_path into the block simply by calling the global. So instead of $base_path = base_path() you can enter <?php global $base_path; $directory = path_to_theme(); ?>.

base_path not available to custom block

jgulisano - May 8, 2006 - 16:55

Opted for your first suggestion, although they both worked perfectly. Thank you very much!

 
 

Drupal is a registered trademark of Dries Buytaert.