/**
     * Renders the current instance. The $mode parameter can be used to specify
     * which render mode should be used. First, an attempt is made to use
     * a template. If this fails, a render method is called.
     *
     * \param $mode A string denoting the render mode to use (optional).
     * \param $params Additional parameters can be specified, these will be
     * passed unchanged to the render methods.
     *
     */
    function render($mode = null, $params = null)
    {
        /* Handle parameters */
        $args = func_get_args();
        $num_args = func_num_args();
        if ($num_args >= 1) {
            $mode = array_shift($args);
            $params =& $args;
        }
        /* Which render mode? */
        if (is_null($mode)) {
            $mode = $this->getdefault('mode', 'default');
        }
        assert('is_string($mode)');
        /* Check for basetemplate */
        if ($mode === 'default') {
            $template = sprintf('%s.tpl', $this->get('basetemplate'));
        } else {
            $template = sprintf('%s-%s.tpl', $this->get('basetemplate'), $mode);
        }
        $this->set('template', $template);
        if ($this->template_is_available($template)) {
            /* Create a reference to the current object the renderer property so
             * that templates can call methods on it */
            $this->smarty->assign_by_ref('renderer', $this);
            /* TODO: What to do with optional $params? */
            /* Return the rendered template */
            return SmartyTemplate::render();
        }
        /* Fallback to render methods */
        $render_method = 'render_' . str_replace('-', '_', $mode);
        if (!method_exists($this, $render_method)) {
            /* No way to render this object */
            trigger_error(sprintf('%s::%s(): No way to render this block using
						mode "%s", no template "%s" and no method "%s" found.', __CLASS__, __FUNCTION__, $mode, $template, $render_method), E_USER_ERROR);
        } else {
            /* Call the render method with appropriate parameters */
            if ($params) {
                return call_user_func_array(array(&$this, $render_method), $params);
            } else {
                return $this->{$render_method}();
            }
        }
    }
Esempio n. 2
0
<?php

require_once 'libs/SmartyTemplate.php';
$tpl = new SmartyTemplate();
$tpl->render('index', $data = ['name' => 'Patrício']);