0

I used to use the if statement enclosing HTML in my php pages to repeatedly produce HTML units with similar content. Would like to know the equivalent in Python Pyramid.

eg:

<?php if($i<10): ?>
<div class=''x1>
<?php echo $i?>
</div>
<?php endif;?>

Want to do similarly in Pyramid framework in python within a chameleon .pt template file.

Currently doing this:

 Post1="<div class='posts'>\n<h1 class='content-subhead'> Posted on "
    Post2="</h1><!-- A single blog post --><section class='post'><header class='post-header'><h2 class='post-title'><a href='blog/"
    Post3="</h2><p class='post-meta'>By Blaise M Crowly</p></header><div class='post-description'>"
    Post4="</div><a href='blog/"
    Post5="'>Read More </a></section></div> "
    Post = ""

    phazer = cur.fetchall()
    for each in phazer:
        Text = strip_tags(each[2])
        Text = Text[:400]
        Post=Post+Post1+str(each[4])+" "+str(each[5])+" "+str(each[6])+" "+str(each[7])+Post2+str(each[1])+"'> "+str(each[0])+"</a>"+Post3+str(Text)+Post4+str(each[1])+Post5

    return render_to_response('blaise:templates/blog.pt',{'posts':Post},request=request)
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
BMC
  • 591
  • 3
  • 7
  • 22
  • creating a variable and attaching everything including the html tags before passing it to the renderer – BMC Feb 18 '14 at 19:26
  • Posting the code you have will make it much easier to help you. – kylieCatt Feb 18 '14 at 19:26
  • What template language are you using? Mako, Jinja2, Chameleon ZPT? Pyramid can use all of those and more, but the syntax between those template languages can vary widely. – Martijn Pieters Feb 18 '14 at 19:28
  • @MartijnPieters using Chameleon. – BMC Feb 18 '14 at 19:29
  • @IanAuld added my code to the question – BMC Feb 18 '14 at 19:30
  • 2
    That logic should be moved to the template itself. In Pyramid, anytime you're constructing HTML fragments in response handlers is a sign of a serious problem in your design. – Max Noel Feb 18 '14 at 19:31
  • Off Topic: I am really curious why people love to down vote? There should be a comment field to fill up why someone is downvoting! Apologies for irrelevant stuffs! – Sharif Mamun Feb 18 '14 at 19:34
  • 1
    @S.M.AlMamun: That's a discussion for meta (and has already [been discussed at length](http://meta.stackexchange.com/questions/135/encouraging-people-to-explain-downvotes). – Martijn Pieters Feb 18 '14 at 19:37
  • 1
    @S.M.AlMamun: but the tooltip on the downvote button is a good indicator as to why someone might downvote. – Martijn Pieters Feb 18 '14 at 19:37
  • @MartijnPieters: thanks for the info. It would be really beneficial at least for the newcomers! – Sharif Mamun Feb 18 '14 at 19:40
  • @MaxNoel I know, am wondering how to get it done. – BMC Feb 18 '14 at 19:57
  • 2
    @BMC Your templating engine should provide a looping construct: use that. If it doesn't, use another one (I recommend Mako). – Max Noel Feb 18 '14 at 20:24

0 Answers0