View previous topic :: View next topic |
Author |
Message |
Eughen Smarty n00b
Joined: 28 Jun 2018 Posts: 4
|
Posted: Thu Jun 28, 2018 12:53 pm Post subject: convert variable on echo |
|
|
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 |
|
AnrDaemon Administrator
Joined: 03 Dec 2012 Posts: 1785
|
Posted: Thu Jun 28, 2018 3:58 pm Post subject: |
|
|
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 |
|
Eughen Smarty n00b
Joined: 28 Jun 2018 Posts: 4
|
Posted: Fri Jun 29, 2018 11:19 am Post subject: |
|
|
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 |
|
Back to top |
|
AnrDaemon Administrator
Joined: 03 Dec 2012 Posts: 1785
|
Posted: Fri Jun 29, 2018 5:19 pm Post subject: |
|
|
Use objects instead of plain variables, with __toString method tailored to your preferences. |
|
Back to top |
|
AnrDaemon Administrator
Joined: 03 Dec 2012 Posts: 1785
|
Posted: Fri Jun 29, 2018 5:21 pm Post subject: |
|
|
Eughen wrote: | I cant relay on front-end developers skills, most of them cant even read FAQ and Template Development Guide |
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 |
|
Eughen Smarty n00b
Joined: 28 Jun 2018 Posts: 4
|
Posted: Sat Jun 30, 2018 9:44 am Post subject: |
|
|
Quote: | you better disband your team and hire a new one. |
i cant disband top managers to stop hiring low skill front-end developers
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 |
|
AnrDaemon Administrator
Joined: 03 Dec 2012 Posts: 1785
|
Posted: Sat Jun 30, 2018 11:07 am Post subject: |
|
|
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 |
|
Eughen Smarty n00b
Joined: 28 Jun 2018 Posts: 4
|
Posted: Sat Jun 30, 2018 5:48 pm Post subject: |
|
|
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 |
|
AnrDaemon Administrator
Joined: 03 Dec 2012 Posts: 1785
|
Posted: Sun Jul 01, 2018 9:59 am Post subject: |
|
|
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 |
|
|