View previous topic :: View next topic |
Author |
Message |
SlowFox Smarty Regular
Joined: 02 Oct 2006 Posts: 55
|
Posted: Thu May 21, 2020 10:52 am Post subject: Access the Smarty-Parser |
|
|
As one of my project grows bigger I implement ways for automatic testing. In PHP I can use php-ast to parse my source files and e.g. check if classes, methods or constands do exist. In my Smarty-Templates I make extensive use of constants, but up to now I have way to test them upon they are hit while rendering the output.
For the moment template testing does only cover the compilation process:
Code: | try {
$tpl = $this->smarty->createTemplate($template, NULL, NULL, NULL, false);
$tpl->compileTemplateSource();
}
catch (\SmartyCompilerException $e) {
$errors->addErrorMessage(htmlspecialchars_decode($e->getMessage()));
}
|
Is there any way to improve this by extracting template tokens for further tests (mostly existance of constants - I am not sure if there is anything else suited for static checks)? |
|
Back to top |
|
AnrDaemon Administrator
Joined: 03 Dec 2012 Posts: 1785
|
Posted: Thu May 21, 2020 9:12 pm Post subject: |
|
|
That makes no sense.
Not even unit tests require that kind of introspection. |
|
Back to top |
|
SlowFox Smarty Regular
Joined: 02 Oct 2006 Posts: 55
|
Posted: Thu May 21, 2020 9:43 pm Post subject: |
|
|
AnrDaemon wrote: | That makes no sense.
Not even unit tests require that kind of introspection. |
Well, sense or not...
If somebody modifies a template (yes, there ARE client who may do that) and damages a class name or constant this can't be caught by a unit test and will result in a run-time error.
Every additional safety net will only make it better. |
|
Back to top |
|
AnrDaemon Administrator
Joined: 03 Dec 2012 Posts: 1785
|
Posted: Sat May 23, 2020 5:17 pm Post subject: |
|
|
That's not an area you should test. |
|
Back to top |
|
SlowFox Smarty Regular
Joined: 02 Oct 2006 Posts: 55
|
Posted: Sat May 23, 2020 6:04 pm Post subject: |
|
|
AnrDaemon wrote: | That's not an area you should test. |
Well, service is my success (and reduced service calls are my gain).
If it's not possible to get there: ok, I'll live with it, like I've done for the last decade.
Apart from that I prefer to choose the degree of caretaking by myself. Templates on my system are pretty nice (in my eyes), like:
Code: |
<div>{link page=\Page\User\Admin\Edit::class parameter=$user}Edit user{/link}</div>
|
Almost anybody can maintain that, but typos in the class name break things at run-time. It would be really easy to check, if I could get to the template tokens, so I thought I'll give it a try. |
|
Back to top |
|
AnrDaemon Administrator
Joined: 03 Dec 2012 Posts: 1785
|
Posted: Mon May 25, 2020 3:07 pm Post subject: |
|
|
Why your business logic sticking out in templates?
Wrap it into a presentation-safe function. |
|
Back to top |
|
AnrDaemon Administrator
Joined: 03 Dec 2012 Posts: 1785
|
Posted: Mon May 25, 2020 3:09 pm Post subject: |
|
|
Said that, if you are using modern enough PHP version, you could catch these errors as exceptions. |
|
Back to top |
|
SlowFox Smarty Regular
Joined: 02 Oct 2006 Posts: 55
|
Posted: Sat May 07, 2022 7:57 am Post subject: |
|
|
AnrDaemon wrote: | Why your business logic sticking out in templates?
Wrap it into a presentation-safe function. |
It is not a matter of business logic: In most cases it is just class constants being used to access array elements or make case distinctions. There may by typos.
But stupid me nevertheless - Smarty parser is the wrong place to look for this anyway. Just start the compiler and put the result into the existing PHP checker:
Code: | $tpl = $smarty->createTemplate($template, NULL, NULL, NULL, false);
$tpl->loadCompiler();
$phpString = $tpl->compiler->compileTemplate($tpl);
$errors = $ast->checkCode($phpString);
|
Voila, 26 new typos found :-) |
|
Back to top |
|
|