webwizo / laravel-shortcodes

Wordpress like shortcodes for Laravel 4.2, 5.x, 6.x, 7.x, 8.x, 9.x and 10.x
MIT License
214 stars 49 forks source link

Selective usage of shortcode not working #34

Open bernhardh opened 6 years ago

bernhardh commented 6 years ago

Hello,

first of all: Thanks for your great work! I have the following issue (bug?):

I have the following controller:

Route::get('shortcode', function(){
    return view('test.shortcode')->with("code", '[b class="bold"]Bold text[/b]');
});

Case 1 - As expected When I use the variable $code in the template like: {{ $code }} it works as expected - I only get the raw string as a result: [b class="bold"]Bold text[/b]

Case 2 - As expected When I use the variable $code with the compile comand in the template like: {{ \Shortcode::compile($code) }} it works as well as expected - I get the (escaped) parsing result: &lt;strong class=&quot;bold&quot;&gt;Bold text&lt;/strong&gt; Using it with unescaped blade: {!! \Shortcode::compile($code) !!} I get <strong class="bold">Bold text</strong>

Case 3 - Weird But when I combine both versions, the result is weird.

{{ $code }}<br>
{{ \Shortcode::compile($code) }}

This is the result:

<strong class="&quot;bold&quot;">Bold text</strong><br>
&lt;strong class=&quot;bold&quot;&gt;Bold text&lt;/strong&gt;

WTF? Why gets the first line ({{ $code }}<br>) parsed and why is the result wrong (&quot;bold&quot;)?

professorhaseeb commented 6 years ago

your Case 1 works if you add ->withShortcodes(); at the end.

Route::get('/test', function(){
    return view('test')->with("code", '[b class="bold"]Bold text[/b]')->withShortcodes();
});

Result: https://prnt.sc/jyi07l

bernhardh commented 6 years ago

Thats not the problem, because case 1 is not expected to work, so its ok, that it doesn't work. The problem is the weird behavior of case 3.

professorhaseeb commented 6 years ago

test.blade.php

{{$code}}
<br>
{!! \Shortcode::compile($code) !!}

web.php

Route::get('/test', function(){
     return view('test')->with("code", '[b class="bold"]Bold text[/b]');
});

Result: http://prntscr.com/jz3s82

Try using \Shortcode::enable(); in your controller or ->withShortcodes();

adnedelcu commented 2 years ago

I think what @bernhardh wants to say is that he doesn't want the first line ({{ $code }}<br>) to be parsed, while the second one should ({{ \Shortcode::compile($code) }}).

This is also something we've been facing ourselves. Is there a way to achieve this?