Path: csiph.com!usenet.pasdenom.info!aioe.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed4.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'static': 0.04; 'output': 0.05; 'true,': 0.05; '"""': 0.07; 'none:': 0.07; 'variables': 0.07; 'false,': 0.09; 'inserted': 0.09; 'insertion': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'skip:$ 30': 0.09; 'subject:build': 0.09; 'python': 0.11; 'def': 0.12; 'template': 0.14; "'email": 0.16; 'conditional': 0.16; 'create:': 0.16; 'fields:': 0.16; 'received:80.91.229.3': 0.16; 'received:dip0.t-ipconnect.de': 0.16; 'received:plane.gmane.org': 0.16; 'received:t-ipconnect.de': 0.16; 'sorting': 0.16; 'subject:most': 0.16; 'true:': 0.16; 'student': 0.16; 'language': 0.16; 'wrote:': 0.18; 'variable': 0.18; 'advance.': 0.19; "skip:' 30": 0.19; 'thoughts': 0.19; 'this?': 0.23; 'header:User-Agent:1': 0.23; 'jquery': 0.24; 'looks': 0.24; 'this:': 0.26; 'header:X -Complaints-To:1': 0.27; 'chris': 0.29; 'list:': 0.30; 'code': 0.31; 'lines': 0.31; "skip:' 10": 0.31; 'class': 0.32; 'another': 0.32; 'subject:the': 0.34; 'but': 0.35; 'false': 0.36; 'yield': 0.36; 'method': 0.36; 'thanks': 0.36; 'subject:?': 0.36; 'searching': 0.37; 'skip:o 20': 0.38; 'sometimes': 0.38; 'skip:. 20': 0.38; 'to:addr:python-list': 0.38; 'realize': 0.39; 'sure': 0.39; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'how': 0.40; 'skip:u 10': 0.60; 'most': 0.60; 'name:': 0.61; 'email addr:gmail.com': 0.63; 'default': 0.69; 'ideas.': 0.84 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Peter Otten <__peter__@web.de> Subject: Re: What is the most pythonic way to build up large strings? Date: Sat, 08 Feb 2014 09:51:19 +0100 Organization: None References: Mime-Version: 1.0 Content-Type: text/plain; charset="ISO-8859-1" Content-Transfer-Encoding: 7Bit X-Gmane-NNTP-Posting-Host: p57bd9cc2.dip0.t-ipconnect.de User-Agent: KNode/4.7.3 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 96 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1391849487 news.xs4all.nl 2860 [2001:888:2000:d::a6]:50780 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:65653 cstrutton11@gmail.com wrote: > I am writing a couple of class methods to build up several lines of html. > Some of the lines are conditional and most need variables inserted in > them. Searching the web has given me a few ideas. Each has its pro's and > cons. > > The best I have come up with is: > > > def output_header_js(self, jquery=True, theme=None): > if self.static_path is None : > return None > > if jquery is True: > output = '"'% static > output += '"'% static > > if theme is not None: > output += ' theme output += 'rel="stylesheet" type="text/css" />' > > output += '"' % "static" > > > I realize that a lot of the above looks repetitive but it is designed to > eliminate boilerplate HTML. I sometimes use a variation of the above def output_header(...): if jquery: yield """src=...""" if theme is not None: yield """ I have another method that will build some javascript that looks like > this: > > $('#StudentTableContainer').jtable({ > title: 'The Student List', > paging: true, //Enable paging > pageSize: 10, //Set page size (default: 10) > sorting: true, //Enable sorting > defaultSorting: 'Name ASC', //Set default sorting > actions: { > listAction: '/Demo/StudentList', > deleteAction: '/Demo/DeleteStudent', > updateAction: '/Demo/UpdateStudent', > createAction: '/Demo/CreateStudent' > }, > fields: { > StudentId: { > key: true, > create: false, > edit: false, > list: false > }, > Name: { > title: 'Name', > width: '23%' > }, > EmailAddress: { > title: 'Email address', > list: false > }, > ... > > Almost every line in this code will require variable insertion or if > statements. > > Any thoughts on how to improve this? Thanks in advance. Chris Again, you can build this with Python proper "... title: {title} ...".format(title="The Student List", ...) but a template language will make sure that your data is escaped properly, think "... title: {title} ...".format(title="The Student's List", ...)