Thread tagged as: Question, Problem

How to retain leading space in content field entry

I'm trying to create a field where I can have entries that will retain a value with a leading space. I can force this by putting    before the text but is there a neater way?


Alan Coggins

Alan Coggins 0 points

  • 3 years ago
Rachel Andrew

Rachel Andrew 394 points
Perch Support

You should use CSS for that.

Sorry, I don't follow. Do you mean I need to edit the css of the backend Perch form?

To clarify, I enter " word" and it saves it as "word". I just want it to not trim the space from in front.

OK - solved this. I am using a hyphen in the text box, but converting it later in php to replace with a space.

Alan, padding or margin css would do this as Rachel recommends above, then you wouldn’t have to do anything to modify the content for visual appeal.

R. K

Hussein Al Hammad

Hussein Al Hammad 105 points
Registered Developer

I understand in some cases CSS isn't the way to go, but you still wouldn't add leading space in the content. You can just add white space in your template though. An example from the default Blog templates templates/blog/author_name.html:

<perch:blog id="authorGivenName" type="text" /> <perch:blog id="authorFamilyName" type="text" />

The above outputs: authorGivenName authorFamilyName with whitespace between them. If there's whitespace in your template, Perch keeps it that way.

Thanks for all the replies, and I can understand now why you are suggesting css. Sorry... my fault... I didn't explain the problem clearly enough.

The field is actually a modifier for the slug and it is used to create page titles and h1 headings. And it sometimes needs a space, and sometimes it doesn't. So if the slug is "trumpet" I put "s" into this field and it makes the title "trumpets". But sometimes the slug might be "violin" and I want to enter a modifier " strings" to make the title "violin strings". So I was looking for a way to stop that stripping out of the leading space when the record was saved.

Actually the solution with the hyphen is fine because you can see more clearly in the record that you have entered the space in there. Cheers.

Duncan Revell

Duncan Revell 78 points
Registered Developer

Just to throw my tuppence into the hat:

As opposed to adding and removing a hyphen, could you not add a checkbox to the region - "tick here to add a leading space"? If only because the content you're saving will always have a character in it you don't want - at least with a checkbox and a bit of code, the content could be used elsewhere (potentially) without having a spurious hyphen in it...

Great idea Duncan! That's a much neater solution. I'll do it that way. Thanks for the suggestion. Cheers.

Edit. Took me 10 mins to set up and works beautifully!