php - How can i add smartyBc to slim framework -
i writing simple slim app , write raw php mixed smarty syntax keep getting following error.
syntax error in template "./templates/application/header.tpl" on line 106 "{php}" unknown tag "php"
if change tpl file {include_php}
this
{include_php} <?php echo 'home ' ?> {/include_php}
i following error ,
type: smartyexception message: {include_php} deprecated, use smartybc class enable file: /home/ramza/apps/php/company/smarty/sysplugins/smarty_internal_compile_include_php.php line: 52
my slim index.php below :
<?php session_cache_limiter(false); session_start(); require 'vendor/autoload.php'; require 'vendor/php-activerecord/activerecord.php'; require_once('smarty/smartybc.class.php'); activerecord\config::initialize(function($cfg) { $cfg->set_model_directory('models'); $cfg->set_connections(array( 'development' => 'mysql://root:@localhost/xxx' )); }); require 'models/ocmember.php'; $app = new \slim\slim(array( 'view' => new \slim\views\smarty() )); $view = $app->view(); $view->parserdirectory = dirname(__file__) . 'smarty'; $view->parsercompiledirectory = dirname(__file__) . '/compiled'; $view->parsercachedirectory = dirname(__file__) . '/cache'; $view->parserextensions = array( dirname(__file__) . '/vendor/slim/views/slim/views/smartyplugins', ); $app->add(new \slim\middleware\sessioncookie(array( 'expires' => '20 minutes', 'path' => '/', 'domain' => null, 'secure' => false, 'httponly' => false, 'name' => 'slim_session', 'secret' => '1e12350dc6629a44df6b6771b6cb0cabd153aadc91f6af2697d371294296f69866b7c729a58b82108770ab02b087fda821af4533fe85b8115504ed9084055d77', 'cipher' => mcrypt_rijndael_256, 'cipher_mode' => mcrypt_mode_cbc ))); $authenticate = function ($app) { return function () use ($app) { if (!isset($_session['company_id'])) { $_session['urlredirect'] = $app->request()->getpathinfo(); $app->flash('error', 'login required'); $app->redirect('/company/login'); } }; }; $app->hook('slim.before.dispatch', function() use ($app) { $current_company = null; if (isset($_session['company_id'])) { $current_company = company::find($_session['company_id']); } $app->view()->setdata('current_company', $current_company); }); require 'app/controllers/before_filters.php'; require 'app/controllers/members.php'; require 'app/controllers/login.php'; require 'app/controllers/registration.php'; #require 'app/controllers/category_menu.php'; $app->run();
you shouldn't mix php smarty should need in php , assign value smarty. won't need use {php} tag @ all. smarty displaying data , mixing php smarty makes using smarty doesn't make sense.
however if need use php tag in smarty template should change:
$app = new \slim\slim(array( 'view' => new \slim\views\smarty() ));
into
$app = new \slim\slim(array( 'view' => new \slim\views\smartybc() ));
but don't recommend using php inside smarty templates
edit - smartybc view doesn't exists in slim framework have create it
although i've never used slim framework i've decided further. smartybc doesn't exists in slim views, have manually create it.
in slim\views directory have create smartybc.php file content:
<?php /** * slim - micro php 5 framework * * @author josh lockhart * @author andrew smith * @link http://www.slimframework.com * @copyright 2013 josh lockhart * @version 0.1.2 * @package slimviews * * mit license * * permission hereby granted, free of charge, person obtaining * copy of software , associated documentation files (the * "software"), deal in software without restriction, including * without limitation rights use, copy, modify, merge, publish, * distribute, sublicense, and/or sell copies of software, , * permit persons whom software furnished so, subject * following conditions: * * above copyright notice , permission notice shall * included in copies or substantial portions of software. * * software provided "as is", without warranty of kind, * express or implied, including not limited warranties of * merchantability, fitness particular purpose , * noninfringement. in no event shall authors or copyright holders * liable claim, damages or other liability, whether in action * of contract, tort or otherwise, arising from, out of or in connection * software or use or other dealings in software. */ namespace slim\views; /** * smartyview * * smartyview custom view class renders templates using smarty * template language (http://www.smarty.net). * * 2 fields you, developer, need change are: * - parserdirectory * - parsercompiledirectory * - parsercachedirectory * * @package slim * @author jose da silva <http://josedasilva.net> */ class smartybc extends \slim\view { /** * @var string path smarty code directory without trailing slash */ public $parserdirectory = null; /** * @var string path smarty compiled templates folder without trailing slash */ public $parsercompiledirectory = null; /** * @var string path smarty cache folder without trailing slash */ public $parsercachedirectory = null; /** * @var smartyextensions smarty extensions directory want load plugins */ public $parserextensions = array(); /** * @var parserinstance persistent instance of parser object. */ private $parserinstance = null; /** * render template * * method output rendered template content * * @param string $template path template, relative templates directory. * @param null $data * @return string */ public function render($template, $data = null) { $parser = $this->getinstance(); $parser->assign($this->all()); return $parser->fetch($template, $data); } /** * creates new smarty object instance if doesn't exist, , returns it. * * @throws \runtimeexception if smarty lib directory not exist * @return \smarty instance */ public function getinstance() { if (! ($this->parserinstance instanceof \smartybc)) { if (!class_exists('\smartybc')) { if (!is_dir($this->parserdirectory)) { throw new \runtimeexception('cannot set smartybc lib directory : ' . $this->parserdirectory . '. directory not exist.'); } require_once $this->parserdirectory . '/smartybc.class.php'; } $this->parserinstance = new \smartybc(); $this->parserinstance->template_dir = $this->gettemplatesdirectory(); if ($this->parserextensions) { $this->parserinstance->addpluginsdir($this->parserextensions); } if ($this->parsercompiledirectory) { $this->parserinstance->compile_dir = $this->parsercompiledirectory; } if ($this->parsercachedirectory) { $this->parserinstance->cache_dir = $this->parsercachedirectory; } } return $this->parserinstance; } }
in fact simple have copy content smarty.php view , change smarty smartybc. that's it!
the index.php should like:
<?php require 'slim/slim.php'; \slim\slim::registerautoloader(); $app = new \slim\slim(array ( 'view' => new \slim\views\smartybc() ) ); $view = $app->view(); $view->parserdirectory = dirname(__file__) . '/smarty'; $view->parsercompiledirectory = dirname(__file__) . '/compiled'; $view->parsercachedirectory = dirname(__file__) . '/cache'; $view->settemplatesdirectory(dirname(__file__) . '/smarty/templates'); $app->get( '/', function () use ($app) { $app->render('index.tpl', array('test' => 'user')); } ); $app->post( '/post', function () { echo 'this post route'; } ); $app->put( '/put', function () { echo 'this put route'; } ); $app->patch('/patch', function () { echo 'this patch route'; }); $app->delete( '/delete', function () { echo 'this delete route'; } ); $app->run();
you don't have include here manually smartybc.class.php did in code. of course sample , minimal code (used slim framework , added smarty view).
template file:
{$test}, hello world , {php} echo "hello world php"; {/php}
output is:
user, hello world , hello world php
so wanted achieve.
however remind that's not best way mix php templates. in smarty documentation can read:
{php} tags deprecated smarty, , should not used. put php logic in php scripts or plugin functions instead.
Comments
Post a Comment