genshi

How to initialize global variables in TurboGears 2 with values from a table

流过昼夜 提交于 2019-12-24 21:07:24
问题 I need a global variable that I can call from the templates. I edited app_globals.py in lib directory to declare PATH_TO_IMAGES like this class Globals(object): """Container for objects available throughout the life of the application. One instance of Globals is created during application initialization and is available during requests via the 'app_globals' variable. """ PATH_TO_IMAGES = "" def __init__(self): """Do nothing, by default.""" pass Now I can call from any template the image path

Javascript templates

你说的曾经没有我的故事 提交于 2019-12-24 08:35:11
问题 Does anyone know if is there any way to create javascrit templates in Genshi? I mean, I need a .js file where I can use directives like <py:for> and so. Any idea? Thanks! 回答1: You can write it directly in html like this: <script type="text/javascript"> <py:if test="func1"> /*&lt;![CDATA[*/ function func1() { } /*]]&gt;*/ </py:if> </script> note: code inside CDATA will be right escaped, so you can use '>','<' as usual. 来源: https://stackoverflow.com/questions/3201343/javascript-templates

Why single textarea mess all following xhtml?

混江龙づ霸主 提交于 2019-12-12 20:01:50
问题 I encounter a problem in my web program. I got a textarea in my form, sometimes there is nothing in textarea, so genshi template engine just output it as <textarea xxxx /> and here comes the problem, all following tags are in the textarea. Why all browser can't handle single textarea correctly? If I write it as <textarea xxxx></textarea> and everything works fine. Why a single textarea messes following tags in xhtml? 回答1: Because you are, presumably, serving your XHTML with a text/html

Genshi - how to print out all variables in scope

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-11 00:42:56
问题 Quite simply I'd like to print out all variables that are in scope in my genshi template, as a debugging and discovery measure. Is there a way to do it? 回答1: The standard Python function locals() (which returns a dict) works for me. I'm using Genshi 0.5.1, and as you'll see, everything seems to be in __data__ . ${repr(locals())} 来源: https://stackoverflow.com/questions/1050452/genshi-how-to-print-out-all-variables-in-scope

What is the method of doing nl2br in Genshi?

ⅰ亾dé卋堺 提交于 2019-12-09 23:14:18
问题 hiyas. I using Genshi+Pylons. please teach me, how use \n to <br/>tag in Genshi? I hope to obtain the same result as " nl2br " in php to change line. Or, does not the solution exist? i'm assign template to some text. (genshi template) <p>${c.message}</p> Im tried. case 1: (python code) c.message = """ foo bar """ NG. display result is "foo bar" case 2: (python code) c.message = """ foo<br /> bar """ NG. display result is "foo<br />bar". displayed escaped stirings! It was a same deal as <br/>

python/genshi newline to html <p> paragraphs

时光怂恿深爱的人放手 提交于 2019-12-07 17:38:42
问题 I'm trying to output the content of a comment with genshi, but I can't figure out how to transform the newlines into HTML paragraphs. Here's a test case of what it should look like: input: 'foo\n\n\n\n\nbar\nbaz' output: <p>foo</p><p>bar</p><p>baz</p> I've looked everywhere for this function. I couldn't find it in genshi or in python's std lib. I'm using TG 1.0. 回答1: def tohtml(manylinesstr): return ''.join("<p>%s</p>" % line for line in manylinesstr.splitlines() if line) So for example,

What is the method of doing nl2br in Genshi?

三世轮回 提交于 2019-12-04 20:07:36
hiyas. I using Genshi+Pylons. please teach me, how use \n to <br/>tag in Genshi? I hope to obtain the same result as " nl2br " in php to change line. Or, does not the solution exist? i'm assign template to some text. (genshi template) <p>${c.message}</p> Im tried. case 1: (python code) c.message = """ foo bar """ NG. display result is "foo bar" case 2: (python code) c.message = """ foo<br /> bar """ NG. display result is "foo<br />bar". displayed escaped stirings! It was a same deal as <br/> as for <br />. Postscript. I want to avoid using the pre tag. thanks. When it is not easy to read

Insert javascript at top of including file in Jinja 2

狂风中的少年 提交于 2019-11-30 05:09:58
In Jinja2, I would like the following to work as it looks like it should, by running: from jinja2 import Environment, FileSystemLoader env = Environment(loader=FileSystemLoader('.')) template = env.get_template('x.html') print template.render() Essentially the objective is to coalesce all the javascript into the <head> tags by using a a {% call js() %} /* some js */ {% endcall %} macro. x.html <html> <head> <script type="text/javascript> {% block head_js %}{% endblock %} </script> </head> <body> {% include "y.html" %} </body> </html> y.html {% macro js() -%} // extend head_js {%- block head_js

Why is __init__ not called after __new__ SOMETIMES

爷,独闯天下 提交于 2019-11-29 06:30:36
Let me start with this is not a repeat of Why does __init__ not get called if __new__ called with no args . I have tried to carefully construct some sample code for __new__ and __init__ that has no explanation I can find. The basic parameters: There is a base class called NotMine as it comes from another library (I'll disclose at the end, not important here) That class has an __init__ method that in turn calls a _parse method I need to override the _parse method in subclasses which subclass I'm creating is not known until invocation I know there are factory design methods but I cannot use them

Insert javascript at top of including file in Jinja 2

随声附和 提交于 2019-11-29 02:57:37
问题 In Jinja2, I would like the following to work as it looks like it should, by running: from jinja2 import Environment, FileSystemLoader env = Environment(loader=FileSystemLoader('.')) template = env.get_template('x.html') print template.render() Essentially the objective is to coalesce all the javascript into the <head> tags by using a a {% call js() %} /* some js */ {% endcall %} macro. x.html <html> <head> <script type="text/javascript> {% block head_js %}{% endblock %} </script> </head>