473,387 Members | 1,693 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

heredoc and variables

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
Jul 18 '05 #1
7 11421

"flupke" <fl****@nonexistingdomain.com> schreef in bericht
news:3U********************@hebe.telenet-ops.be...
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?
Jul 18 '05 #2
flupke wrote:
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?


print """\ .... here's how \
.... to escape \
.... newlines"""
here's how to escape newlines


Peter

Jul 18 '05 #3
flupke wrote:
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
Jul 18 '05 #4
Michael Geary wrote:
flupke wrote:
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!
Jul 18 '05 #5
Peter Otten wrote:
flupke wrote:
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?


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.
print """\ ... here's how \
... to escape \
... newlines"""
here's how to escape newlines


Peter


Thanks
Jul 18 '05 #6
> Michael Geary wrote:
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]

flupke wrote: 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
Jul 18 '05 #7
Michael Geary wrote:
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
Jul 18 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: bigoxygen | last post by:
Is it possible to invoke a function from inside heredoc? Thanks
6
by: bill | last post by:
Thanks to those that taught me to use heredoc to embed html in php. I don't want to start the html in php vs php in html discussion again., heredoc works a treat for that which I am doing...
5
by: Sandman | last post by:
I dont think I understand them. I've read the section on scope in the manual inside out. I'm running PHP 5.2.0 Here is the code I'm working on: //include_me.php <?php $MYVAR = array(); global...
2
by: Matt F | last post by:
I can't seem to get heredoc to populate correctly with variables through a form. <textarea name="Template" rows="10" cols="80">Template Here</textarea> Contents could be something like: I want...
1
weaknessforcats
by: weaknessforcats | last post by:
C++: The Case Against Global Variables Summary This article explores the negative ramifications of using global variables. The use of global variables is such a problem that C++ architects have...
7
by: Jeff | last post by:
I have this: $content = <<<TD $D TD; that fails silently and stops php even though errors are turned on
2
by: Adam Cantrell | last post by:
How could I count the number of newlines in a heredoc variable? This outputs 0, but I thought heredoc preserved newlines? <? $test = <<<END <a href="test5.php">adam</a> <p> Hi There </p>...
2
by: someusernamehere | last post by:
hi, I have some heredoc on this way: $foo = <<<bar <form action="index.php" method="POST" name="user"> ............................................................................. HTML code...
2
by: ziycon | last post by:
I have a site that has been using the HEREDOC syntax for awhile, after a recent upgrade to the site a lot of pages are erroring with the below error, i can't figure it out at ll, any ideas? All the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.