0

I'm trying to set a stash variable in a custom module I'm writing with the following code:

$params = array(
    'name'  => 'st_test_variable',
    'scope' => 'user',
    'type'  => 'snippet'
);
$value = "Test test";
Stash::set($params, $value);

However when I try and retrieve this in a template I get nothing. The code I'm using is as follows:

{exp:stash:get name="st_test_variable"}

Should this be possible to do?

Bit of additional info: I'm using Stash 2.5.7 with EE 2.5.2 (not able to upgrade right now!).

Cheers

Jim

1 Answer 1

2

It should be possible, since that's exactly what I'm doing on my project.

Here's my code (which is working):

in my __construct()

$this->EE  = get_instance();

    if ( ! class_exists('Stash'))
    {
        include_once PATH_THIRD . 'stash/mod.stash.php';
    }

Then, lower down in my function, I have a function

function set_stash($name,$d)
{
    $params = array(
        'name' => $name,
        'scope' => 'user'
    );

    Stash::set($params,$d);
}

I'm thinking it's something to do with the fact you're setting it as a snippet - I'm sure, if a stash variable is a snippet, you access it via {name_of_snippet}, in your case {st_test_variable}.

Try dropping that from the variables and seeing if it works.

I'm also assuming that your add-on is executed before the stash variable tries to grab it?

4
  • That's it! I could kiss you, Crawley - well done mate. Where I was going wrong was I wasn't putting the include_once bit in the my__construct() area. I had it in one of my methods. Sep 4, 2014 at 11:30
  • 1
    Putting the include_once for the Stash lib in your constructor would work fine, what was wrong was setting the variable type to snippet but trying to access it as a native Stash variable. Either {exp:stash:get name="st_test_variable" type="snippet"} or {st_test_variable} would work (depending on parse order relative to your plugin). Sep 4, 2014 at 16:02
  • @madebyhippo I've added 'save' => 'yes' to the $params because even though I'm seeing the data in the db, I'm not able to show the stash vars on subsequent page loads. Even with just the stash get in the template. Any ideas? Developing locally using Mamp. Sep 5, 2014 at 8:56
  • Fixed! Seems my problem was down to me having the value of stash_default_scope set to 'local' instead of 'user'. Sep 5, 2014 at 10:12

Your Answer

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

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