View previous topic :: View next topic |
Author |
Message |
robregonm Smarty Rookie
Joined: 21 Oct 2004 Posts: 6 Location: Bucaramanga, Santander, Colombia
|
Posted: Thu Oct 21, 2004 4:57 pm Post subject: Blocks don't work with PHP 5 Classes? |
|
|
Hi everybody...
I've designed some functions and they work very good , but now I designed a block and it's not working , I thought I was programming something bad, but I tested default Smarty blocks like textformat, and they don't work... And now... no idea... The only thing I know is $content variable has not been passed... it's empty... the NULL value is passed OK to $content var,... $params var is passed OK too... Am I doing something wrong?... Perhaps Some config is needed I forgot?
I'm using PHP 5.0.2, Smarty 2.6.6....
Doesn't Smarty work with PHP 5?, just PHP 4?
Thanx 4 all
Ricky
Last edited by robregonm on Thu Oct 21, 2004 8:43 pm; edited 1 time in total |
|
Back to top |
|
boots Administrator
Joined: 16 Apr 2003 Posts: 5611 Location: Toronto, Canada
|
Posted: Thu Oct 21, 2004 5:13 pm Post subject: |
|
|
Hi.
Smarty is known to work with PHP5.
Can you please post pertinent info, perhaps a reproducing case? Do you have error reporting at E_ALL? Very importantly, did you clear your cache/template_c directories during the upgrade from PHP4 to PHP5? |
|
Back to top |
|
robregonm Smarty Rookie
Joined: 21 Oct 2004 Posts: 6 Location: Bucaramanga, Santander, Colombia
|
Posted: Thu Oct 21, 2004 5:31 pm Post subject: |
|
|
Hi....
I cleaned the cache...(all *_c/* files)
error_reporting(E_ALL);
The $params and &$smarty variables are passed Ok... But $content isn't.
I've tested it a lot...
This is my source code... well a part:
Quote: |
function smarty_block_myblock($params, $content, &$smarty, &$repeat)
{
if (is_null($content)) {
return;
}else{
return($content);
}
}
|
$content the first time is NULL (obvious), second is empty... |
|
Back to top |
|
boots Administrator
Joined: 16 Apr 2003 Posts: 5611 Location: Toronto, Canada
|
Posted: Thu Oct 21, 2004 5:40 pm Post subject: |
|
|
I take it that you verified that the template block has content that evaluates to something other than empty?
eg, the following won't do much if $my_empty_var is empty:
Code: | {myblock}
{$my_empty_var}
{/myblock} |
|
|
Back to top |
|
robregonm Smarty Rookie
Joined: 21 Oct 2004 Posts: 6 Location: Bucaramanga, Santander, Colombia
|
Posted: Thu Oct 21, 2004 6:02 pm Post subject: |
|
|
Nope...
My template looks like this:
Quote: |
{myblock}
This is my block...
I don't know why this is not passed to $content variable
{/myblock}
|
|
|
Back to top |
|
messju Administrator
Joined: 16 Apr 2003 Posts: 3336 Location: Oldenburg, Germany
|
Posted: Thu Oct 21, 2004 6:15 pm Post subject: |
|
|
the example works fine for me with 5.0.2-cgi and Smarty-2.6.6 |
|
Back to top |
|
robregonm Smarty Rookie
Joined: 21 Oct 2004 Posts: 6 Location: Bucaramanga, Santander, Colombia
|
Posted: Thu Oct 21, 2004 8:42 pm Post subject: |
|
|
When Smarty class is called directly everything is OK, but When I have a inherited class, ... maybe That's the reason my block doesn't work... My question now is: So What's wrong...
My class is like this:
Code: |
class Template extends Smarty {
private $Properties = array("TemplateName" => "", "Theme" => "");
function __get($prop)
{
if(isset($this->Properties[$prop])) {
return $this->Properties[$prop];
}else{
return false;
}
}
function __set($prop,$val)
{
if(isset($this->Properties[$prop])){
switch($prop){
case 'TemplateName':
if($this->IsTemplate($val)){
global $server_vars;
$this->template_dir = dsTemplates . "$val/";
$server_vars['DOCUMENT_ROOT'] = $this->template_dir;
$_SERVER['DOCUMENT_ROOT'] = $this->template_dir;
$this->Properties[$prop] = $val;
$this->assign($prop, dsTemplates . "$val");
}
break;
case 'Theme':
if($this->IsTheme($this->TemplateName, $val)){
$this->Properties[$prop] = $val;
$this->assign($prop, $val);
}
break;
}
}else{
return false;
}
}
function __construct($templateName = "default", $themeName = "MyTheme") {
$this->Smarty();
//$this->default_template_handler_func = '___no_template';
$this->TemplateName = $templateName;
$this->Theme = $themeName;
$this->compile_dir = "templates_c/";
$this->config_dir = 'configs/';
$this->cache_dir = 'cache/';
$this->caching = false;
global $dsconfig;
$this->compile_check = $dsconfig['compile_check'];
$this->debugging = $dsconfig['debug'];
}
function Show($resourceName = 'index.tpl'){
if(($this->TemplateName != '')and ($this->Theme != '')){
$this->display($resourceName);
}else{
echo('Template or Theme not selected');
}
}
public function getTemplates()
{
$handle=opendir(dsTemplates);
while ($file = readdir($handle)){
if($file != "." && $file != ".." && $file != "_global" && $file != "/"){
if ($this->IsTemplate($file)){
$dirlist[] = $file;
}
}
}
closedir($handle);
return $dirlist;
}
public function getThemes($templateName = 'default')
{
$ThemeDir = dsTemplates.$templateName.dsThemes;
$handle=opendir($ThemeDir);
while ($file = readdir($handle)){
if($file != "." && $file != ".." && $file != "_global" && $file != "/"){
if($this->IsTheme($templateName, $file)){
$dirlist[] = $file;
}
}
}
closedir($handle);
return $dirlist;
}
public function IsTemplate($templateName)
{
return is_readable(dsTemplates.$templateName."/template.php") && is_readable(dsTemplates.$templateName."/style.css.tpl") && is_readable(dsTemplates.$templateName."/index.tpl");
}
public function IsTheme($templateName, $themeName){
if($this->IsTemplate($templateName)){
return is_readable(dsTemplates.$templateName.dsThemes.$themeName."/core.php" /* && is_readable($ThemeDir.$file."/style.css")*/);
}
}
}
|
This code is not so difficult but it's the first time I make a PHP 5 class...
I use the class like this:
Code: |
$template = new Template;
$template->Show("index.tpl", "MyTheme");
|
Basically this is my class source code...
Can anybody help me?
Am I doing something wrong? |
|
Back to top |
|
robregonm Smarty Rookie
Joined: 21 Oct 2004 Posts: 6 Location: Bucaramanga, Santander, Colombia
|
Posted: Fri Oct 22, 2004 7:49 pm Post subject: |
|
|
I've decided for now... rewrite my code replacing the class in standard functions.. ...
I hope to find soon a solution... I think is better if OOP.
If anybody know a solution please email me: ricky_obregon@hotmail.com
|
|
Back to top |
|
boots Administrator
Joined: 16 Apr 2003 Posts: 5611 Location: Toronto, Canada
|
Posted: Sat Oct 23, 2004 12:32 am Post subject: |
|
|
I don't know the problem but considering that Smarty is designed for PHP4, I suspect it is not the best idea to extend it using PHP5 features (particularly __get and __set)
Just a thought. |
|
Back to top |
|
messju Administrator
Joined: 16 Apr 2003 Posts: 3336 Location: Oldenburg, Germany
|
Posted: Sat Oct 23, 2004 2:42 pm Post subject: |
|
|
hmm hmm hmm.
i have two different (and mutual exclusive) fixes to offer:
#1
Code: | Index: Smarty.class.php
===================================================================
RCS file: /repository/smarty/libs/Smarty.class.php,v
retrieving revision 1.504
diff -u -r1.504 Smarty.class.php
--- Smarty.class.php 1 Oct 2004 15:26:44 -0000 1.504
+++ Smarty.class.php 23 Oct 2004 14:37:07 -0000
@@ -561,6 +561,13 @@
*/
var $_cache_including = false;
+ /**
+ * used when handling block-plugins to store template contents
+ *
+ * @var string
+ */
+ var $_block_content = null;
+
/**#@-*/
/**
* The class constructor.
|
#2
Code: | Index: Smarty_Compiler.class.php
===================================================================
RCS file: /repository/smarty/libs/Smarty_Compiler.class.php,v
retrieving revision 1.350
diff -u -r1.350 Smarty_Compiler.class.php
--- Smarty_Compiler.class.php 1 Oct 2004 15:26:44 -0000 1.350
+++ Smarty_Compiler.class.php 23 Oct 2004 14:40:19 -0000
@@ -716,8 +716,8 @@
$output .= $this->_compile_plugin_call('block', $tag_command).'($this->_tag_stack[count($this->_tag_stack)-1][1], null, $this, $_block_repeat=true);';
$output .= 'while ($_block_repeat) { ob_start(); ?>';
} else {
- $output = '<?php $this->_block_content = ob_get_contents(); ob_end_clean(); ';
- $_out_tag_text = $this->_compile_plugin_call('block', $tag_command).'($this->_tag_stack[count($this->_tag_stack)-1][1], $this->_block_content, $this, $_block_repeat=false)';
+ $output = '<?php $_block_content = ob_get_contents(); ob_end_clean(); ';
+ $_out_tag_text = $this->_compile_plugin_call('block', $tag_command).'($this->_tag_stack[count($this->_tag_stack)-1][1], $_block_content, $this, $_block_repeat=false)';
if ($tag_modifier != '') {
$this->_parse_modifiers($_out_tag_text, $tag_modifier);
}
|
I'd vote for #2. i really don't know why the block-contents are stored as an object-property and not as a local variable. they are used nowhere except in to call to the closing block. |
|
Back to top |
|
robregonm Smarty Rookie
Joined: 21 Oct 2004 Posts: 6 Location: Bucaramanga, Santander, Colombia
|
Posted: Mon Oct 25, 2004 3:10 pm Post subject: |
|
|
Thanx a lot...
It's working now...
I used the #2...
Thanx 4 all.... |
|
Back to top |
|
|