gpt4 book ai didi

laravel-5 - 在 Laravel 5 Blade 模板中包含 SVG 内容

转载 作者:行者123 更新时间:2023-12-03 13:41:20 25 4
gpt4 key购买 nike

在 Laravel 5 Blade 模板中包含 SVG 文件(位于 Assets 文件夹中)的内容的最佳方法是什么?

我不想使用图像/对象/嵌入标签,出于速度原因,这应该是内联 SVG。

我知道我可以使用 <?php file_get_contents("file.svg") ?>但是有没有更好的特定于 Laravel/Blade 的方法?

编辑:澄清一下,该方法应该适用于所有 SVG 文件,包括以下文件。

<?xml version="1.0" encoding="UTF-8"?>
<svg xmlns="http://www.w3.org/2000/svg">
<path stroke="red" fill="#00f" d="M10 10h100v100H10z"/>
</svg>

最佳答案

类似于接受的答案,但更清晰(imo)。

使用 laravel 指令扩展 Blade ,像这样(在您的应用服务提供商中,如 here 所述):

    \Blade::directive('svg', function($arguments) {
// Funky madness to accept multiple arguments into the directive
list($path, $class) = array_pad(explode(',', trim($arguments, "() ")), 2, '');
$path = trim($path, "' ");
$class = trim($class, "' ");

// Create the dom document as per the other answers
$svg = new \DOMDocument();
$svg->load(public_path($path));
$svg->documentElement->setAttribute("class", $class);
$output = $svg->saveXML($svg->documentElement);

return $output;
});

然后像这样在你的 Blade 中使用它:
        <div class="Login__image Login__cloud">
@svg('cloud.svg', 'Cloud')
</div>

关于laravel-5 - 在 Laravel 5 Blade 模板中包含 SVG 内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30058556/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com