Home >PHP Framework >ThinkPHP >How to use Markdown in ThinkPHP6

How to use Markdown in ThinkPHP6

WBOY
WBOYOriginal
2023-06-20 23:00:13906browse

In the development of the modern Internet era, document writing has gradually changed from cumbersome HTML tags to the simpler and easier to read and write Markdown syntax. ThinkPHP6 uses a highly flexible template engine and provides convenient Markdown extensions, making it very easy to write and display Markdown files in projects.

What is Markdown

Markdown is a lightweight markup language that can quickly convert documents written in plain text into HTML for online reading. Markdown syntax is relatively simple and easy to remember, and is mainly divided into several parts such as titles, paragraphs, lists, code blocks, links and pictures.

For example, the following is a simple Markdown example:

# 标题一

这是一个段落。

## 标题二

这是一个无序列表:
- 列表项一
- 列表项二

这是一个代码块:

echo "Hello, world!";

这是一个链接:[GitHub](https://github.com)

这是一张图片:
![图片](https://example.com/image.jpg)

When using Markdown to write documents, just follow Just compose the text according to the corresponding grammatical specifications. There is no need to consider complex HTML tags, which greatly reduces the complexity of writing.

Using Markdown in ThinkPHP6

After ThinkPHP6.0 version, there is built-in support for Markdown. You only need to install the extension and configure the template engine to write and render documents using Markdown.

Install extension

First, you need to install the league/commonmark extension. The specific command is as follows:

composer require league/commonmark

This extension provides an explanation of Markdown syntax and The conversion function can easily convert files written in Markdown format into HTML.

Configuring the template engine

After completing the extension installation, you need to further configure the Markdown parser in the template engine.

Open the config/view.php file and add the following content:

use LeagueCommonMarkCommonMarkConverter;

return [
    // ...
    'tpl' => [
        'type' => 'Think',
        // ...
        'config' => [
            // ...
            'markdown' => function ($markdown, $vars = []) {
                $converter = new CommonMarkConverter();
                $html = $converter->convertToHtml($markdown);
                return $html;
            },
            // ...
        ],
    ],
];

In the above code, the markdown key represents the callback for Markdown file parsing Function, this function will pass in two parameters: the first parameter is the text in Markdown format, and the second parameter is the optional template variable. In the callback function, a Markdown parser is created through the league/commonmark extension to parse Markdown format text into HTML format text.

Using Markdown

After the above configuration, you can happily use Markdown syntax to write documents in the ThinkPHP6 project.

For example, we can use the view method in the controller to render the Markdown file:

public function index()
{
    $markdown = file_get_contents('path/to/document.md');
    return view($markdown, [], ['content_type' => 'text/markdown']);
}

When rendering the Markdown file, the response needs to be specified in the third parameter The header Content-Type is text/markdown so that the browser can correctly parse the Markdown text.

Summary

Through the above steps, we can easily use Markdown text to write and display project documents in the ThinkPHP6 project. Markdown text is concise and easy to understand, easy to read and maintain, and is suitable as a method for document input and output. Combining Markdown with ThinkPHP6 can not only improve the efficiency of document writing, but also provide more beautiful and easy-to-read output effects. It is an excellent solution and practice.

The above is the detailed content of How to use Markdown in ThinkPHP6. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn