Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

StuffedGoat's avatar

Blade file: Timestamp format and leading space

Hi! I'm building a Sitemap Generator and have some issues with it and kindly ask if you could give me some input.

  1. I want to change the date format of {{ unix() }} to match the standards of sitemap protocol. Is this in some way possible in a Blade file?
  2. You can see that I'm forwading a $tmp variable into the view to render this: <?xml version='1.0' encoding='UTF-8'?>. I also tried to put this into the Blade file: <?php echo '<?xml version="1.0" encoding="UTF-8"?>'; ?> But how ever I do this he puts a leading space before the line. :-) How can I get rid of this? (This produces an error since it expulses against the Sitemap protocol.

Here is my code:

// Method for creating the view

public function showSitemap()
{   
    $pages = Page::all();       
    $tmp = "<?xml version='1.0' encoding='UTF-8'?>";        
    return response()->view('backend.pages.sitemap', ['pages' => $pages, 'tmp' => $tmp])->header('Content-Type', 'text/xml;charset=UTF-8');
}

// My Blade File

{!! $tmp !!}
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
@foreach($pages as $page)
    <url>
        <loc>http://www.example.org/{{ $page->url }}</loc>
        <lastmod>{{ unix() }}</lastmod>
        <changefreq>daily</changefreq>
        <priority>0.8</priority>
    </url>
@endforeach
</urlset>

And the error message: Alt Text

0 likes
8 replies
Snapey's avatar

Check that you are not closing PHP tags on any of your scripts, ie "?>" as this will cause anything following to be sent to the output.

1 like
thepsion5's avatar

Have you tried outputting it via <?=$xmlOpenTag; ?> instead of with blade?

EDIT: What about removing it altogether and seeing if the leading space is still present?

StuffedGoat's avatar

@thepsion5 Yes, when I remove altogehter the leading space is gone, so I thinks it's because of the <? ?> notation. When I just write test at the first line it is also without a leading space.

I don't fully understand your <?=$xmlOpenTag; ?> In Blade file I have to use the { { } } / {!! !!} syntax to readout a variable -> so this doesn't work for me <?={!! $tmp !!} ?> nor <?= $tmp; ?>

StuffedGoat's avatar

@Snapey Nope :( Than he has some problems with version.

FatalErrorException in 79da7e70c5a57c64fe8eac4b4e3cf4bb line 1:
syntax error, unexpected 'version' (T_STRING)

This is driving me nuts...

I also tried to change my php.ini -> short_open_tag = 0 without luck. Wheather with shorttags yes or no neither of them worked.

// doesn't work 
<?php echo '<?xml version="1.0" encoding="utf-8" ?>'; ?>
Snapey's avatar
Snapey
Best Answer
Level 122

I have a working example. Not sure what it is that is different from my approach;

routes.php

Route::get('sitemap.xml', function() {

    $pages = [ ['url' => 'abc'],
               ['url' => '123']
             ];

    $xmlhdr = '<?xml version="1.0" encoding="UTF-8" ?>';

    $view = view('sitemap', compact('xmlhdr','pages'));

    return Response::make($view, '200')->header('Content-Type', 'text/xml');

});

and sitemap.blade.php

{!! $xmlhdr !!}
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
@foreach($pages as $page)
    <url>
        <loc>http://www.example.org/{{ $page['url'] }}</loc>
        <lastmod></lastmod>
        <changefreq>daily</changefreq>
        <priority>0.8</priority>
    </url>
@endforeach
</urlset>
1 like
StuffedGoat's avatar

After a couple of hours I now found the mistake I made. In the routes.php was a leading space at the first line which causes this error.

I also found a solution for the date format -> I just can use date() in a blade file for example { { date('Y-m-d') } }.

Thanks again all and especially @Snapey for your patience!

Please or to participate in this conversation.