def testTemplateInclude(self): t = jsontemplate.Template(self.include_template, more_formatters=self.include_formatter) d = {'profile': {'name': 'Bob', 'age': 13}} self.verify.Equal(t.expand(d), 'Bob is 13\n')
def __init__(self, output_dir): taste.StandardVerifier.__init__(self) self.output_dir = output_dir # Counter for unique filenames self.counter = 1 self.blog_template = jsontemplate.Template( _BLOG_HTML, default_formatter='html') self.html_template = jsontemplate.Template( _TEST_CASE_HTML, default_formatter='html') # Don't need any escaping here. self.js_template = jsontemplate.Template( _JS_EXAMPLE, default_formatter='raw')
def testLookupChain(self): chained = formatters.LookupChain([ formatters.PythonPercentFormat, self.include_formatter, ]) # Test that the cases from testPythonPercentFormat and testTemplateInclude # both work here. t = jsontemplate.Template(self.printf_template, more_formatters=chained) self.verify.Equal(t.expand(a=1.0 / 3), '0.33') t = jsontemplate.Template(self.include_template, more_formatters=chained) d = {'profile': {'name': 'Bob', 'age': 13}} self.verify.Equal(t.expand(d), 'Bob is 13\n')
def testPlural(self): chained = formatters.LookupChain([ formatters.Plural, ]) t = jsontemplate.Template( "{num-people} {num-people|plural? people person} in this group", more_formatters=chained) self.verify.Equal(t.expand({'num-people': 3}), '3 people in this group') self.verify.Equal(t.expand({'num-people': 1}), '1 person in this group')
def testPluralWithSpaces(self): chained = formatters.LookupChain([ formatters.Plural, ]) # This demonstrates how you can include spaces in the arguments. The first # thing after plural? is the separator char -- in this case, a comma. t = jsontemplate.Template( "{num-people}{num-people|plural?, people, person} in this group", more_formatters=chained) self.verify.Equal(t.expand({'num-people': 3}), '3 people in this group') self.verify.Equal(t.expand({'num-people': 1}), '1 person in this group')
def __init__(self): taste.StandardVerifier.__init__(self) # Counter for unique method names self.counter = 1 self.assertions = [] def ToJson(x): return json.dumps(x, indent=2) self.js_template = jsontemplate.Template( _HTML_TEMPLATE, default_formatter='raw', meta='[]', more_formatters=formatters.Json(ToJson))
def Expansion( self, template_def, dictionary, expected, ignore_whitespace=False, ignore_all_whitespace=False): """ Args: template_def: ClassDef instance that defines a Template. """ tested_template = jsontemplate.Template( *template_def.args, **template_def.kwargs) expanded = tested_template.expand(dictionary) json_str = json.dumps(dictionary, indent=2) self.WriteHighlightedHtml(template_def, json_str, expanded) # Mark test methods with the live-js label to generate the Live JavaScript # example. if taste.HasLabel(self.current_method, 'live-js'): self.WriteLiveJavaScriptExample(template_def, json_str, expanded) self.counter += 1
def Expansion(self, template_def, dictionary, expected, ignore_whitespace=False, ignore_all_whitespace=False, all_formatters=False): """ Args: template_def: taste.ClassDef instance. """ if all_formatters: template_def.kwargs[ 'more_formatters'] = formatters.PythonPercentFormat template = jsontemplate.Template(*template_def.args, **template_def.kwargs) left = expected right = template.expand(dictionary) self.LongStringsEqual(left, right, ignore_whitespace=ignore_whitespace)
def testTemplatesAreNotOpenedMoreThanOnce(self): t = jsontemplate.Template(""" {profile1|template-file include-test.jsont} {profile2|template-file include-test.jsont} """, more_formatters=self.include_formatter) d = { 'profile1': { 'name': 'Bob', 'age': 13 }, 'profile2': { 'name': 'Andy', 'age': 80 }, } # Do the expansion ... t.expand(d) # .. and make sure that the include-test.jsont file wasn't opened more than # once self.verify.Equal(formatters._open.open_count, 1)
def testBasic(self): t = jsontemplate.Template('{a}') self.verify.Equal(t.expand(a=1), '1')
# Page 1 and 2 have *different* data dictionaries ... PAGE_ONE_DATA = { 'title': "Itchy & Scratchy", 'desc': 'jerks', } PAGE_TWO_DATA = { 'title': 'Page Two', 'verb': 'bites', } # ... and *different* templates PAGE_ONE_TEMPLATE = jsontemplate.Template('<b>{title}</b> are {desc}', default_formatter='html') PAGE_TWO_TEMPLATE = jsontemplate.Template('{title} <i>{verb}</i>', default_formatter='html') # This is the skeleton we want to share between them. Notice that we use # {body|raw} to prevent double-escaping, because 'body' is *already HTML*. # 'title' is plain text. HTML_TEMPLATE = jsontemplate.Template(""" <html> <head> <title>{title}</title> </head> <body>{body|raw}</body> </html>
def TemplateThatCanRenderProfiles(template_str): return jsontemplate.Template(template_str, more_formatters=MoreFormatters(), default_formatter='html')
PAGE_TWO_TEMPLATE = TemplateThatCanRenderProfiles("""\ {profile|user-profile} <p>Here is the HTML for the profile above:</p> <pre>{profile|user-profile|html}</pre> """) # The same HTML template from reusing_the_outside.py. HTML_TEMPLATE = jsontemplate.Template(""" <html> <head> <title>{title}</title> </head> <body>{body|raw}</body> </html> """, default_formatter='html') # Same site as last time. # # Now go back to the 'Design Minimalism' blog post (linked from # http://code.google.com/p/json-template/). def Site(path_info): """Returns an HTML page.""" if path_info == '/one':
TABLE_TEMPLATE = jsontemplate.Template("""\ <html> <head> <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"> </script> <script type="text/javascript" src="http://ajax.aspnetcdn.com/ajax/jquery.dataTables/1.9.4/jquery.dataTables.min.js"> </script> <link rel="stylesheet" type="text/css" href="http://ajax.aspnetcdn.com/ajax/jquery.dataTables/1.9.4/css/jquery.dataTables.css" /> </head> <body> <p><code>{basename}</code> - {num_rows|commas} rows, {num_bytes|commas} bytes</p> <table class="data-table" align="center"> <thead> <tr> {.repeated section thead} <th>{@}</th> {.end} </tr> </thead> <tbody> {.repeated section rows} <tr> {.repeated section @} <td>{@}</td> {.end} </tr> {.end} </tbody> </table> </body> <script type="text/javascript"> $('.data-table').dataTable(); </script> </html> """, default_formatter='html', more_formatters=FORMATTERS)
def testPythonPercentFormat(self): t = jsontemplate.Template( self.printf_template, more_formatters=formatters.PythonPercentFormat) self.verify.Equal(t.expand(a=1.0 / 3), '0.33')
def EvaluationError(self, exception, template_def, data_dict): template = jsontemplate.Template(*template_def.args, **template_def.kwargs) self.Raises(exception, template.expand, data_dict)
} BODY_STYLE = jsontemplate.Template("""\ <!DOCTYPE html> <html> <head> <title>{.template TITLE}</title> <script type="text/javascript" src="{base_url}../../web/ajax.js"></script> <script type="text/javascript" src="{base_url}../../web/table/table-sort.js"></script> <link rel="stylesheet" type="text/css" href="{base_url}../../web/table/table-sort.css" /> <link rel="stylesheet" type="text/css" href="{base_url}../../web/wild.css" /> </head> <body onload="initPage(gUrlHash, gTables, gTableStates, kStatusElem);" onhashchange="onHashChange(gUrlHash, gTableStates, kStatusElem);"> <p id="status"></p> <p style="text-align: right"><a href="/">oilshell.org</a></p> {.template NAV} {.template BODY} </body> </html> """, default_formatter='html') # NOTE: {.link} {.or id?} {.or} {.end} doesn't work? That is annoying. NAV_TEMPLATE = jsontemplate.Template("""\
default=5, help='Number of server threads, i.e. simultaneous connections.') parser.add_option('--root-dir', dest='root_dir', type='str', default='_tmp', help='Directory to serve out of.') return parser HOME_PAGE = jsontemplate.Template("""\ <h3>latch</h3> {.repeated section pages} <a href="{@|htmltag}">{@}</a> <br/> {.end} """, default_formatter='html') LATCH_PATH_RE = re.compile(r'/-/latch/(\S+)$') # TODO: Rewrite latch.js using raw XHR, and get rid of jQuery. This could # interfere with pages that have jQuery already. LATCH_HEAD = """\ <script type='text/javascript' src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"> </script> <script src="/-/latch.js"></script> """
HOME_PAGE = jsontemplate.Template("""\ <html> <head> <title>webpipe home</title> <link href="/static/webpipe.css" rel="stylesheet"> </head> <body> <p align="right"> <a href="plugins/">plugins</a> - <a href="/">home</a> <p> <h2>webpipe</h2> <div id="scrolls"> <p> Active Scroll: <a href="s/{active_scroll|htmltag}">{active_scroll}</a> </p> <h4>Old Scrolls</h4> {.repeated section scrolls} <a href="s/{@|htmltag}">{@}</a> <br/> {.end} <p> (<a href="s/">browse</a>) </p> </div> </body> </html> """, default_formatter='html')
def testRepr(self): t = jsontemplate.Template('{a|repr}') self.verify.Equal(t.expand(a=u'\u00b5'), "u'\\xb5'")