Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Output filter on smarty v.5 doesn't running . #899

Closed
Factify opened this issue Sep 4, 2023 · 11 comments
Closed

Output filter on smarty v.5 doesn't running . #899

Factify opened this issue Sep 4, 2023 · 11 comments

Comments

@Factify
Copy link

Factify commented Sep 4, 2023

No description provided.

@wisskid
Copy link
Contributor

wisskid commented Sep 5, 2023

@Factify we could use a little bit more information. Maybe some sample code and the actual output vs expected output?

@wisskid wisskid added the waiting Waiting for answer label Sep 5, 2023
@Factify
Copy link
Author

Factify commented Sep 7, 2023

// load extension smarty class
require_once('mySmarty.extension.php');

use \Smarty{Smarty, Template};
use Factify\Classes{Hook, RunStatic};

class mySmarty extends Smarty {

/** __construct **/
public function __construct() {
    parent::__construct();

	
	$this->compile_check = false; 
    $this->force_compile = false; 
	
	/* The Smarty delimiter tags { and } will be ignored so long as they are surrounded
	by white space. This behavior can be disabled by setting auto_literal to false. */
	$this->auto_literal = true;
	
    // set compile root
	$this->setCompileDir('compile_tpl');
	
    // set root tpl||components
	$tempDirs['FTL'] = 'template_dir_1'; 
	$tempDirs['STL'] = 'template_dir_2'; 
	$tempDirs['MTL'] = 'template_dir_3'; 

	// set cache root
    $this->cache_dir = 'cache_tpl'; 
	
	// set configs root array
	$this->setConfigDir('config_tpl');
	
	// set Template Dir array
	$this->setTemplateDir($tempDirs);

	
    // Prefilter Before Compiled
	$this->registerFilter('pre', [$this, 'runFilterPre']);
	
	// outputfilter asfter Compiled 
	$this->registerFilter('output', [$this, 'runFilterOutput']);
	
	//#TDOD Handling missing templates || OLD => $this->_smarty_include
	$this->registerDefaultTemplateHandler([$this, 'handleMissingTemplate']);

    // add Extension
	$this->addExtension(new mySmartyExtension($this));

	// testInstall 
	//if(true === SD_DEBUG) $this->testInstall();
}

/*** Get Smarty object ***
* --> @return Smarty **/
//public function getSmarty() { return $this; }


public function ifExistRegistered($type = null, $name = null) {
	return (($type && $name) && isset($this->registered_plugins[$type][$name]));
}

public function handleMissingTemplate($type, $name, &$content, &$modified, Smarty $smarty) {		
	// return blank tpl
	return SD_TPL_ROOT_G . 'blank.tpl';
}
	
public function repFileName($name_file = '') {
	if(str_contains($name_file, ':')){
		if (($pos = strpos($name_file, ']')) !== false) $name_file = substr($name_file, $pos+1);
		if (($pos = strpos($name_file, ':')) !== false) $name_file = substr($name_file, $pos+1);
	}
	return $name_file;
}

/** ## Prefilter Before Compiled smarty output
 * @param string compiled content
 * --> @return string - transformed content **/
public function runFilterPre($tpl_output, Template $_template) {
	$file_dir = $_template->template_resource;
	if(!str_ends_with($file_dir, '.tpl')) return $tpl_output;
	
	//Remove HTML Comments
	$tpl_output = preg_replace('/<!--(.|\s)*?-->/', '', $tpl_output);
	
	$name_file = $this->repFileName($file_dir);
    Hook::load('smartyPreFetchHook', $tpl_output, $name_file);
	
	//@todo 
	$tpl_output = str_replace('xajax_', _PERFIX_REACT, $tpl_output);
	
	return $tpl_output;
}

/** ## transforms compiled smarty output
 * @param string compiled content
 * --> @return string - transformed content **/
public function runFilterOutput($tpl_output, Template $_template) {
	$file_dir = $_template->template_resource;
	if(!str_ends_with($file_dir, '.tpl')) return $tpl_output;
	
	$name_file = $this->repFileName($file_dir);
	echo ' <br>runFilterOutput name: ' . $name_file; // only for test
    Hook::load('smartyFetchHook', $tpl_output, $name_file);
	
    // static data class parser
    $tpl_output = RunStatic::collectJSDynamicCode($tpl_output, $name_file);			
	
	return $tpl_output;
}

}

//*** my mySmartyExtension file ***//
use Smarty\Extension\Base;

class mySmartyExtension extends Base {

/** __construct **/
public function __construct() {}

	
public function getModifierCallback(string $modifierName) {
	if(is_callable($modifierName)) return $modifierName;
	//#|TODO if(isset($this->functionCallback[$modifierName])) return $this->functionCallback[$modifierName];
	
	switch ($modifierName) {
		// #TODO
	}
	//if(is_callable($modifierName)) return $modifierName;
    return null;
}

}

@wisskid
Copy link
Contributor

wisskid commented Sep 11, 2023

@Factify found it, thanks for your bug report!

@Factify
Copy link
Author

Factify commented Sep 12, 2023

@wisskid I have tested, working only in fetch not on index.

@wisskid
Copy link
Contributor

wisskid commented Sep 13, 2023

Do you mean $smarty->display()?

@Factify
Copy link
Author

Factify commented Sep 13, 2023

@wisskid yes display.

@wisskid
Copy link
Contributor

wisskid commented Sep 13, 2023

There should be no difference between the two. Except maybe for already cached output. Can you try to empty your caches?

@Factify
Copy link
Author

Factify commented Sep 13, 2023

@wisskid I don't used the cache.

@wisskid
Copy link
Contributor

wisskid commented Sep 13, 2023

Strange. I've tested (and just retested) it using both $smarty->display('issue899.tpl'); and echo $smarty->fetch('issue899.tpl');. Result is the same, output filter is running perfectly. Please check if the error isn't in your own code, e.g. in the if(!str_ends_with($file_dir, '.tpl')) return $tpl_output; part.

If you're sure it's in Smarty, please provide a clear reproduction scenario. This issue is a great example on how to provide a reproduction scenario that will help us find the problem.

@Factify
Copy link
Author

Factify commented Sep 13, 2023

'public function runFilterOutput($tpl_output, Template $_template) {
$file_dir = $_template->template_resource;
if(!str_ends_with($file_dir, '.tpl')) return $tpl_output;

echo ' <br>runFilterOutput name: ' . $file_dir; // only for test	

return $tpl_output;

}'

@Factify
Copy link
Author

Factify commented Sep 13, 2023

@wisskid Now in my project I have smarty version 4.3.2 .output filter is OK.

@wisskid wisskid added needs-repro and removed waiting Waiting for answer labels Sep 15, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants