Jump to content

How to use php code in a tpl file


bogdean

Recommended Posts

Hi. i have this code 

<?php

function getcurlway($url) {
    $content=file_get_contents($url);
    return $content;
}

function pick($start,$stop,$from){
@$from=explode($start,$from);
@$from=explode($stop,$from[1]);
@$from=$from[0];
return $from;
}

$ch = curl_init(); 

$url="http://blog.larisfashion.ro/";
curl_setopt($ch, CURLOPT_URL, $url); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
curl_setopt($ch, CURLOPT_REFERER, "http://blog.larisfashion.ro/");
curl_setopt($ch, CURLOPT_USERAGENT, $userAgent); 

$file = curl_exec($ch); 
curl_close($ch); 
$cons=explode('<article id',$file);
$cc=count($cons);
for($i=1; $i<=3; $i++){
$link=pick('<h2>','</h2>',$cons[$i]);
$text=pick('</a></p>','</article>',$cons[$i]);
$img=pick('<img src="','"',$cons[$i]);

echo $link.'<br>';
echo $text.'<br>';
echo $img.'<br>';

}
?>

how can i use this in a tpl file? any ideas? thanks 

 

Link to comment
Share on other sites

It's the opposite of what tpl is meant for. The correct way would be to write a module and hand over the results of your php into a smarty array. This array could then be referenced within the tpl - same way as every module does it.

 

If you insist to go the wrong way - this is the syntax to run php within tpl.

{php}echo "this is php code running within a smarty file!"{/php}

It is possible that you have to change the smarty class to allow this. Change smarty class php

$php_handling = SMARTY_PHP_ALLOW;
Edited by Scully (see edit history)
Link to comment
Share on other sites

The usual way to handle this is to have the code run in the place where the page is generated and to assign the result to a Smarty variable that is then used in the tpl file. 

 

For example the product page is created when the file /controllers/front/ProductController.php calls/implements the product.tpl file in its function initContent. So you should run in that file (or an override of it) the extra code and assign it to a Smarty variable that you then use in the product.tpl file.

  • Like 1
Link to comment
Share on other sites

That is exactely the way it should be. Since we do not know where the asking bogdean want's to implement this php, we can just guess.

 

Other thoughts:

  • Your not checking character encoding
  • Your do not perform any kind of input validation
  • Your code just relies on data should be where you expect it to be
  • If formatting of the source page changes, you're code will most likely fail.
  • The user agent isn't set this way you used it.

 

Overall:

I have to say it here:

It's a very sloppy programmed pice of source coude.

This code would never ever run on a machine I have responsibility for.

 

Better but also not secure would be to use the rss feed of the source page.

http://blog.larisfashion.ro/feed/

Edited by Scully (see edit history)
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...