I want to fetch a php file in my .tpl but it will be display as plain text. if i use include it works, but need to fetch it.
{fetch file="http://domain.xy/index.php?c=2"}
thx for help
{php}
//include php code to get the text file here
{/php}
You could maybe do something like this and include the php code you need between the tags. The php tags let you put straight php into the template.
Or it may be better to fetch the file content before you load the template and assign it to a smarty variable then call the template. Something like this:
$smarty->assign('some_var', file_get_contents ('http://domain.xy/index.php?c=2'));
$smarty->display('the_current.tpl');
thx. the following code works for me
{php}
$url = file_get_contents('http://domain.xy/index.php?c=1');
echo $url;
{/php}
Related
Is possible pass from php to tpl with this:
PHP:
$smarty->assign('variable',$foo);
TPL:
{$variable}
But i don't know if is possible pass a variable from tpl to php...
I have never tried it, but take a look to get_template_vars() and {php} tag. Maybe it is what you are looking for.
I want to change my website main page, using php code!, I found the tpl file which I need to change, problem is that I have no experience in tpl and I want to write it with php but it won't work. I am trying to use:
{include_php file="/path/to/somefile.php"}
To use php language but it does not work. How does it work with tpl in Smarty templates using php language.
My suggestion the following is a php file
include_once("/var/www/html/smarty/libs/Smarty.class.php");
$smartyobject=new Smarty();
$varr="stackoverflow"; //php variable it can also be a array";
$smartyobject->assign("website", $varr); //assigning a variable to "website", and this is now a smarty variable.
$smartyobject->display("tplfile"); //this is the actuall file that get displayed, in this tpl file you can use "website" variable {$website}
smarty has so many in functions loops.. so there is no need include any php file. do whatever you want to in the php file save the result in a variable and display it in a tpl file.
I am trying to display some html code from a php variable created in a controller like
$this->template()->assign(array('html' => "<p>Ajith chandran</p>"));
But when I write {$html} in template file,this html code is rendering as normal text and displaying it with full code in browser.And I tried this in two other ways like
{php}echo $html;{/php} and <?php echo $html; ?>
but both are not displaying anything.
You should display this variable in Smarty using simply:
{html}
If for any reason I need to use {php} tag what I really don't recommend (it's a bad practice and in Smarty 3.1 is deprecated) you can display html value using:
{php} echo $template->getTemplateVars('html'); {/php}
Of course those both syntaxes should be used in TPL file and not in PHP file. If you want to use in PHP file any Smarty variable you could probably use in your case:
echo $this->template()->getTemplateVars('html');
What you should also consider is security of this solution. You should think about escaping your output or at least strip some tags. Now you can set<script>alert('I am a very bad script');</script> to html variable and user will see JavaScript alert. I assume you might not know what your html variable will store and Smarty by default doesn't escape variables what can make to serious problems with your site.
So you could either use for example:
{$html|escape:"html"}
{php} echo htmlspecialchars($template->getTemplateVars('html')); {/php}
to display those data safely or use global setting escape_html to do it for all Smarty variables:
In PHP
$this->template->escape_html = true;
In Smarty:
{$html}
{php} echo htmlspecialchars($template->getTemplateVars('html')); {/php}
As you see when using {php} tag even if setting escape_html to true you need to escape data in PHP otherwise you will display data as they were set.
Either set $escape_html to false in Smarty or use {$html nofilter}
See: http://www.smarty.net/docs/en/variable.escape.html.tpl
I have completely no idea about smarty template system. What I am trying to do is include a php file to get some variable inside a .tpl file (this is a WHMCS template).
I have tried like:
{php} include ('file.php'); {/php} //doesn't work
{include_php file='file.php'} //doesn't work
I have also followed the answer of this question. Still couldn't get it working.
How can I include code.php inside header.tpl of WHMCS? Any help please?
Just for reference: both tpl and php file is in the same directory, if that helps anyway.
It's really not recommended to use php code in Smarty. In fact it's now deprecated and you should avoid such solution as much as possible because it often doesn't have sense.
However if you really want to use PHP in your Smarty files for some reason you need to use SmartyBC (Smarty Backward Compatibility) class instead of Smarty class.
So for example instead of:
require_once(_PS_SMARTY_DIR_.'Smarty.class.php');
$smarty = new Smarty();
you should use:
require_once('SmartyBC.class.php');
$smarty = new SmartyBC();
Then you will be able to use PHP in your Smarty template files
EDIT
If you have just problem with including it's probably your directories problem (however you haven't showed any errors).
I assume you have your files inside your templates directory and you set it properly using:
$smarty->setTemplateDir('templates');
if you simple display index.tpl file in Smarty and you have this PHP file in the same directory (in template directory) you can include it without a path.
However if you include in this index.tpl file another tpl file, when you want to include php file you need to pass the full path to this PHP file, for example:
{include_php 'templates/file.php''}
Your using Smarty the wrong way. The whole point of Smarty is to NOT include any PHP in your presentation bits (views, a.k.a. the good ol' HTML).
So, whatever you're trying to do in that PHP file, just let it do its magic, but send the actual result to Smarty. For example, do you want to show a table of users you get out of a database? Execute the query, fetch the result and pass the results (like an array of results) to smarty like this:
<?php
$smarty = new Smarty();
$users = $db->query('SELECT * FROM users');
// Assign query results to template file.
$smarty->assign('users', $users);
// Compile and display the template.
$smarty->display('header.tpl');
Now, in your smarty template you can access that array like this:
<html>
{foreach from=$users item=user}
Username: {$user->username}<br />
{/foreach}
</html>
I hope you see where I am going. Keep your application logic in the PHP file, and let the template just take care of the looks. Keep the template as dumb as possible!
You get data into Smarty by assigning it. Embedding PHP is not recommended, and deprecated from Smarty 3.
php:
$smarty->assign('foo','bar');
smarty:
{$foo}
I need to use PHP file_get_contents() in smarty tpl file. I can't use it in PHP and assign it to smarty template. Because the URL is generated dynamically through loop inside smarty template file. So I'm using smarty plugin function to achieve that task. But I want to know whether is there any way I can use it in template file directly instead of parsing it from plugin file.
I've attached the plugin code which I'm using to achieve this function. Please anyone let me know how to use it in smarty tpl file directly.
function smarty_function_getTitle($params)
{
if ($params['id']) {
$content = file_get_contents("http://youtube.com/get_video_info?video_id=".$params['id']);
parse_str($content, $ytarr);
return $ytarr['title'];
}
}
I've used below code to call it in smarty template:
{getTitle id=$videoId}
Suggestions are welcome!
For those of you reading that didn't read the comments above, myself and OP are both aware that this is not how you use a template engine. He seems to have his reasons for wanting to do this directly in the template rather than a plugin or ahead of time in his code. So don't slag me for demonstrating how, please :)
Here is how you can do it in Smarty.
{"http://youtube.com/get_video_info?video_id=`$videoId`"|file_get_contents|parse_str:$result}
{$result.title}
I did the first part all in one call but if you want to be careful you can split it into multiple calls with checks. But I tested this locally and it worked fine.