heredoc and variables

F

flupke

Hi,

i have php script where i use a heredoc type of variabele
to print out some html. In the heredoc, i use the values of
several other vars.

php snippet:

$div=<<<END_DIV
<DIV class="linkblock">
<DIV class="linkblock_title">$titel_name</DIV>
$url
</DIV>
</DIV>

So the variabele div is a heredoc which will contain some
html woth the vars titel_name and url in it.

1) Can i do this in python and how?
2) Consider following heredoc

end_html="""</BODY>
</HTML>"""

I have to write it like this to avoid having an empty line added above
AND below the actual string when using it.
So this would produce the extra empty lines

end_html="""
</BODY>
</HTML>
"""

I actually think that the last piece of code is more readable. Is there
a way to stop that behaviour or is there a kind of trim function i
can apply to get rid of the 2 extra lines?

Thanks
 
F

flupke

flupke said:
Hi,

i have php script where i use a heredoc type of variabele
to print out some html. In the heredoc, i use the values of
several other vars.

php snippet:

$div=<<<END_DIV
<DIV class="linkblock">
<DIV class="linkblock_title">$titel_name</DIV>
$url
</DIV>
</DIV>

So the variabele div is a heredoc which will contain some
html woth the vars titel_name and url in it.

1) Can i do this in python and how?

To answer the first part of the question: Yes
div="""<DIV class="linkblock">
<DIV class="linkblock_title">%s</DIV>
%s
</DIV>
"""

print div % (title_name,url) works.
Any idea on the second problem?
 
P

Peter Otten

flupke said:
2) Consider following heredoc

end_html="""</BODY>
</HTML>"""

I have to write it like this to avoid having an empty line added above
AND below the actual string when using it.

Why would you care about whitespace in HTML?
So this would produce the extra empty lines

end_html="""
</BODY>
</HTML>
"""

I actually think that the last piece of code is more readable. Is there
a way to stop that behaviour or is there a kind of trim function i
can apply to get rid of the 2 extra lines?
.... here's how \
.... to escape \
.... newlines"""
here's how to escape newlines
Peter
 
M

Michael Geary

flupke said:
2) Consider following heredoc

end_html="""</BODY>
</HTML>"""

I have to write it like this to avoid having an empty line added
above AND below the actual string when using it.
So this would produce the extra empty lines

end_html="""
</BODY>
</HTML>
"""

I actually think that the last piece of code is more readable. Is
there a way to stop that behaviour or is there a kind of trim
function i can apply to get rid of the 2 extra lines?

If you can live with the newline at the end of the text (which in most cases
is what you want anyway), this is the cleanest way to do it:

end_html = """\
</BODY>
</HTML>
"""

Or, you can get rid of both newlines this way:

end_html = """
</BODY>
</HTML>
"""[1:-1]

-Mike
 
F

flupke

Michael said:
flupke said:
2) Consider following heredoc

end_html="""</BODY>
</HTML>"""

I have to write it like this to avoid having an empty line added
above AND below the actual string when using it.
So this would produce the extra empty lines

end_html="""
</BODY>
</HTML>
"""

I actually think that the last piece of code is more readable. Is
there a way to stop that behaviour or is there a kind of trim
function i can apply to get rid of the 2 extra lines?

If you can live with the newline at the end of the text (which in
most cases is what you want anyway), this is the cleanest way to do
it:

end_html = """\
</BODY>
</HTML>
"""

Or, you can get rid of both newlines this way:

end_html = """
</BODY>
</HTML>
"""[1:-1]

-Mike

Thanks for the sollution!
 
F

flupke

Peter said:
Why would you care about whitespace in HTML?

For the same reason i care about nicely spaced code. To make
it readable. I do not use any HTML editor so i want the html code
to be clean and easily readable.
... here's how \
... to escape \
... newlines"""
here's how to escape newlines

Peter

Thanks
 
M

Michael Geary

Michael said:
If you can live with the newline at the end of the text
(which in most cases is what you want anyway), this
is the cleanest way to do it:

end_html = """\
</BODY>
</HTML>
"""

Or, you can get rid of both newlines this way:

end_html = """
</BODY>
</HTML>
"""[1:-1]
Thanks for the sollution!

De nada.

Now that I think of it, if you do want to get rid of both newlines, here are
a couple of other ways to do it:

end_html = """\
</BODY>
</HTML>\
"""

That is a bit ugly, but this seems reasonable (and better than the [1:-1]
slice trick):

end_html = """\
</BODY>
</HTML>"""

-Mike
 
?

=?ISO-8859-1?Q?Xavier_Mart=EDnez?=

Michael said:
Now that I think of it, if you do want to get rid of both newlines,
here are
a couple of other ways to do it:

end_html = """\
</BODY>
</HTML>\
"""

That is a bit ugly, but this seems reasonable (and better than the [1:-1]
slice trick):

end_html = """\
</BODY>
</HTML>"""

-Mike
Just for completeness, you can also use lstrip(), rstrip() and strip()
string methods to remove whitespace at the beggining, end or both parts
of a string, respectively. I.e.:

end_html = """
</BODY>
</HTML>
""".lstrip()

will get rid of the beginning newline. Using strip(), will get rid of
both.

I still prefer Mike's backslash solution, as getting rid of the newline
is done at parse time rather than at run time. Also lstrip() will eat
too much space if the first line of the string ("</BODY") is indented
(and you want to preserve that indentation).

- Xavier
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top