1

There is a block which its body is produced by PHP Code. I need to set a variable before loading the block and use it in it. This is my code

function THEME_preprocess_block(&$variables){
    $variables['var']="some text";
}

when I open the block configuration page and in its body I write

<?php print $var;?>

it prints nothing. How do I can pass variable from block template page in to the block body in the block configuration page?

1
  • 2
    If you're using PHP filter to put code in the body, it is (thankfully) executed in isolation, not in the same scope as the template file. Variables are only available in the context of a template file's execution, so you'll need to find a different way to solve this
    – Clive
    May 18, 2015 at 14:32

1 Answer 1

2

Your variable will be available in block.tpl.php. Consider making your own block like described on How to theme a custom block?. (You probably do not need hook_theme().)

Using PHP directly in blocks (via UI) is not the best practice: It slows your site down, as the block needs to be rendered with every request, when it comes to caching.

2
  • The block contains lots of html, css classes and php code. you mean I should implement it in .tpl file?
    – M a m a D
    May 18, 2015 at 14:34
  • no - i would suggest to make a custom block
    – rémy
    May 19, 2015 at 11:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.