Jump to:

3443 Posts in 1030 Topics by 871 members

Template Questions

SilverStripe Forums » Template Questions » First character of paragraph in <div>

Moderators: martimiz, Sean, biapar, Willr, Ingo, swaiba, simon_w

Page: 1
Go to End
Author Topic: 1377 Views
  • gakenny
    Avatar
    Community Member
    153 Posts

    First character of paragraph in <div> Link to this post

    Hello,

    I am wanting to do some special formatting on the very first character of the very first paragraph on the Homepage of my site by including this within a <div>.

    My issue is how to split this first character from the remaining characters from the $Content variable.

    I have created a couple of functions and added these to my HomePage.php file:
    function ContentFirstCharacter() {
    return ($Content[0]);
    }

    function ContentLastCharacters() {
    return (substr($Content,1));
    }

    It appears that the $Content variable is not available and these functions return blanks. I have tried to pass the variable as a parameter with no success either.

    Can anyone give me a push in the right direction?

    Many thanks,

    Gary

  • Pigeon
    Avatar
    Community Member
    243 Posts

    Re: First character of paragraph in <div> Link to this post

    To manipulate the content you need to use $this->Content; in the PHP, $Content is just for the templates.

  • gakenny
    Avatar
    Community Member
    153 Posts

    Re: First character of paragraph in <div> Link to this post

    Hi Pigoen,

    Thanks for the reply. I did try this and I was receiving a '<' from ContentFirstCharacter. After a sleep, it seems obvious that this would be the first character of the HTML code included in the $Content variable (and referenced in PHP by $this->Content.

    Can I strip the HTML from this variable and reference the text only in my PHP functions?

    Kind regards,

    Gary

  • Pigeon
    Avatar
    Community Member
    243 Posts

    Re: First character of paragraph in <div> Link to this post

    Ah, of course it would return a '<'.

    You want to do this:

    $content = $this->dbObject('Content')->NoHTML();

    That will give you the HTML stripped content.

  • gakenny
    Avatar
    Community Member
    153 Posts

    Re: First character of paragraph in <div> Link to this post

    Thanks Pigeon,

    That cracked it. I had just tried the following with the same result:
    $content = strip_tags($this->Content)

    However, I have reverted to your code as this utilises the functions within SilverStripe.

    Thanks for your help!

    Cheers,

    Gary

    1377 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.