Du bist nicht angemeldet. Der Zugriff auf einige Boards wurde daher deaktiviert.

#1 25. Februar 2011 21:48

piratos
Gast

[GELÖST] Plugin assign_global_var

Damit lassen sich globale Variable an Smarty zuweisen und in Inhalten oder Templates nutzen:

<?php

#-------------------------------------------------------------------------
# Plugin assign_global_var
# Version: 1.0
#-------------------------------------------------------------------------
# This is (c) 2011 by Jan Alfred Czarnowski (czarnowski@powercms.org)
# This project's homepage is: http://powercms.org
#
#-------------------------------------------------------------------------
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
# Or read it online: http://www.gnu.org/licenses/licenses.html#GPL
#
#-------------------------------------------------------------------------

function smarty_function_assign_global_var($params, &$smarty) {
    if ($params['name'] == '')
        return;
    if ($params['neu'] <> '')
        $neu = $params['neu'];
    else
        $neu=false;
    $pa = $params['name'];
    global $$pa;
    if (!$neu)
        $smarty->assign($pa, $$pa);
    else
        $smarty->assign($neu, $$pa);
}

?>

Einsatzbeispiel in einer Seite:

{assign_global_var neu ='cg' name='gCms'}
{assign var='db' value=$cg->GetDB()}
{assign var = 'result' value=$db->GetArray('select content_id from cms_content order by hierarchy')}
{$result|var_dump}

Man kann neu als Parameter angeben, dann wird die globale Variable unter name an Smarty unter neu zugewiesen, ansonsten unter name.

Es können alle Arten von Variable zugewiesen werden, wenn sie global vorliegen.

Damit lassen sich auch Dinge direkt im Template erledigen, wozu man sonst ein Plugin benötigt.