Jump to content

[Solved] Cannot access smarty var


ThibaultWUN

Recommended Posts

I have a php file in which  I assign a var

$this->context->smarty->assign('test', 'test' );

Immediately after, I display my template.

return $this->display(__FILE__, 'template.tpl');

In my template, I try to access my var

<div style="display:none;">{$test|var_dump}</div>
<div style="display:none;">{$test}</div>

The first line gives me NULL. How is it possible ? What am I doing wrong ?

Edited by ThibaultWUN
solved (see edit history)
Link to comment
Share on other sites

sample 1:

$this->context->smarty->assign(array(
                'test' =>  'test',
		'test2' =>  'test2',
            ));

tpl:

{$test}<br />{$test2}

==================================================

sample 2:

$test_array = array('test 1', 'test 2');

$this->context->smarty->assign(array(
                'test' =>  $test_array,
            ));

tpl:

{foreach from=$test item=item}
  {$item}<br/>
{/foreach}

 

Edited by Guest (see edit history)
Link to comment
Share on other sites

Quote

sample 1:

$this->context->smarty->assign(array( 'test' => 'test', 'test2' => 'test2', ));

tpl:

{$test}<br />{$test2}

==================================================

sample 2:

$test_array = array('test 1', 'test 2'); $this->context->smarty->assign(array( 'test' => $test_array, ));

tpl:

{foreach from=$test item=item} {$item}<br/> {/foreach}

It did not work either... I was unable to display me page at all

Link to comment
Share on other sites

Quote

have you test {debug} in the file tpl ?

Unfortunately, the website was created without a long term vision and there is no test version, only the production version. I read that it is quite risky to use {debug} in a live site as it can really slow things down.

I am actually forced to create hidden div to see if I can get the data I need. A nightmare... Even more so as I am enable to get it !

Link to comment
Share on other sites

19 hours ago, ThibaultWUN said:

I have a php file in which  I assign a var


$this->context->smarty->assign('test', 'test' );

Immediately after, I display my template.


return $this->display(__FILE__, 'template.tpl');

In my template, I try to access my var


<div style="display:none;">{$test|var_dump}</div>
<div style="display:none;">{$test}</div>

The first line gives me NULL. How is it possible ? What am I doing wrong ?

Please try with below command for assigning var in php file:

Context::getContext()->smarty->assign('test','test');

Try with this and let me know if you get success.

  • Like 1
Link to comment
Share on other sites

Quote

Please try with below command for assigning var in php file:

Context::getContext()->smarty->assign('test','test');

Try with this and let me know if you get success.

It does not work... Perhaps I should have said it earlier but I am in a module inside a hook function...

My main goal here is actually to be able to show info about users groups. I need to know i which group my user is and to retrieve id's of users belonging to a group. If this changes the problem...

Link to comment
Share on other sites

The problem will be elsewhere.
Everything is working.
Without your codes, we can only guess where you have a mistake.

in module:

obrazek.png.ffc6941ada050fd51d859b71d045dc2a.png

 

in tpl:

obrazek.png.322308a102732d2d2570ce4f8b24c529.png

 

(sample) on category page:

obrazek.png.56f5e9988e5a59492371442881828070.png

Link to comment
Share on other sites

Quote

The problem will be elsewhere.
Everything is working.

You answer got me thinking... I made a stupid mistake and my code was is the wrong hook. I saw you screenshot showing the hookHeader and I realised it... I works fine now.

Thank you all for your replies !

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