0

I'm trying to use vues v-html in a blade foreach loop:

@foreach($entries as $entry)
    <a href="/entry/{{$entry->id}}"><h3>{{ $entry->created_at->toRfc822String() }}</h3></a>
    <div v-html="<p>hello world</p>">

    </div>
    <hr>
@endforeach

When I do that I get this error:

[Vue warn]: Error compiling template:

invalid expression: Unexpected token '<' in

<p>hello world</p>

Raw expression: v-html="

hello world

"

The reason I want to use v-html is because I intend to use a method to output markdown like <div v-html="marked({{$entry-content}})">

2
  • 1
    The raw html should be quoted as well <div v-html="'<p>hello world</p>'"></div> Commented Oct 27, 2019 at 8:23
  • @OluwafemiSule That works, but how would I get <div v-html="marked({{$entry->content}})"> to work properly? Commented Oct 27, 2019 at 8:28

1 Answer 1

1

Assuming marked to be a method declared in Vue instance, you can quote the interpolated content but first convert all the characters in it to corresponding HTML entities. For example,

<div v-html="marked('{{ htmlentities($entry->content) }}')">

I suggest to write this in the model as a computed property.

class Entry extends Model {
    protected $appends = ['content_html']

    getContentHtmlAttribute() {
        return htmlentities($this->content);
    }
}

Then use the computed field in your template,

<div v-html="marked('{{ $entry->content_html }}')">
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.