如何从Laravel php框架中的模板中删除注释

时间:2014-11-21 22:04:10

标签: laravel

每个开发人员都在他的HTML,CSS或Javascript中写评论。 但我不希望其他人看到我的评论。 我开发了一个带有laravel php框架的web应用程序,我想清除我的代码中的所有注释:

// comment 
/* comment */
<!-- comment -->

我的意思是在运行时清除注释,因此最终用户不要在网页源代码中看到它们。 我怎么能这样做?

3 个答案:

答案 0 :(得分:0)

如果您使用的是刀片,则可以使用

{{-- comment --}}

答案 1 :(得分:0)

它没有说明,但我想你想要一种方法来留下没有用HTML呈现的评论吗?

如果你在Laravel中使用PHP模板,只需使用PHP评论

<?php // this is my comment ?>

如果您正在使用刀片模板引擎,请使用刀片评论

{{-- This comment will not be in the rendered HTML --}}

答案 2 :(得分:0)

嗯,我找到了一个解决方案,它被称为Blade Extending。 那么,如何将它用于那个purpouse?我会举个例子:

Blade::extend(function($value)
{
    $value = preg_replace('/<!--(.+?)-->/s', '', $value); //it's removing <!-- multiline comments -->
    $value = preg_replace('/\/\*(.*?)\*\//s', '', $value); //it's removing /* multiline comments */
    $value = preg_replace('/(?<!:)\/\/.+/', '', $value); //it's removing // single line comments in JS      
    return $value;
});

您应将此代码放在非公共/ app / filters.php 或其他任何地方,无论是否在视图渲染之前执行。

我希望这对其他人也有用。