ratosluaf
29.10.2012, 15:14:29
Jak mogę użyć PHP w szablonie smarty? Użycie tagów {php} nie przynosi skutków.
Posiadam taki kod:
Kod
$pagination->render();
Odpowiada on za wyświetlanie się paginacji.
Kod
$smarty->assign("pagination_render",$pagination->render());
Nie działa.
Wrzucenie kodu paginacji do funkcji i wywoływanie funkcji przez assign też nie działa.
Jak więc dodać tam ten kod?
nospor
29.10.2012, 15:16:49
Cytat
Wrzucenie kodu paginacji do funkcji i wywoływanie funkcji przez assign też nie działa.
A robisz potem w smarty:
{$pagination_render}
?
ratosluaf
29.10.2012, 15:31:55
robię
{pagination_render}
Kod
function paginacja(){
require_once("include/db.config.inc.php");
// how many records should be displayed on a page?
$records_per_page = 50;
// include the pagination class
require 'Zebra_Pagination.php';
// instantiate the pagination object
$pagination = new Zebra_Pagination();
// the MySQL statement to fetch the rows
// note how we build the LIMIT
// also, note the "SQL_CALC_FOUND_ROWS"
// this is to get the number of rows that would've been returned if there was no LIMIT
// see http://dev.mysql.com/doc/refman/5.0/en/information-functions.html#function_found-rows
$mysql = mysql_connect($config->db_host,$config->db_user,$config->db_pass);
$MySQL = 'SELECT SQL_CALC_FOUND_ROWS bid FROM amx_bans LIMIT ' . (($pagination->get_page() - 1) * $records_per_page) . ', ' . $records_per_page . '';
// if query could not be executed
if (!($result = @mysql_query($MySQL))) {
// stop execution and display error message
die(mysql_error());
}
// pass the total number of records to the pagination class
$pagination->records($rows['rows']);
// records per page
$pagination->records_per_page($records_per_page);
// render the pagination links
$pagination->render();
}
$smarty->assign("paginacja",paginacja());
to jest w pliku ban_list.php
Kod
{$paginacja}
{paginacja}
Niestety, nie działa.
nospor
29.10.2012, 15:33:23
Przecież wyraźnie napisałem {$pagination_render}
znajdź 10 różnic
{$pagination_render}
{pagination_render}
ratosluaf
29.10.2012, 15:36:14
I to i to nie działa, patrz post wyżej.
nospor
29.10.2012, 15:38:59
1) Z kodu wynika, że o to:
$smarty->assign("paginacja",paginacja());
wywołujesz w funkcji paginacja()... Przecież ty się tym zapętlasz w nieskończonosc
2) Funkcja paginacja() nic nie zwraca wiec trudno oczekiwać by o to:
$smarty->assign("paginacja",paginacja());
przypisała ci colowiek prócz NULLa
ratosluaf
29.10.2012, 15:42:05
Napisałem tak, bo nie chciałem podawać całego amx_bans.php. W rzeczywistości assign znajduje się dalej.
Czyli
Kod
$pagination->render();
Zamienić na
Kod
return $pagination->render();
Dokładnie o to ci chodzi?
nospor
29.10.2012, 15:44:10
No ale funkcja render() też musi zwracać kod. Jeśli tego nie robi to nadal będzie kicha.
Cytat
Napisałem tak, bo nie chciałem podawać całego amx_bans.php. W rzeczywistości assign znajduje się dalej.
Nikt nie kazał podawać całości. ale pisać skrótowo że wygląda to totalnie błędnie, to też nie nalezy.
ratosluaf
29.10.2012, 15:44:15
Okej, ale jeżeli w amx_bans.php nie użyję funkcji i będzie samo $pagination->render();, to paginacja się pojawia, lecz za szablonem.
nospor
29.10.2012, 15:45:47
Bo pewnie twoja funkcja render() ma ECHO które pluje cały kod od razu. No i masz problem
ALbo to popraw i nie rób ECHO tylko zamiast tego zapamietuj wszystko do zmiennej a potem zwracaj tę zmienną
Albo przy pomocy funkcji z rodziny ob_ wyłapuj to co plują twoje ECHO z render()
Do wyboru do koloru
ratosluaf
29.10.2012, 15:47:50
Kod
/**
* Generates the output.
*
* <i>Make sure your script references the CSS file!</i>
*
* <code>
* // generate output but don't echo it
* // but return it instead
* $output = $pagination->render(true);
* </code>
*
* @param boolean $return_output Setting this argument to TRUE will instruct the script to return the
* generated output rather than outputting it to the screen.
*
* Default is FALSE.
*
* @return void
*/
function render($return_output = false)
{
// get some properties of the class
$this->get_page();
// if there is a single page, or no pages at all, don't display anything
if ($this->_total_pages <= 1) return '';
// start building output
$output = '<div class="pagination">';
// if the number of total pages available is greater than the number of selectable pages
// it means we can show the "previous page" link
if ($this->_total_pages > $this->_selectable_pages) {
$output .= '<a href="' .
// the href is different if we're on the first page
($this->page == 1 ? 'java script:void(0)' : $this->_build_uri($this->page - 1)) .
// if we're on the first page, the link is disabled
'" class="navigation left' . ($this->page == 1 ? ' disabled' : '') . '"' .
// good for SEO
// http://googlewebmastercentral.blogspot.de/2011/09/pagination-with-relnext-and-relprev.html
' rel="prev"' .
'>previous page</a>';
}
Dokładnie, czyli wystarczy użyć tej zmiennej i potem przypisać ją do templatki?
nospor
29.10.2012, 15:48:51
No render() ma mieć na końcu:
return $output;
a potem możesz z tym robić co chcesz.
ratosluaf
29.10.2012, 15:52:39
Kod
$output = return $pagination->render(true);
Tak to ma wyglądać? (Pytanie jest głupie i pyta o banały, ale gdybym nie raczkował w php, to bym o takie rzeczy się nie pytał, a dokumentacja mówi mało o używaniu funkcji w zmiennych)
Cały kod:
Kod
require_once("include/db.config.inc.php");
// how many records should be displayed on a page?
$records_per_page = 50;
// include the pagination class
require 'Zebra_Pagination.php';
// instantiate the pagination object
$pagination = new Zebra_Pagination();
// the MySQL statement to fetch the rows
// note how we build the LIMIT
// also, note the "SQL_CALC_FOUND_ROWS"
// this is to get the number of rows that would've been returned if there was no LIMIT
// see http://dev.mysql.com/doc/refman/5.0/en/information-functions.html#function_found-rows
$mysql = mysql_connect($config->db_host,$config->db_user,$config->db_pass);
$MySQL = 'SELECT SQL_CALC_FOUND_ROWS bid FROM amx_bans LIMIT ' . (($pagination->get_page() - 1) * $records_per_page) . ', ' . $records_per_page . '';
// if query could not be executed
if (!($result = @mysql_query($MySQL))) {
// stop execution and display error message
die(mysql_error());
}
// pass the total number of records to the pagination class
$pagination->records($rows['rows']);
// records per page
$pagination->records_per_page($records_per_page);
// render the pagination links
$output = echo $pagination->render(true);
$smarty->assign("output",$output);
Nie wyświetla się nic...
nospor
29.10.2012, 15:54:05
return $output;
To ma być na końcu funkcji render. Jeśli nie wiesz jak się uzywa RETURN to zajrzyj do manuala do działu funkcji - tam masz to opisane.
ratosluaf
29.10.2012, 15:55:38
Jest już takie coś:
Kod
echo $output;
nospor
29.10.2012, 16:00:22
Przecież napisałem wyraźnie:
return $output;
echo $output ma zniknąć.
Funkcja ma zwracać tekst a nie go wyświetlać....
A tak poza tym z kodu co pokazałes to wynika, ze funkcja render() i tak już chyba zwraca tekst. Wystrczy wywołąć ją z parametrem true.
Wówczas nie tak jak ty pisałes:
$output = echo $pagination->render(true);
$smarty->assign("output",$output);
a:
$smarty->assign("output",$pagination->render(true));
A w szablonie smarty:
{$output}
ratosluaf
29.10.2012, 16:14:20
Dzięki, w końcu działa

Jeszcze nigdy nie spotkałem się z tak szybką pomocą na forum.
To jest wersja lo-fi głównej zawartości. Aby zobaczyć pełną wersję z większą zawartością, obrazkami i formatowaniem proszę
kliknij tutaj.