Welcome Guest, Not a member yet? Register   Sign In
Remove blank line when render view in CI4
#1

(This post was last modified: 01-24-2024, 08:58 PM by tmtuan.)

Hi, i have a problem with the view render, it always render a blank line at the top of the html file, how can i remove that?

[Image: OLGXgxX.jpg]

in my source code i tried with the view renderer or the view function all of them show the same result

My code in controller

PHP Code:
$postData model(PostModel::class)
            ->join('post_content''post_content.post_id = post.id''LEFT')
            ->where('lang_id'$this->_data['curLang']->id)
            ->where('post_status''publish')
            ->where('post_type'PostTypeEnum::POST)
            ->orderBy('post.id''desc')
            ->select('post.id, post.user_init, post.post_type, post.created_at, post_content.title, post_content.slug, post_content.description')
            ->findAll(500);

        // add posts to the feed
        foreach ($postData as $post) { //dd($post->author);
            $this->feeds->add($post->title$post->url$post->created_at$post->author$post->slug$post->description$post->categories);
        }

        $renderer  single_service('renderer');

        return $renderer
            
->setData($this->feeds->getData())
            ->render('rss'); 


PHP Code:
$postData model(PostModel::class)
            ->join('post_content''post_content.post_id = post.id''LEFT')
            ->where('lang_id'$this->_data['curLang']->id)
            ->where('post_status''publish')
            ->where('post_type'PostTypeEnum::POST)
            ->orderBy('post.id''desc')
            ->select('post.id, post.user_init, post.post_type, post.created_at, post_content.title, post_content.slug, post_content.description')
            ->findAll(500);

        // add posts to the feed
        foreach ($postData as $post) { //dd($post->author);
            $this->feeds->add($post->title$post->url$post->created_at$post->author$post->slug$post->description$post->categories);
        }

        return view('rss'$this->feeds->getData(), ['debug' => false]); 


My code in view file

PHP Code:
<?php
/**
 * @author tmtuan
 * @github https://github.com/tu801
 * created Date: 1/24/2024
 */
header('Content-type: application/rss+xml; charset=utf-8');
echo 
'<?xml version="1.0" encoding="'$channel['encoding'] .'" ?>'//print_r($items); exit;
?>
<rss version="2.0">
    <channel>
        <title><?php echo $channel['title']; ?></title>
        <link><?php echo $channel['link']; ?></link>
        <description><?php echo $channel['description']; ?></description>
        <pubDate><?php echo $channel['pub_date'?></pubDate>
        <language><?php echo $channel['language']; ?></language>
        <copyright><?php echo $channel['copyright']; ?></copyright>
        <generator><?php echo $channel['generator']; ?></generator>
        <docs><?php echo $channel['docs']; ?></docs>
        <?php if ( !empty($channel['web_master_email']) ) : ?>
        <webMaster><?php echo $channel['web_master_email']; ?></webMaster>
        <?php endif; ?>
        <ttl>20</ttl>
        <?php if (!empty($items)): ?>
            <?php foreach ($items as $item): ?>
                <item>
                    <title><?php echo xml_convert($item['title']); ?></title>
                    <link><?php echo site_url($item['link']) ?></link>
                    <pubDate><?php echo date('D, d M Y H:i:s O'$item['pub_date']->getTimestamp()) ?></pubDate>
                    <author><?php echo $item['author']->username ?></author>
                    <guid isPermaLink="true"><?php echo site_url($item['slug']) ?></guid>
                    <description><![CDATA[<?php echo strip_tags(html_entity_decode($item['description'])); ?>]]></description>
                    <?php if (!empty($item['categories'])): ?>
                    <?php foreach ($item['categories'] as $category): ?>
                        <category><![CDATA[<?php echo $category['title'];?>]]></category>
                    <?php endforeach; ?>
                    <?php endif; ?>
                </item>
            <?php endforeach; ?>
        <?php endif; ?>
    </channel>
</rss> 


Can anyone help me to remove that blank line please
Reply
#2

Code:
?><rss version="2.0">
Reply
#3

i did test it. just remove this comment line

/**
* @author tmtuan
* @github https://github.com/tu801
* created Date: 1/24/2024
*/
Reply
#4

Replace echo:
PHP Code:
echo '<?xml version="1.0" encoding="'$channel['encoding'] .'" ?>'


PHP Code:
<?php
header
();
?><?xml version="1.0" encoding="<?= $channel['encoding'?>
Simple CI 4 project for beginners codeigniter-expenses ( topic )
Reply
#5

Thanks you all for you help, i solved the problem,  I tried you guys solution but it isn't worked!
i shared my solution here in case that someone may face the same problem

First in the controller i replace the extend
From:

Code:
class Rss extends BaseController

To:
PHP Code:
use CodeIgniter\Controller;

class 
Rss extends Controller 

Second i remove the header function in the view file
Finally fix the index function in the controller use CI response->setheader + setBody for this
PHP Code:
    public function index()
    {
        $postData model(PostModel::class)
            ->join('post_content''post_content.post_id = post.id''LEFT')
            ->where('lang_id'$this->lang->id)
            ->where('post_status''publish')
            ->where('post_type'PostTypeEnum::POST)
            ->orderBy('post.id''desc')
            ->select('post.id, post.user_init, post.post_type, post.created_at, post_content.title, post_content.slug, post_content.description')
            ->findAll(500);

        // add posts to the feed
        foreach ($postData as $post) { //dd($post->author);
            $this->feeds->add($post->title$post->url$post->created_at$post->author$post->slug$post->description$post->categories);
        }

        $this->response->setHeader('Content-Type''application/xml;charset=UTF-8');
        $html view('rss'$this->feeds->getData(), ['debug' => false]);
        return $this->response->setBody($html);
    
Reply
#6

Yes, you should not use `header()` because the Response object manages all headers.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB