Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: Jak używać php w smarty.
Forum PHP.pl > Forum > PHP
ratosluaf
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
Cytat
Wrzucenie kodu paginacji do funkcji i wywoływanie funkcji przez assign też nie działa.


A robisz potem w smarty:
{$pagination_render}
?
ratosluaf
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
Przecież wyraźnie napisałem {$pagination_render}

znajdź 10 różnic
{$pagination_render}
{pagination_render}
ratosluaf
I to i to nie działa, patrz post wyżej.
nospor
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
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
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
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
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
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
No render() ma mieć na końcu:
return $output;
a potem możesz z tym robić co chcesz.
ratosluaf
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
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
Jest już takie coś:
Kod
echo $output;
nospor
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
Dzięki, w końcu działa smile.gif
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.
Invision Power Board © 2001-2025 Invision Power Services, Inc.