View previous topic :: View next topic |
Author |
Message |
Chewi Smarty Rookie
Joined: 13 Apr 2004 Posts: 13
|
Posted: Thu May 06, 2004 10:26 pm Post subject: Adjustments to textformat |
|
|
Hello. I've just been playing around with textformat. I'm trying to use it to format my HTML neatly (yeah I'm a fussy bastard) but I think it needs two small adjustments for it to work properly.
Firstly, it always inserts a line break before the {/textformat} tag for seemingly no particular reason. For example, if I do this...
Code: | <style type="text/css">
<!--{textformat indent=3 indent_char=" "}--><!--{$css}--><!--{/textformat}-->
</style> |
Then it produces this...
Code: | <style type="text/css">
body { background-color: green; }
</style> |
This still happens even if I put the whole lot on one line.
Secondly, textformat seems to ignore line breaks in a variable. If I pass a string with some \n codes from PHP, the breaks appear when not using textformat but they disappear when I do use it. It would also need to indent properly after each line break.
textformat: http://smarty.php.net/manual/en/language.function.textformat.php
Last edited by Chewi on Thu May 13, 2004 9:03 am; edited 1 time in total |
|
Back to top |
|
Chewi Smarty Rookie
Joined: 13 Apr 2004 Posts: 13
|
Posted: Fri May 07, 2004 9:34 pm Post subject: |
|
|
Okay the extra line is obviously being caused by this...
Code: | $output .= $paragraph . $wrap_char . $wrap_char; |
Why is the input being dealt with in paragraphs (two line breaks)? |
|
Back to top |
|
mohrt Administrator
Joined: 16 Apr 2003 Posts: 7368 Location: Lincoln Nebraska, USA
|
Posted: Fri May 07, 2004 10:04 pm Post subject: |
|
|
Thats just how it was implemented, mostly for formatting e-mail text. I suppose modifying it so the last paragraph doesn't have any wrap characters at the end would solve your problem. (?) |
|
Back to top |
|
mohrt Administrator
Joined: 16 Apr 2003 Posts: 7368 Location: Lincoln Nebraska, USA
|
Posted: Fri May 07, 2004 10:50 pm Post subject: |
|
|
Try the CVS version of block.textformat.php, see if that helps. |
|
Back to top |
|
Chewi Smarty Rookie
Joined: 13 Apr 2004 Posts: 13
|
Posted: Sat May 08, 2004 9:04 am Post subject: |
|
|
Okay that's better but now I have to add an extra line break!
Code: | <style type="text/css">
<!--{textformat indent=3 indent_char=" "}--><!--{$css}--><!--{/textformat}-->
</style> |
I guess the line ought to be this...
Code: | $_output = implode($wrap_char . $wrap_char, $_paragraphs) . $wrap_char; |
Seems to work. It's still eating the line breaks in the variable though. I'll stare at it for a bit longer. |
|
Back to top |
|
Chewi Smarty Rookie
Joined: 13 Apr 2004 Posts: 13
|
Posted: Sat May 08, 2004 9:23 am Post subject: |
|
|
It's this part that's doing it, deliberately it seems. If I comment it out, it works.
Code: | // convert mult. spaces & special chars to single space
$_paragraphs[$_x] = preg_replace(array('!\s+!','!(^\s+)|(\s+$)!'), array(' ',''), $_paragraphs[$_x]); |
I can see why it's there but I'm not quite sure why it's effecting my variable. It's supposed to replace anything that goes "[beginning of line][space][possibly more spaces]" or "[space][possibly more spaces][end of line]" with a single space, right? The string I passed into $css (see above) was "body {\nbackground-color: green;\n}" and there's no spaces on either side of the \n breaks so why are they getting replaced? In any case, I may want to put tabs or spaces around the breaks so could this line be made optional? If all you want to do is indent some already well-formed text then it shouldn't be necessary. |
|
Back to top |
|
mohrt Administrator
Joined: 16 Apr 2003 Posts: 7368 Location: Lincoln Nebraska, USA
|
Posted: Sat May 08, 2004 2:35 pm Post subject: |
|
|
Remember that php eats newlines (\n) after each PHP close tag (?>), so those have to be compensated for in the rendered templates. Check the compiled template and see if that is the case. If so, then the extra $wrap_char on the end should do it. |
|
Back to top |
|
Chewi Smarty Rookie
Joined: 13 Apr 2004 Posts: 13
|
Posted: Tue May 11, 2004 2:33 pm Post subject: |
|
|
I think you misunderstand, I've fixed the missing/extra line at the end problem. Now I'm trying to tackle the second problem, which is that textformat swallows the line breaks within a variable so this...
Code: | $smarty->assign('css', "body {\nbackground-color: green;\n}"); |
...turns into this...
Code: | body { background-color: green; } |
|
|
Back to top |
|
messju Administrator
Joined: 16 Apr 2003 Posts: 3336 Location: Oldenburg, Germany
|
Posted: Tue May 11, 2004 2:55 pm Post subject: |
|
|
I think you are misusing textformat for a task it isn't designed for.
textformat is supposed to format text-paragraphs like emails or forum-posts, not to format sourcecode like your css definitions. |
|
Back to top |
|
mohrt Administrator
Joined: 16 Apr 2003 Posts: 7368 Location: Lincoln Nebraska, USA
|
Posted: Tue May 11, 2004 3:00 pm Post subject: |
|
|
Messju is correct. textformat does not consider the context of newlines, they are all paragraph separators as far as it is concerned. |
|
Back to top |
|
messju Administrator
Joined: 16 Apr 2003 Posts: 3336 Location: Oldenburg, Germany
|
Posted: Tue May 11, 2004 3:11 pm Post subject: |
|
|
mohrt wrote: | textformat does not consider the context of newlines, they are all paragraph separators as far as it is concerned. |
I think even more correct:
textformat ignores single newlines but treats double newlines as paragraph separators.
like:
Code: |
these lines contains a newline for readabiltity
in the editor, but it has no meaning in the
text.
but two newlines (like above) separate two
paragraphs and are handled like that.
|
|
|
Back to top |
|
Chewi Smarty Rookie
Joined: 13 Apr 2004 Posts: 13
|
Posted: Tue May 11, 2004 4:06 pm Post subject: |
|
|
Okay that's fair. It just seems a little silly that I can't use the really quite useful indenting feature without the rest of the stuff kicking in. Could they be separated somehow? Sorry for being a pest but this place is for suggestions, after all. |
|
Back to top |
|
Chewi Smarty Rookie
Joined: 13 Apr 2004 Posts: 13
|
Posted: Wed May 12, 2004 10:57 pm Post subject: |
|
|
Well I guess if you want something done, you have to do it yourself. If I implement this myself, would you accept it? Otherwise I'll just use it on my own site. And if so, would you prefer a new {indent} tag or shall I make use of the style parameter in {textformat}? Currently the only style is email. The docs state that the style simply sets the parameters to certain preset values but I would actually be altering the behaviour in this case. So which way would be best? I'm thinking a new tag since, as you say, {textformat} seems to be designed for other things. |
|
Back to top |
|
Chewi Smarty Rookie
Joined: 13 Apr 2004 Posts: 13
|
Posted: Wed May 12, 2004 11:17 pm Post subject: |
|
|
Oh. There's already an indent tag. You can kill me now. It only handles variables and not stuff written in the templates but I'll figure something out. It works for that CSS example anyway.
That's the second time I've made an ass of myself here. I'm really not that dumb, I promise. Work must be getting to me.
indent: http://smarty.php.net/manual/en/language.modifier.indent.php
Last edited by Chewi on Thu May 13, 2004 9:05 am; edited 1 time in total |
|
Back to top |
|
boots Administrator
Joined: 16 Apr 2003 Posts: 5611 Location: Toronto, Canada
|
Posted: Thu May 13, 2004 1:08 am Post subject: |
|
|
If you want to ensure properly formatted output code for some reason, you may want to consider the Tidy extension for which there is a Smarty Output Filter. Caveat: PHP5 only? (apparently not, but I haven't tried)
Cheers!
ps. Chewi, can you edit your posts and include the links to the plugins you found? thanks! |
|
Back to top |
|
|