can regular ol' python do a php include?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • John Salerno

    #1

    can regular ol' python do a php include?

    Whenever I turn an eye back toward website design, I find myself making
    frequent use of PHP's include function (or SSI's include directive). So
    I'm curious, is there a way to do this with Python using just the
    standard library?

    I know that PSP allows you to embed like PHP, but I don't have access to
    this. Basically I'm working on a server that has Python 2.2 available
    for me to use. I could also use PHP's include, but I like sticking with
    Python. SSI isn't available or I'd probably just use that.

    I get the feeling the answer is no, but maybe there's something I've
    missed that will allow me to do this.

    Thanks.
  • Fredrik Lundh

    #2
    Re: can regular ol' python do a php include?

    John Salerno wrote:
    Whenever I turn an eye back toward website design, I find myself making
    frequent use of PHP's include function (or SSI's include directive). So
    I'm curious, is there a way to do this with Python using just the
    standard library?
    you don't even need anything from the standard library to inserting output
    from one function into given text...

    text = "... my page with %(foo)s markers ..."

    print text % dict(foo=functi on())

    (doing this with output from another Python script is of course also easy;
    just use execfile, and collect the output, but that's pretty silly when you can
    import the darn thing, and call it instead).

    </F>



    Comment

    • John Salerno

      #3
      Re: can regular ol' python do a php include?

      Fredrik Lundh wrote:
      you don't even need anything from the standard library to inserting output
      from one function into given text...
      >
      text = "... my page with %(foo)s markers ..."
      >
      print text % dict(foo=functi on())
      Wow, thanks. So if I have a file called header.html that contains all my
      header markup, I could just insert this line into my html file? (which I
      suppose would become a py file)

      print open('header.ht ml').read()

      Not quite as elegant as include('header .html'), but it seems like it
      would work.

      Comment

      • Steve Holden

        #4
        Re: can regular ol' python do a php include?

        John Salerno wrote:
        Fredrik Lundh wrote:
        >
        >
        >>you don't even need anything from the standard library to inserting output
        >>from one function into given text...
        >>
        > text = "... my page with %(foo)s markers ..."
        >>
        > print text % dict(foo=functi on())
        >
        >
        Wow, thanks. So if I have a file called header.html that contains all my
        header markup, I could just insert this line into my html file? (which I
        suppose would become a py file)
        >
        print open('header.ht ml').read()
        >
        Not quite as elegant as include('header .html'), but it seems like it
        would work.
        Don't overlook the fact that any *real* percent signs you want in your
        template content should be represented as a doubled percent sign: %%

        regards
        Steve
        --
        Steve Holden +44 150 684 7255 +1 800 494 3119
        Holden Web LLC/Ltd http://www.holdenweb.com
        Skype: holdenweb http://holdenweb.blogspot.com
        Recent Ramblings http://del.icio.us/steve.holden

        Comment

        • John Salerno

          #5
          Re: can regular ol' python do a php include?

          John Salerno wrote:
          print open('header.ht ml').read()
          >
          Not quite as elegant as include('header .html'), but it seems like it
          would work.
          Spoke too soon maybe. Does using the above mean that I have to create an
          entire Python file rather than an HTML file? What I'm looking for is to
          be able to use pure HTML, but just stick in a line of Python when I need
          to include something.

          It doesn't seem to work this way though, unless I'm doing something wrong.

          Comment

          • Fredrik Lundh

            #6
            Re: can regular ol' python do a php include?

            John Salerno wrote:
            Spoke too soon maybe. Does using the above mean that I have to create an
            entire Python file rather than an HTML file? What I'm looking for is to
            be able to use pure HTML, but just stick in a line of Python when I need
            to include something.
            "include" is a PHP statement, not an HTML statement.
            It doesn't seem to work this way though, unless I'm doing something wrong.
            Python CGI scripts are Python programs that generate output, not HTML
            files that are run though some HTML filter thingie on the way out. if
            you want the latter, you have to use a suitable template engine, and
            configure your web server to run your files through that engine on the
            way out. mod_python is one such tool:

            Now, next, and beyond: Tracking need-to-know trends at the intersection of business and technology


            spyce is another one:

            Spyce - Python Server Pages: a server-side language that supports simple and efficient Python-based dynamic HTML generation.


            </F>

            Comment

            • skip@pobox.com

              #7
              Re: can regular ol' python do a php include?


              JohnWhat I'm looking for is to be able to use pure HTML, but just
              Johnstick in a line of Python when I need to include something.

              Take a look at any of a number of different templating systems. Start here:



              Skip

              Comment

              • Larry Bates

                #8
                Re: can regular ol' python do a php include?

                John Salerno wrote:
                Whenever I turn an eye back toward website design, I find myself making
                frequent use of PHP's include function (or SSI's include directive). So
                I'm curious, is there a way to do this with Python using just the
                standard library?
                >
                I know that PSP allows you to embed like PHP, but I don't have access to
                this. Basically I'm working on a server that has Python 2.2 available
                for me to use. I could also use PHP's include, but I like sticking with
                Python. SSI isn't available or I'd probably just use that.
                >
                I get the feeling the answer is no, but maybe there's something I've
                missed that will allow me to do this.
                >
                Thanks.
                You might want to review:



                This comes just about as close to PHP-style "includes" (your term) as I
                have seen.

                -Larry Bates

                Comment

                Working...