Jump to content

using PHP in template


demeto

Recommended Posts

Posted

I'm wondering how one would use php in a template, in version 3 it was just <php> </php>

Anyone have more info/ an article on changes on parsing etc?

 

this wont work:

{{echo "hello";}}

in fact if I do that nothing at all is displayed (encoding error) so it seems to break the site, although it is the way according to the docs: http://community.invisionpower.com/4docs/advanced-usage/development/template-logic-r73/

​.

This is correct .. it works for me .. {{echo "hello";}} .. will display hello in left top corner ..

 

.

Posted

I tried something that is close to what you do here, the only difference is I'm using a 3rd party framework.

Declaring:

{{\IPS\IPS::$PSR0Namespaces['Pheal'] = \IPS\ROOT_PATH . '/system/3rd_party/phealng/vendor/3rdpartyeve/phealng/lib/Pheal/';}}
{{use \Pheal\Pheal;}}

Throw errors :

Whoops \ Exception \ ErrorException (E_PARSE)

syntax error, unexpected 'use' (T_USE)

Posted

I tried something that is close to what you do here, the only difference is I'm using a 3rd party framework.

Declaring:

{{\IPS\IPS::$PSR0Namespaces['Pheal'] = \IPS\ROOT_PATH . '/system/3rd_party/phealng/vendor/3rdpartyeve/phealng/lib/Pheal/';}}
{{use \Pheal\Pheal;}}

Throw errors :

​Any code in a template is going to be compiled into a cached form and then executed in the scope of a class method (I think). As such, what you're trying to do will not work. You need to put together a hook or application if you're going to be pulling in a library.

 

demeto, your error is probably a result of where exactly you're trying to do that. Echoing wherever you did put it outside of the encoded stream, hence, encoding error. Ralf's approach will put it within the stream and avoid errors.

Posted

The "use" operator  for namespace has to be used outside of functions and classes...

You should start to use the full qualified class name, then you'll never run into such troubles:D

Posted

Hi guys sorry for the late reply, for some weird reason I wasn't able to log in here .. Seems the post is mildly hijacked in the meanwhile:unsure:

​Any code in a template is going to be compiled into a cached form and then executed in the scope of a class method (I think). As such, what you're trying to do will not work. You need to put together a hook or application if you're going to be pulling in a library.

 

demeto, your error is probably a result of where exactly you're trying to do that. Echoing wherever you did put it outside of the encoded stream, hence, encoding error. Ralf's approach will put it within the stream and avoid errors.

​@Ryan, That seems to only work well for a simple variable. If I try to use this for an array  (for instance The sessions array)  I get an error. Whilst this was possible in IPboard 3, which proofed very useful for learning what is running etc.

If anyone has a way to output an array ( the session array if possible, ) that would be super helpful! :)

{{print_r($_somearray);}} // this doesn't work
{$_somearray} // this doesn't work
{{$output=$_somearray}}   {$output} // this doesn't work

all help is welcome

The following works but is just so bloated...anyone any ideas? print_r would be of great help.

{{$output = array(1,2,3);}}
{{$new='';}}
{{foreach $output as $key => $value}}
{{$new .=$key.$value;}}
{{endforeach}}
{$new}

 

Posted

Well, print_r has a second parameter to return as string. So it's a little roundabout, but you could do:

{{$var = print_r($_someArray,1);}}
{$var}

There might be a more direct route, but I haven't tested. Not sure if single brackets can output any statement, or variables only.

You could probably also do {{echo $whatever; exit;}} to echo directly while avoiding the encoding error you hit initially.

Posted

The "use" operator  for namespace has to be used outside of functions and classes...

You should start to use the full qualified class name, then you'll never run into such troubles:D

echo Pheal::class;

returns Pheal\Pheal

​This is not the one used already ?

Posted

​This is not the one used already ?

​I think he's saying use 'echo \Pheal\Pheal::class' instead of the use statement. But you're kind of hijacking a topic here.

Posted

Well, print_r has a second parameter to return as string. So it's a little roundabout, but you could do:

{{$var = print_r($_someArray,1);}}
{$var}

There might be a more direct route, but I haven't tested. Not sure if single brackets can output any statement, or variables only.

You could probably also do {{echo $whatever; exit;}} to echo directly while avoiding the encoding error you hit initially.

​Thanks Ryan, I'll give that a try, cheers for your help mate!

Archived

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

  • Recently Browsing   0 members

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