Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
PHP tag in templates
#1
Hello,

The issue I am experiencing is related with the cached templates not being updated when there is a modification done in login.tpl. I think this will apply to all templates and in fact from a broader perspective to php code interpretation in those templates. Let me elaborate:

The issue stems from the way php is now tagged in those templates. There probably was an update to COREbos code in this regard and that seems to be why my customisations are not working. Prior the update to COREbos I did last week, php code was tagged with the {php} tag and everything was working just fine. That tag is not working anymore and prevents the php code from being interpreted so I tried using <?php and ?> but when I do that I get some sort of duplicate tagging in the cached template file such as for example this: 

Code:
&copy; 2004- <?php echo '<?php ';?>echo date('Y')<?php echo '?>';?>

for the record the code in the template file is:
Code:
&copy; 2004- <?php echo date('Y')?>

The php code is then output as text and of course not interpreted as php code. If I remove those duplicate tags  within the cached file then everything works fine. Not a solution of course but it proves the code is functional.

I tried using the {} as well since that’s how you seem to be tagging php in the templates but I must be doing something wrong because using that tag doesn’t load my customisations, at least not the first part of the code with functions and such.

I also tried to put the customised code in a différent template file and then calling the code through include file= in the login.tpl template but that doesn’t seem to work either.
Reply
#2
Hi.

Since 1 month ago, I think, we have updated Smarty to version 3. So this version not support more the php tags, you can read here: http://www.smarty.net/docs/en/language.function.php.tpl

We have updated all the templates removing all the php tags and write this code into php scripts to after assign this values to smarty variables.

For example:

If you want date('Y') on your template, you have to add this to the php file that call the template like this:

PHP Code:
$smarty = new vtigerCRM_Smarty();
$smarty->assign('YEAR'date('Y')); 

And now on smarty template you can use YEAR variable like this: {$YEAR}
Reply
#3
(01-31-2017, 07:16 AM)omarllorens Wrote: Hi.

Since 1 month ago, I think, we have updated Smarty to version 3. So this version not support more the php tags, you can read here: http://www.smarty.net/docs/en/language.function.php.tpl

We have updated all the templates removing all the php tags and write this code into php scripts to after assign this values to smarty variables.

For example:

If you want date('Y') on your template, you have to add this to the php file that call the template like this:


PHP Code:
$smarty = new vtigerCRM_Smarty();
$smarty->assign('YEAR'date('Y')); 

And now on smarty template you can use YEAR variable like this: {$YEAR}

Thank you Omar. That's good to know. However that only addresses partially my issue. The code I provided above is just a sample. The remaining code I didn't post here is the one creating most the trouble. Here it is:


Code:
{php} echo "Your are connecting from IP ";

echo $_SERVER["REMOTE_ADDR"];
function get_ip_address() {
  if (!empty($_SERVER['HTTP_CLIENT_IP']) && $this->validate_ip($_SERVER['HTTP_CLIENT_IP']))
   return $_SERVER['HTTP_CLIENT_IP'];
  if (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
    $iplist = explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']);
    foreach ($iplist as $ip) {
     if ($this->validate_ip($ip))
      return $ip;
    }
   }
  if (!empty($_SERVER['HTTP_X_FORWARDED']) && $this->validate_ip($_SERVER['HTTP_X_FORWARDED']))
   return $_SERVER['HTTP_X_FORWARDED'];
  if (!empty($_SERVER['HTTP_X_CLUSTER_CLIENT_IP']) && $this->validate_ip($_SERVER['HTTP_X_CLUSTER_CLIENT_IP']))
   return $_SERVER['HTTP_X_CLUSTER_CLIENT_IP'];
  if (!empty($_SERVER['HTTP_FORWARDED_FOR']) && $this->validate_ip($_SERVER['HTTP_FORWARDED_FOR']))
   return $_SERVER['HTTP_FORWARDED_FOR'];
  if (!empty($_SERVER['HTTP_FORWARDED']) && $this->validate_ip($_SERVER['HTTP_FORWARDED']))
   return $_SERVER['HTTP_FORWARDED'];
   return $_SERVER['REMOTE_ADDR'];
 }
function validate_ip($ip) {
     if (filter_var($ip, FILTER_VALIDATE_IP, 
                         FILTER_FLAG_IPV4 | 
                         FILTER_FLAG_IPV6 |
                         FILTER_FLAG_NO_PRIV_RANGE | 
                         FILTER_FLAG_NO_RES_RANGE) === false)
         return false;
     self::$ip = $ip;
     return true;
 }
{/php}<br>
<a href='copyright.html' target='_blank'>{$APP.LNK_READ_LICENSE}</a>
|
<a href='http://corebos.org/page/privacy-policy' target='_blank'>{$APP.LNK_PRIVACY_POLICY}</a>
|
&copy; 2004-{php} {date('Y') {/php}

I've left the old tag to show you how it was before. So how should my code be tagged from now on?
Reply
#4
You can do the same.

Each echo and the return of your function must be assgin into smarty variable and after used these variables like {$YEAR} into your smarty template.

You have a lot of examples of this on coreBOS. For example, look at modules/Vtiger/EditView.php, here you have many assings values to smarty.
Reply
#5
I see but that's kind of a hassle though and makes simple customisations more complex. This implies I have to assign smarty variables for most of the customisations I am doing which means I have to modify additional files.

To which php file do I need to add those variables to?
Reply
#6
In the php file wher is calling to your template.

For example, the modules/Accounts/EditView.php on the end of this file call to:

$smarty->display('salesEditView.tpl');

So, this EditView.php and modules/Vtiger/EditView.php contains the smarty variables.

This way to assign variables to smarty on coreBOS and Vtiger exists since a lot of years, this is not new, but some smarty templates in the past contained this php tags, and now Smarty decided that this is not correct.
Reply
#7
The code I am adding is in the login.tpl template. Does this means I have to add the variables to the index.php file?
Reply
#8
For know where is calling the smarty template, you can search "Login.tpl" on your project. In this case this call is on modules/Users/Login.php at the end of this file.
Reply
#9
Thanks Omar. I really appreciated the help. I think however this is a bit too time consuming to create multiple variables for just a few echos and a few functions that are only used in one or two templates. I understand if the purpose is to write a lot of repetitive code. These variables makes it much faster but in my case it just adds additional maintenance to the code and to be honest I don't have all that much knowledge in coding.

Is this the only way? If so then I guess I'll have to drop those customisations for good.
Reply
#10
Paul, can you join our gitter group?

https://gitter.im/corebos/discuss

that will probably be more agile for you

In any case, this separation of concern that Smarty3 has imposed on the developers is the correct way to go. It is what a templating system is for, it makes no sense to invest in a templating system just to go out and start using php as a templating system.

In any case, the correct way to do what you want is not adding that to the Login template, it is adding it to the authentication process. Right now coreBOS has IP bloocking for admin users by defining the global variable Application_AdminLoginIPs. I didn't answer your email because I wanted to enhance that validation with your IP detection code (which is more complete than ours) and also add normal user IP blocking. That way the application will simply return the correct login error message and you won't have to modify the template at all unless you want to change the layout

A little patience, we will get to it
Joe
TSolucio
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)