Jump to content

Create new category instance inside template - prestashop 1.7


Recommended Posts

I know i can use category class methods inside .tpl template files like this:

{assign var='all_categories' value=Category::getCategories()}


But how can i actually initialize Category object inside template? So that __construct function runs.

I ask this because when i try to use some Category class functions, i get this error:

Using $this when not in object context

Link to comment
Share on other sites

  • 3 weeks later...

There is no way to instance a category through a tpl file, some classes have a public static method to do this, eg, like the Db class, this have one called getInstance, unfortunately by default doesn't exist nothing similar in the Category class. You should instance it in a php file and send to Smarty, or modify the class adding an object:

public static $instance = array();

And the method:

public static function getInstance($id_category)
{
    if (isset(self::$instance[$id_category])) {
        return self::$instance[$id_category];
    }

    return self::$instance[$id_category] = new Category($id_category);
}

Now you can use in your tpl:

{assign var='category' value=Category::getInstance(3)}

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...