john123's avatar

Retrieving styled text from database

Hello everyone , so right now, I am developing a Laravel-Vue website, and I am trying to make a news/event page. The contents of the blog or news I can simply output from the database without a problem, my problem is the inconsistency of the styling of each different blog since they might have different number of titles and sub-titles inside the blog text since it might depend on the number of paragraphs etc..., some words of the content of the blog might be desired to be enlarged or be bold or underlined since its a title , the titles are not consistent between blogs in order to surround a part of the output with tags or any different styling ,do anyone know how to solve that problem?

thank you for your help

0 likes
9 replies
gych's avatar

To display this as html and not string you can use the v-html attribute

v-html="yourTextVariable"

For example

<p v-html="yourTextVariable"></p>
john123's avatar

ok so lets say the coding structure is like this:


<h1 v-html="TitleVariable"></h1>

<p v-html="paragraphVariable"></p> 

paragraphVariable = 
"
subtitle1

Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Donec quam felis, ultricies nec, pellentesque eu, pretium quis, sem. Nulla consequat massa quis enim. Donec pede justo, fringilla vel, 

subtitle2

Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Donec quam felis, ultricies nec, pellentesque eu, pretium quis, sem. Nulla consequat massa quis enim. Donec pede justo, fringilla vel, "

how do I retrieve subtitle1 and subtitle2 from the database while making sure they are of a bigger size or bold or underlined ??

gych's avatar

@john123 Do you use a richtexteditor to style the text before saving it to the database?

john123's avatar

@gych well to be honest I wanted to clear the logic of sending and retrieving styled text to and from the database before heading into the actual styling of the text, I would love it if you can give me pointers on what to use to style the text before saving it to the database too. sorry for bothering you and thanks for the help!!

gych's avatar
gych
Best Answer
Level 29

@john123 First you should look into adding a RichTextEditor that gives you the sytling options you need. There are multiple editors for this available with each their available styling options.

Personally I use SunEditor but there are also others like Quill Js, WYSIWYG Editor, CK Editor, TinyMCE, ... You should check multiple and see which one fits best to your needs and for your application.

When you've installed a RichTextEditor for you application you can use this as a kind of 'TextArea' in your form.

After the form has been saved to the database you can use the v-html attribute to display the styled text.

1 like
john123's avatar

@gych so if the datatype is nvarchar(MAX), the styled text is stored in it and can be retrieved without losing its style?

gych's avatar

@john123 Yes indeed, the output text from the richtexteditor is in HTML format which can be stored as a string in the DB. You can then display it as styled text by using the v-html attribute.

For larger texts you can use BLOB datatype in your DB (=binary in migration file)

1 like
john123's avatar

@gych I have been trying to figure out how to do it for a while thank you very much for your help!!!

1 like
gych's avatar

@john123 I'm glad I could help. If you have any more questions, don't hesitate to reach out.

1 like

Please or to participate in this conversation.