Reputation: 21
Can someone help me out here.
How can i Call a PHP class public function inside a Smarty template file for example.
I have 2 Functions inside the Movie Class
GetAllMovies(); // Gets all Movies
and
GetMovie($movie_id); // Gets movie by movie id
Now i am calling the GetAllMovies();
and assigning it to the template
<?php
include 'movie.class.php';
$movie = new Movie();
$movies = $movie->GetAllMovies();
$smarty->assign('movies',$movies);
<?
Now inside the template file i've got a foreach
statement for the movies.
{foreach from=$movies key=key item=mov}
// Access Movie ID, Title And Images
{/foreach}
Now what i want to do is to call GetMovie($movie_id);
inside this foreach
statement for example do something like this.
Assign Movie class to Smarty Template.
$smarty->assign('movie',$movie);
Then use $movie
to call the function for example
{foreach from=$movies key=key item=mov}
{assign var=movie_info value=$movie::GetMovie($mov.id)}
{$movie_info.rating}
{/foreach}
Could someone please point me in the right direction.
Upvotes: 0
Views: 6054
Reputation: 11
I think the proper way to do it, would be pulling your movies data in a 'ready-to-display' form, such as an array, associative array or their multidimensional counterparts that smarty can handle right away with the {foreach} tag. Maybe something that looked like:
array (
0 => array ( 0 => 'PHP the Movie 1', 1 => '2009', 2 => 'Coder 1', 3 => '3', ),
1 => array ( 0 => 'PHP the Movie 2', 1 => '2010', 2 => 'Coder 2', 3 => '2', ),
2 => array ( 0 => 'PHP the Movie 3', 1 => '2011', 2 => 'Coder 3', 3 => '1', ),
)
array (
0 => array (
'title' => 'PHP the Movie 1',
'year' => '2009',
'director' => 'Coder 1',
'rating' => '3', ),
1 => array (
'title' => 'PHP the Movie 2',
'year' => '2010',
'director' => 'Coder 2',
'rating' => '2', ),
2 => array (
'title' => 'PHP the Movie 3',
'year' => '2011',
'director' => 'Coder 3',
'rating' => '1', ),
)
array (
0 => array (
'title' => 'PHP the Movie 1',
'year' => '2009',
'director' => 'Coder 1',
'rating' => '3', ),
1 => array (
'title' => 'PHP the Movie 2',
'year' => '2010',
'director' => 'Coder 2',
'rating' => '2', ),
2 => array (
'title' => 'PHP the Movie 3',
'year' => '2011',
'director' => 'Coder 3',
'rating' => '1', ),
)
Only my humble opinion but I actually use it at work both to display data-sets with smarty and to build JSON responses for AJAX requests to the server.
Upvotes: 1
Reputation: 111829
I wouldn't tell you how to do it because it doesn't make any sense for me.
Template engine is for displaying data and you should get data inside Model/Controller. You should assign to Smarty data that is prepared to display and in Smarty you should display it.
So in your case before:
$smarty->assign('movies',$movies);
you should use:
$movies = $movie->GetAllMovies();
foreach ($movies as $k => $v) {
$movies[$k]['details'] = $movie->GetMovie($v['id');
}
and then in Smarty:
{foreach from=$movies key=key item=mov}
{$mov.details.rating}
{/foreach}
However if I were you, I would consider getting data. I assume that you get your movies from Database, so using:
$movies = $movie->GetAllMovies(); // 1 query
foreach ($movies as $k => $v) {
$movies[$k]['details'] = $movie->GetMovie($v['id'); // n queries
}
you run n+1
queries to your database, where n
is number of movies.
It's quite possible that instead of this, you could run one query to database (depending on your structure using join or even not) so you should rethink if you get data the best way it's possible.
Upvotes: 2
Reputation: 313
It looks realy creepy but if you realy want to use it in that way, I suggest to create array on PHP side and assing it to smarty like:
$movies = array();
$movie = new Movie();
foreach ($movie->GetAllMovies() as $key => $movieDetails) {
$mov = new Movie();
$movies[] = $mov->getMovie($movieDetails['id']);
}
$smarty->assign('movies',$movies);
and in smarty you can loop thrue your $movies array
{foreach from=$movies key=key item=mov}
{$mov.rating}
{/foreach}
Upvotes: 3
Reputation: 13725
Based on this: http://www.smarty.net/docs/en/advanced.features.static.classes.tpl
This should work:
{assign var=movie_info value=Movie::GetMovie($mov.id)}
At least if it is an existing static method...
Upvotes: 2
Reputation: 81
You could use static methods in your movie class.
If for example your class is named "MovieClass" then you could have:
MovieClass::GetMovie($mov.id);
To create a static method just use the following format when declaring the method inside your class
public static GetMovie($mov.id)
{
...
}
Upvotes: 0