K
K
kovecif2020-02-29 23:20:42
Yii
kovecif, 2020-02-29 23:20:42

php yii2 text output and formatting?

Hello, I'm making a blog for educational purposes and ran into a problem with the output of the text of the posts. I have formatted text in each post (paragraph tags, they have their own classes), but it is stored in the database as plain text. Writing tags with classes to the database is probably a bad idea. Even on the page of a particular post, pictures can be inserted in different places (on some pages there are no pictures at all). Keeping img tags in the database is also probably a bad option. What are some good solutions to these problems?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Maxim, 2020-02-29
@kovecif

Keep like this - Common practice. To output, use something like this:
HTML purifier

<?= Yii::$app->formatter->asHtml($model->content, [
                'Attr.AllowedRel' => ['nofollow'],
                'HTML.SafeObject' => true,
                'Output.FlashCompat' => true,
                'HTML.SafeIframe' => true,
                'AutoFormat.AutoParagraph' => true,
                'URI.SafeIframeRegexp' => '%^(https?:)?//(www\.youtube(?:-nocookie)?\.com/embed/|player\.vimeo\.com/video/)%',
            ]) ?>

However. This option may affect page loading in some cases. Since all text is reformatted when loading pages. If performance is important, then create two fields in the database. One original, the other formatted. In the formatted field, put text that has been passed through the formatting of the original text. And when reading, you no longer need any formatters. And pages will load faster.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question