Jump to content

$category and $subcategory in PHP vs Smarty


Recommended Posts

Hi all. I'm having a little trouble figuring out an aspect of blocktopmenu and it's pulling me back in a template I'm developing.

 

Recently I had a problem that, with the precious help of Vekia, I managed to solve. From that moment I could understand a bit more of how the functions work. I'm no PHP pro and sometimes I don't get some stuff, but doing some testing I understand what the code output will be and where it will be in the site.

 

So my question is, why some functions like

$link->getCatImageLink($category->link_rewrite, $category->id_image, 'medium_default');

work perfectly in php, and

{$link->getCatImageLink($subcategory.link_rewrite, $subcategory.id_image, 'medium_default')|escape:'html'}

also works in Smarty, but when I tried to use the same variables in PHP, like this

$link->getCatImageLink($subcategory.link_rewrite, $subcategory.id_image, 'medium_default');

or like this

$link->getCatImageLink($subcategory->link_rewrite, $subcategory->id_image, 'medium_default');

they do not work.

 

Aren't the variables used in the tpl files the same as in the PHP files?

 

Thanks in advance and sorry if I can't express myself more clearly. Arrays, functions, variables, operators, etcetera etcetera, are concepts that still aren't very clear to me :D

Link to comment
Share on other sites

Hi.

$link->getCatImageLink($subcategory.link_rewrite, $subcategory.id_image, 'medium_default');

In PHP to reach an element of an associative array you use the following syntax: $my_array['index'], so the above code would be $subcategory['link_rewrite']

$link->getCatImageLink($subcategory->link_rewrite, $subcategory->id_image, 'medium_default');

In the above example $subcategory has to be an object.

 

Regards.

Robin.

The CartExpert Team

Link to comment
Share on other sites

Thanks for the reply!

 

I tried your first example, $subcategory['link_rewrite'], but it didn't work. I guess I'll have to define the $subcategory object. Gonna try once I have the time.

 

Thanks again, it was very helpful.

Link to comment
Share on other sites

Hi bro,

 

It's because the two languages use different syntaxes:

 

In PHP :

$link->getCatImageLink($category->link_rewrite, $category->id_image, 'medium_default');

 

the "->" is used in order to acces Object Data, because the data type of $category is Object.

You could have used  $category['link_rewrite'] if $category would have been an array.

In Smarty the syntax differs a little and the PHP "->" is replaced by "." .

 

Here's a little documentation on the difference between Arrays and Objects:

Arrays: http://bit.ly/J0PZOQ

Objects: http://bit.ly/J0Qbhc

 

Hope it cleared things a little.

 

Nick

Link to comment
Share on other sites

Thank you Nick!

 

Actually I did notice that difference in the syntaxes. I was looking for a way to call only the subcategories images in the blocktopmenu but I couldn't find code snippets with "$subcategory" in any of the PHP files. So after looking at the html of the category page I ended up finding the category.tpl file that has the $subcategory object and the getCatImageLink function

<img src="{$link->getCatImageLink($subcategory.link_rewrite, $subcategory.id_image, 'medium_default')|escape:'html'}" alt="" width="{$mediumSize.width}" height="{$mediumSize.height}" />

and then I tried to change the syntax to something more suited to PHP.

 

When something doesn't work I always feel like I'm kinda lost, but since there was no errors I knew that the syntax was probably right but something else was missing.

Thanks for the links, I'm gonna give it a read as soon as I can!

Link to comment
Share on other sites

try  adding something like this in the .tpl file

<img src="{$link->getCatImageLink($subcategory.link_rewrite, $subcategory.id_image, 'subcat')}" alt=""  />

in this string it refers to imagetype named 'subcat',

so if you want to use it without modifying it you'll have to create this image  type (backoffice-preference-images...)

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...