如何在Laravel 5表单中将Markdown用于textarea输入字段? [英] How to use Markdown for textarea input field in Laravel 5 form?

查看:85
本文介绍了如何在Laravel 5表单中将Markdown用于textarea输入字段?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在基于Laravel 5的项目中,我正在使用 http://packalyst的Markdown包.com/packages/package/graham-campbell/markdown .

In my Laravel 5 based project, I am using Markdown package from http://packalyst.com/packages/package/graham-campbell/markdown.

如何在Laravel 5表单中的文本区域输入字段中使用Markdown? 为Yii2找到了一个很好的例子,但需要知道如何在Laravel 5中实现. Yii2的Markdown演示: http://demos.krajee.com/markdown-demo

How to use Markdown for textarea input field in Laravel 5 form? One good example found for Yii2 but need to know how can achieve in Laravel 5. Markdown demo for Yii2: http://demos.krajee.com/markdown-demo

推荐答案

如果要将HTML输出存储在数据库中(您不应该使用IMO),则可以这样做:

If you want to store the HTML output in the database (you shouldn't IMO), you can do it like this:

<?php

namespace App\Http\Controllers;

use App\SomeModel;
use Illuminate\Http\Request;
use GrahamCampbell\Markdown\Facades\Markdown;

class SomeController extends Controller
{
    /**
     * Handle form submission of my markdown form.
     *
     * @return redirect
     */
    public function create(Request $request)
    {
        $markdownInput = $request->get('markdown_input');

        $model = new SomeModel();
        $model->html = Markdown::convertToHtml($markdownInput);

        if ($model->save()) {
            return redirect('/success');
        }
        else {
            die("Handle failed submission.");
        }
    }
}

但是,正如我说的那样,您不应该这样做,因为这将需要大量的存储 IF ,您的数据库中有很多记录.如果没有,那就不会受伤.

But as I said, you shouldn't because it will take a lot of storage IF you have a lot of records in your database. If not, it won't hurt.

相反,将原始markdown输入保存在数据库中,而不将其转换为HTML,并在视图中将输入转换为HTML:

Instead, save the raw markdown input in your database without converting it to HTML and convert the input to HTML in your views:

config/app.php中,为Markdown外观添加别名:

In config/app.php add an alias to the Markdown facade:

'Markdown' => 'GrahamCampbell\Markdown\Facades\Markdown'

然后,您可以执行以下操作:

Then in your views you can do:

{{ Markdown::convertToHtml($rawMarkdownInputFromTheDatabase) }}

这篇关于如何在Laravel 5表单中将Markdown用于textarea输入字段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆