bogdean Posted July 11, 2017 Share Posted July 11, 2017 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 More sharing options...
Scully Posted July 11, 2017 Share Posted July 11, 2017 (edited) 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 July 11, 2017 by Scully (see edit history) Link to comment Share on other sites More sharing options...
bogdean Posted July 13, 2017 Author Share Posted July 13, 2017 i've changed Smarty_class.php and put public $php_handling = self::PHP_ALLOW; instead of PHP_PASSTHRU but my page doesn't work completly when i use <?php or {php} Link to comment Share on other sites More sharing options...
musicmaster Posted July 13, 2017 Share Posted July 13, 2017 You cannot include php in Smarty pages. The {php} label works only on older versions of Smarty, not the one implemented in Prestashop. Link to comment Share on other sites More sharing options...
Scully Posted July 14, 2017 Share Posted July 14, 2017 It's the wrong way anyway. Somehow one could run php inside tpl, but it's crap then. Normal way would be to include the code at the place where thie correspoinding tpl is fired. Link to comment Share on other sites More sharing options...
musicmaster Posted July 14, 2017 Share Posted July 14, 2017 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. 1 Link to comment Share on other sites More sharing options...
Scully Posted July 14, 2017 Share Posted July 14, 2017 (edited) 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 July 14, 2017 by Scully (see edit history) Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now