Invision Community 4: SEO, prepare for v5 and dormant account notifications By Matt Monday at 02:04 PM
demeto Posted May 19, 2015 Posted May 19, 2015 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/
不中用 Posted May 19, 2015 Posted May 19, 2015 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 .. .
opentype Posted May 19, 2015 Posted May 19, 2015 {{echo "hello";}}Usually, you would output that in the stream of the template itself. {{$test = "Output this";}}{$test}
3KyNoX Posted May 20, 2015 Posted May 20, 2015 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)
Ryan H. Posted May 20, 2015 Posted May 20, 2015 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.
Daniel F Posted May 20, 2015 Posted May 20, 2015 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
3KyNoX Posted May 20, 2015 Posted May 20, 2015 echo Pheal::class;returns Pheal\Pheal (same as the one actually used).
demeto Posted May 21, 2015 Author Posted May 21, 2015 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 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}
Ryan H. Posted May 21, 2015 Posted May 21, 2015 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.
3KyNoX Posted May 21, 2015 Posted May 21, 2015 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:Decho Pheal::class;returns Pheal\PhealThis is not the one used already ?
Ryan H. Posted May 21, 2015 Posted May 21, 2015 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.
demeto Posted May 21, 2015 Author Posted May 21, 2015 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!
Recommended Posts
Archived
This topic is now archived and is closed to further replies.