Jump to content
Studio Creativ

Call PHP variable in a .tpl

Recommended Posts

Hello,

I have a simple PHP file like this:

<?php $var="Hello world"; ?>

I want to call $var inside the ps_customtext.tpl file, to show it's content.

 

How should I proceed?

 

Thanks for your help.

Regards.

Share this post


Link to post
Share on other sites

Hello,

You just need to assign this variable on tpl.

phpfile.php
$var="Hello world";
$smarty->assign("var", $var);


Now you can just use {$var} on the tpl file.
like - <div>{$var}</div>

Share this post


Link to post
Share on other sites

Thanks for your answer.

So, this is how my code actually look like:

 

my_php_file.php :

<?php

	$var="Hello world";
	$smarty->assign("var", $var);

?>

 

ps_customtext.tpl :

[...]

<p>{$var}</p>

[...]

 

But still it doesn't work: the value of $var doesn't appear once I load the page, while all other contents of ps_customtext.tpl do appear.

 

What am I missing? Maybe I have to add some additionnal code into my PHP file to tell it to use Smarty?


I don't understand how an annonymous PHP file that is stored somewhere between PS files can generate the $var variable through Smarty if it's not even executed?

Edited by Studio Creativ (see edit history)

Share this post


Link to post
Share on other sites

Prestashop didn't work like that. If ps_customtext.tpl is a file from module you supposed to add your variable into this module controller, in this controller also you supposed to point hook and file to execute.

Share this post


Link to post
Share on other sites
19 hours ago, Peeyush Agrawal said:

Hello

Have you created the object of Smarty class?

If not then here $smarty should be an instance of Smarty  class.

 

Hello, no, the file is as you see it above. Can you explain how to do that?
I just want to call a variable that comes from an external PHP file, inside a tpl file.

Edited by Studio Creativ (see edit history)

Share this post


Link to post
Share on other sites

At this point I've included my own function inside CustomText.php file, but then how do call that function inside ps_customtext.php?

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


×
×
  • Create New...

Important Information

Cookies ensure the smooth running of our services. Using these, you accept the use of cookies. Learn More