MyBB Community Forums

Full Version: eval() in postbit?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi guys,

I've tried using {$foo} in my postbit template which doesn't seem to work. I've tried replacing the hook for index_start which appears to work on the index page, but I can't get it to work on the postbit. Any workaround for this? Toungue

Hook:
$plugins->add_hook('postbit','some_Function');

Function:
function some_Function()
{
    global $mybb, $templates, $foo;

    $bar = 'Bar';

        eval("\$foo = \"".$bar."\";");
}

Cheers guys Smile
This ?

function some_Function(&$post)
{
    global $mybb, $templates, $foo;

    $bar = 'Bar';

        eval("\$foo = \"".$bar."\";");
} 
For postbit functions you need to declare function like this and also return it:

function some_Function(&$post)
{
    global $mybb;

        $post['your_var'] = "some value";

return $post;
}

And in postbit templates you should have this to evaluate it's value:

{$post['your_var']}
You dont need to return it also.
I think it's a good practice, not sure but I always return it.
You don't have to return it as prefixing the parameter with & means you are passing the variable by reference rather than by value.

Anyway, here's what I'd use:

$plugins->add_hook('postbit', 'someFunction');
function someFunction(&$post)
{
    global $templates;

    // Wanting to use a template?
    eval("\$post['some_var'] = \"".$templates->get('some_template')"\";");

    // Just wanting to create a standard variable?
    $post['some_other_var'] = 'Foo';
}
Thanks all, I've got it working. I always thought I had to use eval(), didn't think to use $post['var']; Toungue

Thanks again Smile
You forget to pass $post by reference as @Frank.Barry mentioned (and you don't really need to return it, that is the full point of passing by reference, isn't it?).

If you want to use this:
function some_Function(&$post)
{
    global $mybb, $templates, $foo;

    $bar = 'Bar';

        eval("\$foo = \"".$bar."\";");
}

Then you will need to use {$GLOBALS['foo']} in your postbit template.

For something like what @Crazy4cs mentioned.
function some_Function(&$post)
{
    global $mybb, $templates;

    $bar = 'Bar';

        eval("\$post['foo'] = \"".$bar."\";");
}

Then you will need to use {$post['foo']} in your postbit template.

/ Nevermind, and yes, you don't need to use eval() unless it is a template or similar.