Is possible pass a variable from a tpl file to php file? - php

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.

Related

PHP Smarty template main page problam

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.

Phpfox display html from php variable

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

smarty fetch php file not working

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}

PHP + Smarty: Parse PHP+HTML into a String?

I am using PHP in combination with Smarty Templates to generate pages serverside. Currently, I am loading a page as follows:
$smarty->assign('app', file_get_contents("some_content.php"));
Where some content contains HTML with PHP tags and code inside those tags.
I would like the PHP content inside this file within the current scope (That of the script reading the file), so that a particular function I've defined is available. How would I go about doing so? All the information I can find is regarding the eval(...) function, which doesn't seem to be able to cope with the HTML/PHP mixture: would I need to perform a find/eval/replace operation to achieve the desired result, or is there a more elegant way of doing this?
From my opinion, this short snippet of the code you posted shows that something is generally wrong there :)
But nevertheless you can achieve whatever you are trying to achieve by doing the following:
ob_start();
include("some_content.php");
$result = ob_get_clean();
$smarty->assign('app', $result);
Ich, I'm such a dummkopf. There is an answer right on the PHP manual for eval, right under my nose. Here is the answer I neglected to notice.
You can use {literal}...{/literal} smarty tags to display any content in smarty templates as is. It used to transfer java scripts and other specific content.

How can I assign a smarty variable to a PHP variable

How can I assign a smarty variable to a PHP variable. I have
{assign var=arrayname value="/"|explode:$form.attributes}
{assign var=id value=$arrayname.2}
I want to assign the smarty variable id to a php variable
<?php
// I want $id here
?>
How can I do this?
Thanks in advance
I don't quite understand...how was your $arrayname passed to Smarty....? I assume you have used something like
<?php
$smarty->assign('arrayname',$arrayvariable)
?>
So in PHP there is equation:
$you_have_wanted==$arrayvariable[2]
This isn't really a situation you should find yourself in.
You should be able to either do what you want to do entirely in smarty or entirely in php.
If you explain in detail what your trying to do we can help you with one or the other
It is possible to use php in a smarty template but:
"This is for advanced users only, not normally needed and not recommended."
The whole point of the smarty template is to keep your business logic in a separate view.
Therefore you should deal with your variables in the php page then assign them to the template as in Lucifer Orichalcum's example.
If the user Id value you a trying to retrieve comes from a form you have to deal with the form values in the .php file ($_POST['id']) not in the .tpl
Same rule for your sessions variables.
Can you tell us more about where the Id comes from in the first place ?

Categories