I had two script, one in .php and one in .tpl
I need to pass the variable in php to the tpl.
I tried this one, but nothinng works (but somehow
it works for one or two days, and after that,
it showed blank,
if i create another php script just to echo the variable, it works.
PHP Code:
<?php
$usdidr2 = "12610.198648";
$usdidr2 = number_format($usdidr,2,',','.');
echo $usdidr2;
session_start();
$regValue = $usdidr2;
$_SESSION['regUSDIDR1'] = $regValue;
?>
SMARTY Code:
<li>
<a href="example.php"><strong>
{php}
session_start();
$regValue = $_SESSION['regUSDIDR1'];
$regValue2 = number_format(45.99*$regValue,2,',','.');
echo "Rp. ".$regValue."";
print_r($regValue);
{/php}
</strong></a>
</li>
Here is the syntax to send data from php to tpl
$smarty->assign('variable name with which you can access the data in tpl', $php_data_you_want_to_send);
Update:
$smarty->assign('rate',$usdidr2);// you just need to write rate without $
You can access it in smarty like {$rate} if it is string
You can access it in smarty like {$rate|print_r} if it is array
You can use this syntax:
$res = "Hello World!";
$this->context->smarty->assign('result', $res);
And passing to .tpl file like this:
{$result}
Hope this helping you.
Related
I want to use my index.php page as my template for all my other pages. So I'm printing it out with the code below.
echo file_get_contents("index.php");
I've added this piece of code into the template (index.php) where i want to display the contents. of whichever page im on.
<?php
echo $index_content;
?>
So when I use
echo file_get_contents("index.php");
to get my page template, on for example users.php. In the users.php file I want to use the code below
$index_content = echo "string";
to then print out my page contents where I added this variable
<?php
echo $index_content;
?>
My problem is when I say $index_contents = echo ("string");
it's not printing anything out. onto my template. or it prints the stuff out but at the end or the beginning of the template. not where i've inserted my variable. Why wont it echo out my stuff where I've inserted my variable.
file_get_contents() give you the source of your file.
If I get you right you want to use include instead. Also don't echo in a variable but assign the value and echo it in the template.
users.php:
$content = 'what ever';
include 'template.php';
other.php:
$content = 'other page';
include 'template.php';
template.php:
echo $content
If you call users.php output will be "what ever". If you call other.php output will be "other page".
You are storing the return value of "echo" in $index_content, which is empty.
Just omit the echo when assigning the string to the variable.
The other problem is, with file_get_contents you don't evaluate the php expression where you echo out the $index_content.
Instead, you should use include('index.php') in users.php, and set the variable $index_contents before that.
I currently have a php which echo my html template.
However in that HTML template there is another echo which calls from another php script.
Just wondering how do I do that? Because once I echo my html template the other it doesn't seems to echo my content from the other php script.
HTML TEMPLATE
<php? $html = '<span>name:<?php echo $name; ?></span><span>email:<?php echo $email; ?></span>' ?>
CONTACT TEMPLATE
<php? $name = "hello world"; $email = "hello#world.com"; ?>
I can see what you're trying to do, and it's a simple error. You can't escape php like that whilst inside setting a variable.
Also, I must add that you are declaring php incorrectly.
This is preferred
<?php
not
<php?
So make sure for your contact template you use the correct tag.
Also to include a file you have to call it/require it.
Back to the original question - Here is your method
<php? $html = '<span>name:<?php echo $name; ?></span><span>email:<?php echo $email; ?></span>' ?>
Here is the correct method
<?php
require('contact.php');
$html = '<span>name:'.$name.'</span><span>email:'.$email.'</span>';
echo $html;
?>
First I created the variable. And when doing so I insert the existing variables by escaping the php. Only once this final variable is created do I echo it.
Hope this helps you on your way.
Try to use include. The include statement includes and evaluates the specified file, in this case - your template.
Just Concatenation
<?
$html = '<span>name:'.$name.'</span><span>email:'.$email.'</span>';
?>
Change the tags from <php? ?> to <?php ?> in your script
Example of what i am trying to do:
<?php
$html_element = '<form><label>country</label><select>'.include_once("country_list.php").'</select></form>';
?>
I have tried the above and the following:
<?php
$html_element = '<form><label>country</label><select><?php include_once("country_list.php"); ?></select></form>';
?>
Of course the "$html_element" is then echoed in some div later. So how can i include the "country_list.php" in this php string variable so that it will pull correctly?
If you want to include the data, no matter which file type it is, do like this:
<?php
$html_element = '<form><label>country</label><select>'.file_get_contents("country_list.php").'</select></form>';
?>
My Code:
<!-- {foreach from=$goods_cat.cat_id item=rec_cat name=f1}-->
<?php
**echo $rec_cat[id]; // get nothing, why?**
$smarty->assign('goods_cat_' . $rec_cat[id], assign_cat_goods($rec_cat[id], 4));
$smarty ->assign('cat_goods_nf' , 'goods_cat_' . $rec_cat[id]);
?>
<!--{foreach from=$cat_goods_nf item=goods}-->
{$goods.url}
<!--{/foreach}-->
<!--{/foreach}-->
I need the id of rec_cat ,so I use PHP Tags to get it,but Its display nothing? why ?How can I correct it?
Don't use <?php tag inside a template, but instead use {php} tag of Smarty :
http://www.smarty.net/docsv2/fr/language.function.php.tpl
Then you get your variables just like so :
$var = $this->get_template_vars('var');
I'm working with a custom autonav in the Concrete5 CMS - but I think this might be more of a general PHP question. I'm having a hard time figuring out if it is possible to do the following.
I have some autonav code that looks like this:
<?php defined('C5_EXECUTE') or die("Access Denied."); ?>
<?php
$nav = BlockType::getByHandle('autonav');
$nav->controller->orderBy = 'display_asc';
$nav->controller->displayPages = 'custom';
$nav->controller->displayPagesCID = '135';
$nav->controller->displaySubPages = 'all';
$nav->controller->displaySubPageLevels = 'all';
$nav->render('tertiary');
?>
I also have a text field that outputs via the following code:
<?php if (!empty($field_4_textbox_text)): ?>
<?php echo htmlentities($field_4_textbox_text, ENT_QUOTES, APP_CHARSET); ?>
<?php endif; ?>
What I'd like to do is have the text output within this line of the autonav code:
$nav->controller->displayPagesCID = '135';
Instead of the hard coded 135, I'd like the text outputted by $field_4_textbox_text to display within those single quotes. Something like:
$nav->controller->displayPagesCID = 'echo $field_4_textbox_text';
But that doesn't work. Nothing I've done works. Is there something obvious I might be missing? I'm feeling clueless.
Thanks!
Use double quotes to parse variables, single quotes can never parse variables in php
$nav->controller->displayPagesCID = " $field_4_textbox_text";