Smarty Forum Index Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon.

Access the Smarty-Parser

 
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> General
View previous topic :: View next topic  
Author Message
SlowFox
Smarty Regular


Joined: 02 Oct 2006
Posts: 55

PostPosted: Thu May 21, 2020 10:52 am    Post subject: Access the Smarty-Parser Reply with quote

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
View user's profile Send private message
AnrDaemon
Administrator


Joined: 03 Dec 2012
Posts: 1785

PostPosted: Thu May 21, 2020 9:12 pm    Post subject: Reply with quote

That makes no sense.
Not even unit tests require that kind of introspection.
Back to top
View user's profile Send private message
SlowFox
Smarty Regular


Joined: 02 Oct 2006
Posts: 55

PostPosted: Thu May 21, 2020 9:43 pm    Post subject: Reply with quote

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
View user's profile Send private message
AnrDaemon
Administrator


Joined: 03 Dec 2012
Posts: 1785

PostPosted: Sat May 23, 2020 5:17 pm    Post subject: Reply with quote

That's not an area you should test.
Back to top
View user's profile Send private message
SlowFox
Smarty Regular


Joined: 02 Oct 2006
Posts: 55

PostPosted: Sat May 23, 2020 6:04 pm    Post subject: Reply with quote

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
View user's profile Send private message
AnrDaemon
Administrator


Joined: 03 Dec 2012
Posts: 1785

PostPosted: Mon May 25, 2020 3:07 pm    Post subject: Reply with quote

Why your business logic sticking out in templates?
Wrap it into a presentation-safe function.
Back to top
View user's profile Send private message
AnrDaemon
Administrator


Joined: 03 Dec 2012
Posts: 1785

PostPosted: Mon May 25, 2020 3:09 pm    Post subject: Reply with quote

Said that, if you are using modern enough PHP version, you could catch these errors as exceptions.
Back to top
View user's profile Send private message
SlowFox
Smarty Regular


Joined: 02 Oct 2006
Posts: 55

PostPosted: Sat May 07, 2022 7:57 am    Post subject: Reply with quote

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
View user's profile Send private message
Display posts from previous:   
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> General All times are GMT
Page 1 of 1

 
Jump to:  
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
Protected by Anti-Spam ACP