473,625 Members | 3,318 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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="linkbloc k">
<DIV class="linkbloc k_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 11448

"flupke" <fl****@nonexis tingdomain.com> schreef in bericht
news:3U******** ************@he be.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="linkbloc k">
<DIV class="linkbloc k_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="linkbloc k">
<DIV class="linkbloc k_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
3673
by: bigoxygen | last post by:
Is it possible to invoke a function from inside heredoc? Thanks
6
6243
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 except: In the generated html I have a number of checkboxes. I fill in the checkboxes from a boolean value in a mySQL table. To make this easier I use a function that takes the boolean and return either 'checked' or ''. This worked well when I was...
5
11811
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 $MYVAR, $a; ?>
2
2151
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 to replace $myarray and another variable $myarray I call heredoc this way:
1
29338
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 called it polluting the global namespace. This article explores what happens when the global namespace becomes polluted and how to avoid this condition. The opinions expressed in this article are those of the author alone although many have...
7
5563
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
1647
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> END;
2
1386
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 here.............................................. ...................................................................... $lang = mysql_query("SELECT * FROM lang where selected != '*'");...
2
2309
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 pages are giving the same error on different lines but it seems to be related to the HEREDOC syntx?
0
8256
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8189
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8635
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8497
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6118
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4193
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2621
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1803
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1500
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.