Jump to content

Please, don't convert $table to a string


Ilya Hoilik

Recommended Posts

Posted

Hello!

What's a reason to convert \IPS\Helpers\Table to a string? In some places I see something like this:

\IPS\Output::i()->output = (string) $table;

/* or */

\IPS\Output::i()->output = \IPS\Theme::i()->getTemplate( 'group' )->template( (string) $table );

Can you please avoid converting to a string? It will allow us to hook into a method and change something in the table without touching original code. So, for example, let's test this code:

 1506877232946.png.6ba55f98b2e70e3fba1e8f5f875c86ea.png

When you convert $table to a string we can do nothing with it:

1506877378208.thumb.png.66f18ecf406bfe52aed82750e912a9ed.png

When you don't convert it, then we can easy change something we need:

1506877613397.thumb.png.12e2b0a3f7fe345bb4f4430abd19dd4e.png

In the second case we stay original code untouched, that is better than replacing a whole method.

Posted

in your second example:

\IPS\Output::i()->output = \IPS\Theme::i()->getTemplate( 'group' )->template( (string) $table );

printing/echoing of \IPS\Output::i()->output will return a string any way, even if $table wasn't being casted to a string, cause the template would've done that if they did {$table|raw} inside the template. however your first one, i could see there isn't a need to cast to string.

however, i would ask, is there no other way to hook into what you need, before that point?

Posted

Add a new filter option and new fields to the advanced search

13 hours ago, CodingJungle said:

cause the template would've done that if they did {$table|raw} inside the template

But we can hook into that template and change $table in the template's PHP method :) 

Archived

This topic is now archived and is closed to further replies.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...