vinsa
vinsa

Reputation: 1242

How to delete comments from templates in Laravel phpframework

Every developer writes comments in his HTML, CSS or Javascript. But I don't want my comments to be visible for other people. I develop an web app with laravel php framework and I want to clear all comments from my code as:

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

I mean to clear the comments in runtime, so end-user to not see them in the web page source code. How can I do that?

Upvotes: 1

Views: 702

Answers (3)

vinsa
vinsa

Reputation: 1242

Well, I found a solution, it's something that is called Blade Extending. So, how to use it for that purpouse? I will give you an example:

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;
});

You should place this code in non-public/app/filters.php or anywhere else, there is no matter, if it is executing before the view rendering.

I hope this will be useful for other people too.

Upvotes: 1

Alana Storm
Alana Storm

Reputation: 166066

It's unstated, but I assume you want a way to leave comments that aren't rendered in HTML?

If you're using PHP templates with Laravel, just use PHP comments

<?php // this is my comment ?>

If you're using the blade template engine, just use blade comments

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

Upvotes: 0

Simon Wicki
Simon Wicki

Reputation: 4059

if you are using blade, you can use

{{-- comment --}}

Upvotes: 0

Related Questions