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

convert variable on echo

 
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
Eughen
Smarty n00b


Joined: 28 Jun 2018
Posts: 4

PostPosted: Thu Jun 28, 2018 12:53 pm    Post subject: convert variable on echo Reply with quote

Hi, i`m working on CMS template engine using Smarty 3. And i need an advice on the best way to implement default format method. Now in our templates a lot of constructions like this:

Code:
{$string|escape:'html'}
{$number|cms_format_number}
{$date|cms_format_date}
{$form->getField('name')|cms_format_field}


so next steps is

Code:
{$any|cms_format_auto}


can i use this modifier by default? I think i can modify some "smarty_internal_compile" classes to generate
Code:
<?php echo cms_format_auto($_smarty_tpl->tpl_vars['form']->value->getField('name'));?>


instead of:
Code:
<?php echo $_smarty_tpl->tpl_vars['form']->value->getField('name');?>


the idea is to add default modifier, like this:
Code:
{$any} -> {$any|cms_format_auto}
{$any|somemod} -> {$any|somemod}
{func($any)} -> {func($any)|cms_format_auto}
{$any|raw} -> {$any}


is there some cases when/why i should not do such things? or maybe better way to implement it?
Back to top
View user's profile Send private message
AnrDaemon
Administrator


Joined: 03 Dec 2012
Posts: 1785

PostPosted: Thu Jun 28, 2018 3:58 pm    Post subject: Reply with quote

Quote:
Note

Although Smarty can handle some very complex expressions and syntax, it is a good rule of thumb to keep the template syntax minimal and focused on presentation. If you find your template syntax getting too complex, it may be a good idea to move the bits that do not deal explicitly with presentation to PHP by way of plugins or modifiers.


You can make any template transformation by the means of pre- or post-processing filters, but mind you, this is bad idea.
Don't overcomplicate your code.
Back to top
View user's profile Send private message
Eughen
Smarty n00b


Joined: 28 Jun 2018
Posts: 4

PostPosted: Fri Jun 29, 2018 11:19 am    Post subject: Reply with quote

Thanks! i think pre filter can do this. I know that its bad because of unexpected behavior. So i need a simple way to disable it inside of template, maybe some kind of tag or macros. Alternative is to add custom wrapper for date/form/field/input/image/entity/etc used in template to handle it. But this way it breaks backward compatibility and complicates debugging. I cant relay on front-end developers skills, most of them cant even read FAQ and Template Development Guide Sad
Back to top
View user's profile Send private message
AnrDaemon
Administrator


Joined: 03 Dec 2012
Posts: 1785

PostPosted: Fri Jun 29, 2018 5:19 pm    Post subject: Reply with quote

Use objects instead of plain variables, with __toString method tailored to your preferences.
Back to top
View user's profile Send private message
AnrDaemon
Administrator


Joined: 03 Dec 2012
Posts: 1785

PostPosted: Fri Jun 29, 2018 5:21 pm    Post subject: Reply with quote

Eughen wrote:
I cant relay on front-end developers skills, most of them cant even read FAQ and Template Development Guide Sad

Technical measures are never a solution to social issues.
If you can't communicate your needs inside your team, you better disband your team and hire a new one.
Back to top
View user's profile Send private message
Eughen
Smarty n00b


Joined: 28 Jun 2018
Posts: 4

PostPosted: Sat Jun 30, 2018 9:44 am    Post subject: Reply with quote

Quote:
you better disband your team and hire a new one.

i cant disband top managers to stop hiring low skill front-end developers Laughing

I`m already using Objects with __toString conversion and its one of the biggest problems cause of PHP silent crashes in "__toString" method and its hard to trace when crash occurs for like 1/10000 users on 1/1000 requests. To avoid this i need to manually render all objects using modifiers or functions.
Back to top
View user's profile Send private message
AnrDaemon
Administrator


Joined: 03 Dec 2012
Posts: 1785

PostPosted: Sat Jun 30, 2018 11:07 am    Post subject: Reply with quote

Well, why do you throw exception in __toString calls?
Also, if your HR hiring people without consulting with own specialists in the related department… Time to find a better job.
Back to top
View user's profile Send private message
Eughen
Smarty n00b


Joined: 28 Jun 2018
Posts: 4

PostPosted: Sat Jun 30, 2018 5:48 pm    Post subject: Reply with quote

you know, there is no need to write something like:
Code:
throw new Exception()

to have exception thrown. Its nice and simple solution to some object to generate:
Code:
<div>Hello world</div>
But if you working with deep structured elements like forms, lists, grids, you got hundredths of components in hierarchy and any of them can throw Exception of any kind. Even if u got try-catch blocks in rendering methods and unit tests for every component PHP will crush time to time. And simple modifier can save a lot of time by tracing exceptions, logging errors and displaying fallback messages.
Back to top
View user's profile Send private message
AnrDaemon
Administrator


Joined: 03 Dec 2012
Posts: 1785

PostPosted: Sun Jul 01, 2018 9:59 am    Post subject: Reply with quote

Then don't use __toString.
It's only applicable for simple objects.
If you look at my output code, it's 1/3 data retrieval, 1/3 registerPlugin() and the rest is assigning and fetching of intermediate templates (where registering a plugin is not necessarily the best option).
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