1

I've setup a custom .phtml block that I need to add in various places in a couple of different files. To do this I'm using the following code:

<?php echo $this->getLayout()->createBlock('core/template')->setTemplate('page/template/newsletter.phtml')->toHtml(); ?>

For some reason, this seems to be rendering some random Whitespace before the block itself, see attached image.

enter image description here

I'm using Magento 1.9.1

Here are the contents of my custom .phtml file, although from playing with the template calling it I'm pretty sure the whitespace is added in before the block/content!

<div class="newsletter-signup padded-block">
    <h2 class="major">
        <?php echo $this->__('Email Signup') ?>
    </h2>
    <div class="content">
        <div class="newsletter-name">
            <select class="newsletter-title">
                <option value="Mr">Mr</option>
                <option value="Mrs">Mrs</option>
                <option value="Miss">Miss</option>
                <option value="Ms">Ms</option>
                <option value="Dr">Dr</option>
            </select>
            <input type="text" class="newsletter-firstname" placeholder="First Name">
            <input type="text" class="newsletter-lastname" placeholder="Last Name">
        </div>
    </div>
        <input type="text" class="disabled" placeholder="Your name" disabled>
        <input type="email" class="" placeholder="Email address">
</div>

I'm calling this from a Template file with the following code:

        <div class="col-xs-4 col-sm-4 col-md-12 newsletter-block">
            <?php echo $this->getLayout()->createBlock('core/template')->setTemplate('page/template/newsletter.phtml')->toHtml(); ?>
        </div>
2
  • From what phtml file are you creating this block? Commented Dec 15, 2014 at 11:45
  • @Christopher Thrower Can you paste your actual code that where you use this block rendering? Also is this white space on your Web UI or only on your source code? Check your Source code view were you can see its happening due to your rendering line actually comes in new line.
    – Elamurugan
    Commented Dec 15, 2014 at 12:13

2 Answers 2

2

You might have a BOM in one of your files (http://en.wikipedia.org/wiki/Byte_order_mark). If you are using PhpStorm it is pretty easy to remove it.

If you are working in a *nix environment, you can scan your files using this command:

grep -rl $'\xEF\xBB\xBF' .
1
  • Was some random hidden characters added at the start from the Text Editor. Commented Dec 19, 2014 at 11:17
1

One possibility for whitespace being injected is the use of closing ?> PHP tags in your block classes (see here for more information), so that's worth checking.

You may also want to try adding commenting before you output the block:

<div class="col-xs-4 col-sm-4 col-md-12 newsletter-block"><!--
    --><?php echo $this->getLayout()->createBlock('core/template')->setTemplate('page/template/newsletter.phtml')->toHtml(); ?>
</div>

Or just condense it onto one line:

<div class="col-xs-4 col-sm-4 col-md-12 newsletter-block"><?php echo $this->getLayout()->createBlock('core/template')->setTemplate('page/template/newsletter.phtml')->toHtml(); ?></div>
1
  • I've tried condensing it and I still get some whitespace, altough a little less! I'll look into the comment block and closing PHP though Commented Dec 15, 2014 at 14:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.