danlito's avatar

Rendering HTML from database table to view blade issue or...?.

I am having a problem by rendering some html stuff from a database table. I have a function that is calling and returning some html content from databse table, when i use {{ }} double curly braces it shows the content on page but as a plain text not rebdered as html. After i try to use {!! !!} it does not show anything on page. It's pretty weird, i don't understand why and what's the solution in this case. My blade page contains the .blade extension as well.

Please advice.

0 likes
10 replies
RachidLaasri's avatar

You may need to apply html_entity_decode to your data.

This works fine for Laravel 4

{{html_entity_decode($post->body)}}

For Laravel 5 you may need to use this instead

{!!html_entity_decode($post->body)!!}
14 likes
danlito's avatar

I get the same result as before, the above one shows the content as plain text and the second one shows nothing. same as the results before without html_entity_decode.

RachidLaasri's avatar

My HTML code is stored in the database as entities and I tested the first one and it works for me. Maybe you should check your data in the database?

chuuke's avatar

I believe I am experiencing a similar problem. The Form class is outputting just a string of the markup, as opposed to the actual markup:

{{ Form::open() }}
  <div class="form-group">
   {{ Form::label('task', 'Task') }}
   {{ Form::text('task', null, ['class' => 'form-control']) }}
  </div>
 {{ Form::close() }}

Outputs to my browser as:

&lt;form method="POST" action="http://laravel.dev/tasks/create" accept-charset="UTF-8"&gt;&lt;input name="_token" type="hidden" value="J7wH3c4GgU8ynAO6Ep6TRFGQvWyeaiodMlB9LDxt"&gt;
  <div class="form-group">
   &lt;label for="task"&gt;Task&lt;/label&gt;
   &lt;input class="form-control" name="task" type="text" id="task"&gt;
  </div>
 &lt;/form&gt;

Any ideas? I am using Laravel 5 on the develop branch, and imported the form class through composer: "illuminate/html": "5.0.*@dev"

Thanks!

MThomas's avatar

@chuuke In L5 you need to use {!! !!}} instead of {{ }} if you want to render the html :)

That said, it is not a similar issue :)

@danlito can you show us some of the data in the database and the code you use to display it to the user? (and any thing in between if you are modifying the data)

5 likes
chuuke's avatar

Ahah, that was it. Did not know of the mustache-style templating change. Thanks @MThomas!

danlito's avatar

@RachidLaasri My HTML is stored as text into database table. So it should render correct obviously .

@MThomas

This is the ajax request:

$('input:radio[name=radio]').click(function() {
$.post('getcontinuity', {name: name, _token: _token}, function(data) {
        $('#continuity-wrapper').html(data);
    });
});

This is the function that get the data based on this request:

public static function getContinuity(){
       if($_POST['name']) {
           //Get the content for the continuity to display on page
           $source = GetProducts::getContinuity($_POST['name']);

           if($source){

               return view('extras.continuity')->with( compact('source'));

           }else{
               return false;
           }
       }


    }

This is the function that pulls the data from DB table

  public static function getContinuity($source) {

        try{

            $getContinuity = DB::table('xxx')->where('brand', BrandRead::read())->where('brand_id', $source)->first();

            return $getContinuity;

        } catch (\Exception $ex) {

            return false;
        }
    }

And this is the extras.blade.php page use on view

{!!$source->content !!}
1 like
Snapey's avatar

hey @mahesh_lad

Thanks for your first contribution, but generally it is not necessary to comment on three year old threads.

People have moved on....

Snapey's avatar

@mahesh_lad if you have a question, please start a new thread

instructions for posting code are below every forum input

Please or to participate in this conversation.