Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

Template Questions /

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

First character of paragraph in <div>


Go to End


5 Posts   2071 Views

Avatar
gakenny

Community Member, 153 Posts

16 January 2010 at 11:55pm

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

Avatar
dhensby

Community Member, 253 Posts

17 January 2010 at 1:07am

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

Avatar
gakenny

Community Member, 153 Posts

17 January 2010 at 8:20am

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

Avatar
dhensby

Community Member, 253 Posts

17 January 2010 at 10:35am

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.

Avatar
gakenny

Community Member, 153 Posts

17 January 2010 at 6:46pm

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