|
Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon. |
|
View previous topic :: View next topic |
Author |
Message |
TakingSides Smarty Rookie
Joined: 11 Dec 2010 Posts: 30
|
Posted: Mon Sep 26, 2011 4:28 pm Post subject: Issue with parsing variables |
|
|
(see comment because i cannot post my thread here without a post doesn't exist error) |
|
Back to top |
|
TakingSides Smarty Rookie
Joined: 11 Dec 2010 Posts: 30
|
Posted: Mon Sep 26, 2011 4:31 pm Post subject: |
|
|
Good Afternoon,
Just before I explain my problem with example code, allow me to briefly explain the file structure I currently have in place
../lib/_classes -> All Framework classes are here
../lib/_controllers -> All Framework controllers are here
../lib/_models -> All Framework models are here
../lib/_widgets -> All Widgets/site elements are here
../lib/Facebook -> Facebook library
../lib/Smarty -> Smarty library
Okay, so I have created a plug-in inside the ./Smarty/plugins/ directory, called widget (function.widget.php) this will basically look for a controller.php inside the ./lib/_widgets/<name>/controller.php and create an instance of this item.
My problem is when i parse the $smarty object into the Widget and then set variables, they appear in the list. When i display my view.tpl the variables are blank.
The Process: HTTPD -> Controller -> View -> ((Widget => Controller -> View))
Please see the source code below for further examples...
./themes/default/home/index.tpl
Code: | {include file='../ob/inc.head.tpl' script_title="__TITLE__ | `$smarty.const.CONF_APP_NAME`" keywords='' description=''}
<div id="body">
<div id="leftcol">
<div class="lefttop"></div>
<div class="leftshadow">
<div class="left">
<h1 title="## TTL_HOME_WELCOME ##">## TTL_HOME_WELCOME ##</h1>
<p>## TXT_DEV_COMING_SOON ##</p>
{widget name='Test' param1='hello' param2='world'}
</div>
</div>
<div class="leftbtm"></div>
</div>
<div class="rightcol">
{include file='../ob/inc.blog-categories.tpl'}
</div>
<div class="rightcol">
{include file='../ob/inc.right.tpl'}
</div>
<div class="end"></div>
</div>
{include file='../ob/inc.footer.tpl'} |
./lib/Smarty/plugins/function.widget.php
Code: | <?php
function smarty_function_widget($params, $smarty) {
if (empty($params['name']))
user_error('Widget is missing name.', E_USER_ERROR);
if (!file_exists($controller = sprintf('%s/controller.php', CONF_DIR_WIDGETS . strtolower($params['name']))))
user_error(sprintf('Cannot allocate widget (%s) controller.', $params['name']), E_USER_ERROR);
require_once ($controller);
$widget = call_user_func($params['name'] . '::factory', $params);
$widget->loadView($smarty)->execute();
} |
../lib/_classes/class.widget.php
Code: | <?php
class Widget {
protected $smarty;
private $_vars = array();
final public static function factory($params = array()) {
$className = get_called_class();
return new $className($params);
}
final private function __construct($params) {
$this->_vars = $params;
unset($params, $this->_vars['name']);
}
final public function loadView($smarty) {
$this->smarty = $smarty;
$this->smarty->assign($this->_vars); // when var dump here, this returns the vars i require
return $this;
}
final public function display() {
$className = strtolower(get_called_class());
var_dump($this->smarty->getTemplateVars()); // this returns the vars i require!
$this->smarty->display(CONF_DIR_WIDGETS . $className . '/view');
}
public function execute() {
return $this->display();
}
} |
../lib/_widgets/test/controller.php
Code: | <?php
class Test extends Widget {} |
../lib/_widgets/test/view.tpl
Code: | <div style="background: #f00; padding: 10px;">
{$param1} __ {$param2} <!-- this is where the vars do not appear at all! -->
</div> |
If anyone could help i'd much appreciate it. |
|
Back to top |
|
U.Tews Administrator
Joined: 22 Nov 2006 Posts: 5068 Location: Hamburg / Germany
|
Posted: Mon Sep 26, 2011 5:49 pm Post subject: |
|
|
Note that in Smarty 3 the second parameter of plugins is no longer the Smarty object but the template object.
Code: | <?php
function smarty_function_widget($params, $template) {
if (empty($params['name']))
user_error('Widget is missing name.', E_USER_ERROR);
if (!file_exists($controller = sprintf('%s/controller.php', CONF_DIR_WIDGETS . strtolower($params['name']))))
user_error(sprintf('Cannot allocate widget (%s) controller.', $params['name']), E_USER_ERROR);
require_once ($controller);
$widget = call_user_func($params['name'] . '::factory', $params);
$widget->loadView($template->smarty)->execute();
} |
Not 100% sure if this caused the problem.
I think
$this->smarty->display(CONF_DIR_WIDGETS . $className . '/view');
is a typo and should be
$this->smarty->display(CONF_DIR_WIDGETS . $className . '/view.tpl'); |
|
Back to top |
|
TakingSides Smarty Rookie
Joined: 11 Dec 2010 Posts: 30
|
Posted: Mon Sep 26, 2011 7:11 pm Post subject: |
|
|
Thanks, I totally forget in Smarty 3, that $smarty (aka $template) was NOT the smarty object, but actually the template object (containing the smarty instance).
your solution was infact the answer to my problem, thanks.
Finally, I extended the Smarty class with my own Template class and overridden some methods such as the display to automatically append.tpl to the end of the templates (solely because i will NOT use php templates as i love the smarty syntax).
Cheers! |
|
Back to top |
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|