View previous topic :: View next topic |
Author |
Message |
JBJHJM Smarty Rookie
Joined: 14 Mar 2011 Posts: 12
|
Posted: Fri Jul 08, 2011 9:11 pm Post subject: template validation |
|
|
Hi everybody!
I'm implementing Smarty into an own CMS - working great so far - but there's one important thing I have to correct. I'll give you a short example:
-> an user edits a smarty template
-> it is saved into database
-> but it contains an error.
So it would be possible to catch that error somewhere but that's not what I want to do. So my question is does Smarty have some kind of function which can directly tell me if the template (saved in string variable) is valid or if it contains errors, before it is saved and used somewhere?
thx |
|
Back to top |
|
rodneyrehm Administrator
Joined: 30 Mar 2007 Posts: 674 Location: Germany, border to Switzerland
|
Posted: Sat Jul 09, 2011 9:05 am Post subject: |
|
|
To validate a template just use the eval: resource:
Code: | try {
$smarty->fetch('eval:'. $templateString);
// everything is fine…
} catch(SmartyException $e) {
// error occured
} |
|
|
Back to top |
|
JBJHJM Smarty Rookie
Joined: 14 Mar 2011 Posts: 12
|
Posted: Sat Jul 09, 2011 10:23 am Post subject: |
|
|
Thanks, just wanted to know if I have to use/catch an exception to validate or if there already is a 'template debugging' function. |
|
Back to top |
|
rodneyrehm Administrator
Joined: 30 Mar 2007 Posts: 674 Location: Germany, border to Switzerland
|
Posted: Sat Jul 09, 2011 10:38 am Post subject: |
|
|
well, you could initialize the compiler directly - but you'd still have to work with a try/catch block. |
|
Back to top |
|
Veselin Smarty n00b
Joined: 20 May 2014 Posts: 4
|
Posted: Tue May 20, 2014 6:59 am Post subject: |
|
|
I've the same problem.I need to validate user input for it is a valid smarty template and I'm searching for solution 2 days already with no result...your solution do not work either.
Here is the code:
protected function CheckForInvalidSmartySymbols($syntax = 'asd{&&}') {
//$syntax = '{if $p && $q}1{/if}';
//$syntax = '{if 1 == 1}g {/if}';
//$syntax = '{foreach from=a item=$b}{/foreach}';
$templateFile = '/mnt/home/veselin/vescoTest.tpl';
file_put_contents($templateFile,$syntax);
$template = new NitroTemplate('file:'.$templateFile);
$smarty = $template->Engine;
try {
$smarty->fetch('eval:',$templateFile);
// everything is fine…
} catch(SmartyException $e) {
FB::log('smarty Error');
}
}
With this "eval" fetch I get this in the console:
"E_USER_WARNING: Smarty error: unable to read resource: "eval:" "
Without "eval" in the fetch I get this in the console:
"E_USER_ERROR: Smarty error: [in /mnt/home/veselin/vescoTest.tpl line 1]: syntax error: unrecognized tag: && "
But I can not catch this in the try/catch block
Please not the test case :
$syntax = '{foreach from=a item=$b}{/foreach}';
this breaks the entire application with a error in the console:
"syntax error, unexpected T_ENDFOREACH"
So my question is:
Is there any way to validate is what the user send as post is a valid smarty template before this get into the database,because if it's not later on this will be used and application will fail
Thanks in advance |
|
Back to top |
|
U.Tews Administrator
Joined: 22 Nov 2006 Posts: 5068 Location: Hamburg / Germany
|
Posted: Tue May 20, 2014 10:16 am Post subject: |
|
|
It looks like that you are using Smarty 2.
Smarty2 does not throw exceptions, so you can't catch errors.
Also Smarty2 does not support the 'eval:' resource.
The examples given have been for Smarty3. |
|
Back to top |
|
Veselin Smarty n00b
Joined: 20 May 2014 Posts: 4
|
Posted: Tue May 20, 2014 11:41 am Post subject: |
|
|
Ok
Can you give me solution in my case ,please
Thanks |
|
Back to top |
|
U.Tews Administrator
Joined: 22 Nov 2006 Posts: 5068 Location: Hamburg / Germany
|
|
Back to top |
|
Veselin Smarty n00b
Joined: 20 May 2014 Posts: 4
|
Posted: Tue May 20, 2014 12:41 pm Post subject: |
|
|
If I have such a input :
$syntax = '{foreach from=a item=$b}{/foreach}';
the site will be down ,because of the syntax error smarty produce:
"syntax error, unexpected T_ENDFOREACH("
seems like you can not process syntax or fatal errors with error handlers |
|
Back to top |
|
U.Tews Administrator
Joined: 22 Nov 2006 Posts: 5068 Location: Hamburg / Germany
|
Posted: Tue May 20, 2014 1:56 pm Post subject: |
|
|
That error message is strange. It's not generated by the compiler, but is a fatal PHP error.
Did you get some information where it did occur? |
|
Back to top |
|
Veselin Smarty n00b
Joined: 20 May 2014 Posts: 4
|
Posted: Thu May 22, 2014 7:46 am Post subject: |
|
|
So,I think I found out a solution.Here is the code:
restore_error_handler();
error_reporting(E_ALL);
ini_set('display_errors', 1);
$syntax = $_POST['smartySyntax'];
CheckForInvalidSmartySymbols($syntax);
echo 'valid';
set_error_handler('MyErrorHandler');
function CheckForInvalidSmartySymbols($syntax) {
$time = time();
$templateFile = 'someTempTemplate.tpl'
file_put_contents($templateFile,$syntax);
$template = new NitroTemplate('file:'.$templateFile);
$smarty = $template->Engine;
$smarty->fetch($templateFile);
@unlink($templateFile);
}
function MyErrorHandler(){
}
This script is called in ajax mode.
The idea is :
Capsulate all error in customer handler that is a empty function (do nothing)
According to php documentation you can NOT customize parsing and fatal errors.
These are the errors smarty->fetch will produce if the tempalte is wrong.
If I receive something different from "valid" string...obviously smarty return parsing error ...the template is not valid...for me this validation should be good enough |
|
Back to top |
|
|