5

I want to display the username as the default textarea value for markdown editor using blade syntax.

<textarea v-model="message">
      {{ $detailsFromLaravelContoller }}
</textarea>
<div v-html="compiledMarkdown"></div>

But I am using v-model component for the textarea which requires to declare message with an empty value like this

window.onload = function()
{ 
    var editor = new Vue({
    el: '#editor',
    data: {
        message: '',
        compiledMarkdown: marked('', { sanitize: true }), 
    },
    watch: {
        markdown: function () {
          this.compiledMarkdown = marked(this.message, { sanitize: true })
        }
      }, 
      methods: {
        
      }
  })  
}

This renders the screen with the laravel variable's value. But soon after the page loads the content disappears (as I've used window.onload I guess).
Also I'm not using inline VueJS.
P.S: I'm new to both VueJS and Laravel and the source for the markdown is here(jsfiddle)
Thank you in advance!!!

2
  • even with an empty value Commented Apr 11, 2018 at 6:17
  • Yes. Soon after the page loads the textarea value disappears. I was able to notice that the content is being rendered but removed immediately after screen loading by reloading the page repeatedly. Commented Apr 11, 2018 at 6:21

2 Answers 2

2

You are trying to pass a PHP variable value to a separate Javascript file.

Here's how I would do it:

Declare a global variable detailsFromLaravelContoller to store $detailsFromLaravelContoller as a string value

<script>
    var detailsFromLaravelContoller = @json($detailsFromLaravelContoller);
</script>
<textarea v-model="message">
</textarea>

use the global variable in Javascript file

data: {
    message: detailsFromLaravelContoller,
},

https://jsfiddle.net/jacobgoh101/0dzvcf4d/9954/

Sign up to request clarification or add additional context in comments.

4 Comments

is '{{ $detailsFromLaravelContoller }}' the same as {!! $detailsFromLaravelContoller!!}; ?
@SebastienD I've changed it to {!! $detailsFromLaravelContoller!!} . I know php but I don't really know laravel. This SO answer use {!! !!}, so I follow it now. Sorry for the confusion.
answer updated based on @SebastienD's suggestion. json_encode should help you escape the special characters used in markdown.
That helped to escape the illegal characters. Thanks a lot
2

You can initialize the v-model in data with your laravel variable.

window.onload = function()
{ 
    var editor = new Vue({
    el: '#editor',
    data: {
        message: {!! $detailsFromLaravelContoller !!},
        compiledMarkdown: marked('', { sanitize: true }), 
    },
    watch: {
        markdown: function () {
          this.compiledMarkdown = marked(this.message, { sanitize: true })
        }
      }, 
      methods: {

      }
  })  
}

1 Comment

This one seems a little reliable because VueJS gives me a warning when using script tags in templates. I'll try both the answers and mark the one which satisfies my team members. Thanks for answering.

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.