|
Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon. |
|
View previous topic :: View next topic |
Author |
Message |
cgf64ste Smarty n00b
Joined: 28 Sep 2012 Posts: 1
|
Posted: Fri Oct 05, 2012 3:57 am Post subject: Smarty 3 assingByRef doesn't work as documented (expected). |
|
|
In a nutshell, the following assignment doesn't seem to work as expected in the template file. I'm running Smarty 3.11 (I also tried with 3.12) on Windoze 7 XAMP (Apache/2.4.2 (Win32) OpenSSL/1.0.1c PHP/5.4.4).
The code is from the book "Smarty PHP Template Programming and Applications", which was written for Smarty 2. I updated the example, changing $smarty->assign_by_ref('publisher',$publisher); to $smarty->assignByRef('publisher',$publisher) in index.php; which I found at http://www.smarty.net/forums/viewtopic.php?p=64427
The assignment in the template below should have changed the value of "Packet Publishing" to "PACKET PUBLISHING" Any suggestions would be appreciated. I think it may be a bug.
Code: |
the problem line in the template:
...
{assign var="publisher" value=$publisher|upper}
...
|
My index.php
Code: |
<?php
require("Smarty.class.php");
$publisher='Packt Publishing';
$website = 'http://www.packtpub.com';
$smarty = new Smarty;
$smarty->assignByRef('publisher',$publisher);
$smarty->assign('website',$website);
$smarty->display('index.tpl');
print ' <hr>
<h3> Hello, I am index.php. First, I displayed the index.tpl template. </h3>
Now we will see what happened to the content our variables: <br>
The $publisher variable was sent using <b> assign_by_ref </b> method
and now contains <font color=red> '. $publisher .'</font><br>
and the $website variable was sent using <b> assign </b> method
and now contains <font color=red> '. $website ;
?>
|
index.tpl
Code: |
<html>
<head> <title> Example of assign and assign_by_ref </title>
</head>
<body>
<h3> Hello, I am the template index.tpl and I say: </h3>
I received the value of variable $publisher :
<font color="blue"> {$publisher} </font> <br>
I received the value of variable $website :
<font color="blue"> {$website} </font> <br>
<br>
I will apply the UPPER modifier on both variables by doing : <br>
<b> assign var="publisher" value=$publisher|upper </b> <br>
<b> assign var="website" value=$website|upper </b> <br>
{assign var="publisher" value=$publisher|upper}
{assign var="website" value=$website|upper}
...Done ! <br>
Now, $publisher is:
<font color="blue"> <b> {$publisher} </b></font> <br>
and $website is
<font color="blue"> <b> {$website} </b></font> <br>
<h3> End of Template index.tpl </h3>
|
OUTPUT
Hello, I am the template index.tpl and I say:
I received the value of variable $publisher : Packt Publishing
I received the value of variable $website : http://www.packtpub.com
I will apply the UPPER modifier on both variables by doing :
assign var="publisher" value=$publisher|upper
assign var="website" value=$website|upper
...Done !
Now, $publisher is: PACKT PUBLISHING
and $website is HTTP://WWW.PACKTPUB.COM
End of Template index.tpl
------------------------------------------------------------------------
Hello, I am index.php. First, I displayed the index.tpl template.
Now we will see what happened to the content our variables:
The $publisher variable was sent using assign_by_ref method and now contains Packt Publishing
and the $website variable was sent using assign method and now contains http://www.packtpub.com
Generated HTML
Code: |
<html>
<head> <title> Example of assign and assign_by_ref </title>
</head>
<body>
<h3> Hello, I am the template index.tpl and I say: </h3>
I received the value of variable $publisher :
<font color="blue"> Packt Publishing </font> <br>
I received the value of variable $website :
<font color="blue"> http://www.packtpub.com </font> <br>
<br>
I will apply the UPPER modifier on both variables by doing : <br>
<b> assign var="publisher" value=$publisher|upper </b> <br>
<b> assign var="website" value=$website|upper </b> <br>
...Done ! <br>
Now, $publisher is:
<font color="blue"> <b> PACKT PUBLISHING </b></font> <br>
and $website is
<font color="blue"> <b> HTTP://WWW.PACKTPUB.COM </b></font> <br>
<h3> End of Template index.tpl </h3>
<hr>
<h3> Hello, I am index.php. First, I displayed the index.tpl template. </h3>
Now we will see what happened to the content our variables: <br>
The $publisher variable was sent using <b> assign_by_ref </b> method
and now contains <font color=red> Packt Publishing</font><br>
and the $website variable was sent using <b> assign </b> method
and now contains <font color=red> http://www.packtpub.com
|
|
|
Back to top |
|
U.Tews Administrator
Joined: 22 Nov 2006 Posts: 5068 Location: Hamburg / Germany
|
Posted: Fri Oct 05, 2012 2:09 pm Post subject: |
|
|
The original intention of assign_by_ref in Smarty2 was to pass large chuncks of data by pointer and objects in PHP4.
With PHP5 this was nolonger required.
The example of the book does break the separation of logic and design. A template should never effect varibales in your script. This is bad practise.
If you really need this behaviour it's available by using the SmartyBC class. |
|
Back to top |
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|