Thread: [Rest2web-develop] processing uservalues as a test condition in a template
Brought to you by:
mjfoord
From: Ben <bi...@ma...> - 2008-03-22 17:57:33
|
Hello, First of all, thank you very much for rest2web, again and again, that's a great piece of software for maintaining websites with ReStructuredText! I'm afraid I've a very very silly question so I apologize in advance for such a request, however I'm a bit stuck. I've discovered that I actually can embedded some code in the ReStructured Text file, for instance, this syntax is valid and does what it has to do: --8<----8<----8<----8<----8<----8<----8<----8<----8<----8<----8<----8<----8<-- My ReST doc ============= .. raw:: html <# print_details(default_section, page_title='', item_wrapper='<div class="rightbox"><h2>Articles</h2><ul>%s</ul></div>', do_description=False) #> Some other stuff ---------------------------------- --8<----8<----8<----8<----8<----8<----8<----8<----8<----8<----8<----8<----8<-- However, instead of adding this code in my (numerous!) ReST files and mix code and content, I would like to have a specific value for that (for instance, a value such as 'localnavbar: yes' in the local pages uservalues) and process the condition on the template side. And I'm afraid this is a bit too audacious for me, could someone give me a hand? In my template, I tried: --8<----8<----8<----8<----8<----8<----8<----8<----8<----8<----8<----8<----8<-- if <% localnavbar %>==yes: print_details(default_section, page_title='', item_wrapper='<div class="rightbox"><h2>Articles</h2><ul>%s</ul></div>', do_description=False) else: print "<!-- nope -->" #> --8<----8<----8<----8<----8<----8<----8<----8<----8<----8<----8<----8<----8<-- r2w complains a lot about <% localnavbar %> as an invalid syntax. I monkey-like tried thispage['localnavbar'] etc. but without success. I think I'm clearly missing something from: http://www.voidspace.org.uk/python/rest2web/reference/uservalues.html but I cand find what! Any help would be much appreciated. Many thanks, -- Ben |
From: Michael F. <fuz...@vo...> - 2008-03-22 18:02:59
|
Hello Ben, I think the syntax you want is something like: <# if ocalnavbar == 'yes': print_details(default_section, page_title='', item_wrapper='<div class="rightbox"><h2>Articles</h2><ul>%s</ul></div>', do_description=False) else: print "<!-- nope -->" #> Michael Ben wrote: > Hello, > > First of all, thank you very much for rest2web, again and again, that's > a great piece of software for maintaining websites with ReStructuredText! > > I'm afraid I've a very very silly question so I apologize in advance for > such a request, however I'm a bit stuck. > > I've discovered that I actually can embedded some code in the > ReStructured Text file, for instance, this syntax is valid and does what > it has to do: > > --8<----8<----8<----8<----8<----8<----8<----8<----8<----8<----8<----8<----8<-- > > My ReST doc > ============= > > .. raw:: html > > <# print_details(default_section, page_title='', item_wrapper='<div > class="rightbox"><h2>Articles</h2><ul>%s</ul></div>', > do_description=False) #> > > Some other stuff > ---------------------------------- > > --8<----8<----8<----8<----8<----8<----8<----8<----8<----8<----8<----8<----8<-- > > > However, instead of adding this code in my (numerous!) ReST files and > mix code and content, I would like to have a specific value for that > (for instance, a value such as 'localnavbar: yes' in the local pages > uservalues) and process the condition on the template side. > And I'm afraid this is a bit too audacious for me, could someone give me > a hand? > > In my template, I tried: > > --8<----8<----8<----8<----8<----8<----8<----8<----8<----8<----8<----8<----8<-- > > if <% localnavbar %>==yes: > print_details(default_section, page_title='', item_wrapper='<div > class="rightbox"><h2>Articles</h2><ul>%s</ul></div>', > do_description=False) > else: > print "<!-- nope -->" > #> > > --8<----8<----8<----8<----8<----8<----8<----8<----8<----8<----8<----8<----8<-- > > r2w complains a lot about <% localnavbar %> as an invalid syntax. I > monkey-like tried thispage['localnavbar'] etc. but without success. I > think I'm clearly missing something from: > http://www.voidspace.org.uk/python/rest2web/reference/uservalues.html > but I cand find what! > > Any help would be much appreciated. > Many thanks, > > -- Ben > > > > ------------------------------------------------------------------------- > This SF.net email is sponsored by: Microsoft > Defy all challenges. Microsoft(R) Visual Studio 2008. > http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/ > _______________________________________________ > Rest2web-develop mailing list > Res...@li... > https://lists.sourceforge.net/lists/listinfo/rest2web-develop > |
From: Ben <bi...@ma...> - 2008-03-22 18:16:15
|
Michael Foord wrote: > if localnavbar == 'yes' Hi, Michael, Thank you for your email and have a good Easter holiday! I'm afraid I tried this syntax as well, and I get an error since the localnavbar doesn't seem to be defined [1]. This is what I've put in 2 rest files header: uservalues localnavbar: yes /uservalues (to let you know, about 30 other files don't have this value defined, if it matters?) and the error is: Processing indexfile. Guessing encoding. We guessed: UTF-8 Building stage.html [err] Traceback (most recent call last): [err] File "/usr/bin/r2w", line 170, in <module> [err] count = main(options, config) [err] File "/usr/bin/r2w", line 103, in main [err] return processor.walk() [err] File "/var/lib/python-support/python2.5/rest2web/restprocessor.py", line 468, in walk [err] errorcheck = self.execute_safely(self.buildsection) [err] File "/var/lib/python-support/python2.5/rest2web/restprocessor.py", line 218, in execute_safely [err] val = function(*args, **keywargs) [err] File "/var/lib/python-support/python2.5/rest2web/restprocessor.py", line 1463, in buildsection [err] thepage = render_well(temp_file, namespace, final_encoding=final_encoding) [err] File "/var/lib/python-support/python2.5/rest2web/embedded_code.py", line 89, in render_well [err] template = render(template, namespace, uservalues,final_encoding=final_encoding) [err] File "/var/lib/python-support/python2.5/rest2web/embedded_code.py", line 72, in render [err] value = fun(occ, namespace) [err] File "/var/lib/python-support/python2.5/rest2web/embedded_code.py", line 142, in render_value_exec [err] exec codeobj in namespace [err] File "<string>", line 2, in <module> [err] NameError: name 'localnavbar' is not defined [err] (for your information, stage.txt does not have such a localnavbar value) -- Ben [1] yes, I've tried again and I fixed the little typo in your example (ocalnavbar -> localnavbar). |
From: Michael F. <fuz...@vo...> - 2008-03-22 18:20:36
|
Ben wrote: > Michael Foord wrote: > >> if localnavbar == 'yes' >> > > Hi, Michael, > > Thank you for your email and have a good Easter holiday! > I'm afraid I tried this syntax as well, and I get an error since the > localnavbar doesn't seem to be defined [1]. > > This is what I've put in 2 rest files header: > uservalues > localnavbar: yes > /uservalues > > (to let you know, about 30 other files don't have this value defined, > if it matters?) > > This code is being executed for a file that doesn't have the variable defined - i.e. stage.txt. Is this in your template? If so it will be executed for every file. You could get round this by putting something like the following *before* the code: try: localnavbar except NameError: localnavbar = 'no' Michael > and the error is: > > Processing indexfile. > Guessing encoding. We guessed: UTF-8 > Building stage.html > [err] Traceback (most recent call last): > [err] File "/usr/bin/r2w", line 170, in <module> > [err] count = main(options, config) > [err] File "/usr/bin/r2w", line 103, in main > [err] return processor.walk() > [err] File > "/var/lib/python-support/python2.5/rest2web/restprocessor.py", line 468, > in walk > [err] errorcheck = self.execute_safely(self.buildsection) > [err] File > "/var/lib/python-support/python2.5/rest2web/restprocessor.py", line 218, > in execute_safely > [err] val = function(*args, **keywargs) > [err] File > "/var/lib/python-support/python2.5/rest2web/restprocessor.py", line > 1463, in buildsection > [err] thepage = render_well(temp_file, namespace, > final_encoding=final_encoding) > [err] File > "/var/lib/python-support/python2.5/rest2web/embedded_code.py", line 89, > in render_well > [err] template = render(template, namespace, > uservalues,final_encoding=final_encoding) > [err] File > "/var/lib/python-support/python2.5/rest2web/embedded_code.py", line 72, > in render > [err] value = fun(occ, namespace) > [err] File > "/var/lib/python-support/python2.5/rest2web/embedded_code.py", line 142, > in render_value_exec > [err] exec codeobj in namespace > [err] File "<string>", line 2, in <module> > [err] NameError: name 'localnavbar' is not defined > [err] > > (for your information, stage.txt does not have such a localnavbar value) > > -- Ben > > [1] yes, I've tried again and I fixed the little typo in your example > (ocalnavbar -> localnavbar). > > ------------------------------------------------------------------------- > This SF.net email is sponsored by: Microsoft > Defy all challenges. Microsoft(R) Visual Studio 2008. > http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/ > _______________________________________________ > Rest2web-develop mailing list > Res...@li... > https://lists.sourceforge.net/lists/listinfo/rest2web-develop > |
From: Ben <bi...@ma...> - 2008-03-22 18:35:17
|
> This code is being executed for a file that doesn't have the variable > defined - i.e. stage.txt. > (snip) > try: > localnavbar Oh, yes, Michael, it was that simple! Thank you so much. it's perfectly working now! Many many many thanks. -- Ben |