|
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 |
ardiartani Smarty n00b
Joined: 28 Jul 2015 Posts: 3
|
Posted: Tue Jul 28, 2015 10:30 am Post subject: Smarty Code inside PHP not working |
|
|
Hi,
My smarty code is : {$movie_detail_single.imdb} it works fine even when i do {/php}{$movie_detail_single.imdb}{php} but when i enter the code inside file_get_content it doesn't work??
When i do this it doesn't work:
http://api.themoviedb.org/3/movie/{/php}{$movie_detail_single.imdb}{php}?api_key=94a2f36cd4e27626b6a7a07766a76196&append_to_response=trailers
it show this error:
Warning: file_get_contents(http://api.themoviedb.org/3/movie/ ?><?php echo Array['movie_detail_single']['imdb']; ?> <?php ?api_key=94a2f36cd4e27626b6a7a07766a76196&append_to_response=trailers): failed to open stream: HTTP request failed! HTTP/1.1 400 BAD_REQUEST in /cache/templates_c/%%E6^E60^E6058F2E%%modules_view.tpl.php on line 195
{php}
$url = file_get_contents("http://api.themoviedb.org/3/movie/<ENTER HERE SMARTY CODE>?api_key=94a2f36cd4e27626b6a7a07766a76196&append_to_response=trailers");
$json = json_decode($url, true);
$m_poster = $json["poster_path"];
$m_tagline = $json["tagline"];
{/php}
{php} echo "$m_title";{/php} |
|
Back to top |
|
U.Tews Administrator
Joined: 22 Nov 2006 Posts: 5068 Location: Hamburg / Germany
|
Posted: Tue Jul 28, 2015 3:14 pm Post subject: |
|
|
Using {php} tags (PHP code in templates) is deprecated as it is unsafe and creates all sorts of problems and side effects. Do it in your PHP script and assign the result as template variable or write a pluin.
See http://www.smarty.net/docsv2/en/plugins.tpl |
|
Back to top |
|
ardiartani Smarty n00b
Joined: 28 Jul 2015 Posts: 3
|
Posted: Tue Jul 28, 2015 6:17 pm Post subject: |
|
|
i did what you said, i created a php file
{php}
$imdb = file_get_contents("http://website.com/parts/imdb.php");
$url = file_get_contents("http://api.themoviedb.org/3/movie/$imdb?api_key=94a2f36cd4e27626b6a7a07766a76196&append_to_response=trailers");
$json = json_decode($url, true);
$m_poster = $json["poster_path"];
{/php}
THIS DOESNT WORK
<h4>{php} echo $imdb; {/php}</h4>
it shows this {$movie_detail_single.imdb}
THIS WORKS
<h1>{include file='parts/imdb.php'}</h1>
it shows tt2268016 |
|
Back to top |
|
U.Tews Administrator
Joined: 22 Nov 2006 Posts: 5068 Location: Hamburg / Germany
|
Posted: Tue Jul 28, 2015 7:28 pm Post subject: |
|
|
No that is same evil.
A function plugin:
See http://www.smarty.net/docsv2/en/plugins.functions.tpl
Put as file function.getmovie.php into the plugins folder
Code: | <?php
function smarty_function_getmovie($params, &$smarty)
{
$url = file_get_contents("http://api.themoviedb.org/3/movie/{$params['imdb']}?api_key=94a2f36cd4e27626b6a7a07766a76196&append_to_response=trailers");
$smarty->assign($params['assign'], json_decode($url, true));
} |
In your template
Code: |
{getmovie imdb=$movie_detail_single.imdb assign='movie1'}
// will get the data and assign it as template variable $movie1
now you can access the data
{$movie1.title}
{$movie1.poster_path}
{$movie1.tagline}
etc.
|
You can use it in any template with any imdb parameter and assign it to any template variable.
Note this is an untested sample code |
|
Back to top |
|
ardiartani Smarty n00b
Joined: 28 Jul 2015 Posts: 3
|
Posted: Wed Jul 29, 2015 2:20 pm Post subject: |
|
|
thank you it worked! |
|
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
|