Closed mcbmcb0 closed 7 months ago
To return HTML see: https://github.com/kgitthoene/grav-plugin-shortcode-external-caller/tree/master#return-html-or-json
To evaluate arguments in PHP use: $argv
I'm going to express this in a simple example:
Source code in markdown page:
[external-caller="/usr/bin/php page://external.php One Two Argument\ with\ spaces Four"]
[/external-caller]
PHP-Script (external.php, placed in the same directory as the markdown page):
<?php
echo json_encode(array( 'html' => '<div>'.
'<b style="color:tomato;">TEST</b><br>'.
'<code>'.json_encode($argv).'</code>'.
'</div>' ));
?>
HTML code must be returned as JSON string/object.
Output on grav page:
IMPORTANT! By grav now there seem to be a different handling of shortcode content, i.e. the STDIN for the external program. I'm going to fix this in the next time.
thanks for your reply and example. I can get arguments using $argv. I'm testing this on Grav v1.7.44 on php8.2.4 on xampp (win 11) but using your exact example (with paths changed) i still see html as text - pic below.
I've tried changing grav site settings to process with and without md and or twig, but no luck. I also tried wrapping the shortcode in {{...}} and {{...|raw}} but as markdown processing is before the shortcode i guess no difference. any suggestions?
thanks
I'm testing with GRAV 1.7.44, PHP 7.4.33 and nginx. The Plugin got an update today, feel free to test it now. I'm going to test it with PHP 8.x with W$, tomorrow. Get the new version from github here.
cd GRAV-DIRECTORY/user/plugins mv shortcode-external-caller shortcode-external-caller-0.16 git clone https://github.com/kgitthoene/grav-plugin-shortcode-external-caller.git mv grav-plugin-shortcode-external-caller shortcode-external-caller
ok thanks. with the latest on xampp php 8.2.4 i now have to specify the full enviroment path C:\xampp\php/php.exe
rather than just php
, which will make moving to my debian server interesting.
also i still am getting html entities back from the plugin. perhaps this is escaping on windows? i had a quick at the code but am unclear. can you let me know if html is displayed properly for you when you try a windows box?
thanks
EDIT: i can't see where the extra escaping occurs. but if external-caller.html.twig line 12 becomes:
<code style="background-color:#eeeeee;">{{ stdout_output |raw }}</code>
then the html is displayed properly. Tho i assume this is not the source of my problem and will create other problems
EDIT2 - I'm almost sure twig is escaping the output (using external-caller.html.twig). if ShortcodeExternalCaller L254 => 'stdout_output' => html_entity_decode("1<br>2". $stdout_output)
it makes no difference - even the 1<br>2
shows as entities. But i can't see why this would be a windows problem. this is a fresh Grav install.
??
I've tested your issue in a fresh Xampp installation on Win 10, with PHP 8.2.12.
Changed external-caller.html.twig, LINE 2 to: {{ stdout_output | raw }}
The plugin should now work as you expect it.
/admin/config/system
Caching
No
Page-Code:
[external-caller="C:/xampp/php/php.exe page://external.php One Two Argument\ with\ spaces Four"]
send to me!
1
2
3
4
5
6 7 8 9 10
[/external-caller]
External PHP-Script:
<?php
$lines = array();
while($line = fgets(STDIN)) {
array_push($lines, trim($line));
}
echo '<div>'.
'<b style="color:tomato;">TEST</b><br>'.
'ARGV=<br>'.
'<code>'.json_encode($argv).'</code><br>'.
'STDIN=<br>'.
'<code>'.json_encode($lines).'</code>'.
'</div>';
Result:
this works very well for me. thanks for all your help!
Hi. I'm trying out this plugin to return html from a php script. shortcode on page:
[external-caller="php grav://mcb/test.php 'One Two Argument'\ with\ spaces Four"] ``` send to me! ``` [/external-caller]
test.php:
echo "<b>"; while($f = fgets(STDIN)){ echo $f; } echo "</b><br />";
result dislayed on grav page:
<b><code> send to me! </code></b><br />
so:
can you advise? thanks!