On 8/10/07, Marcin Kasperski <[EMAIL PROTECTED]> wrote:
>
>
> Just a few questions about pylons programming style.
>
> 1) h.url_for is a great idiom to generate urls to dynamic
>    pages. But what should be used to generate links to
>    *static* resources? There is h.stylesheet_link_tag,
>    but what should I use to generate links to static HTML,
>    images, binary files etc?

If the number of files is small you can use a named path for each:

map.connect("banner", "/images/foo.jpg")
h.url_for("banner")
    => "/images/foo.jpg"

If they're in a predictable location under a static directory, you can
use url_for with variables:

map.connect("attachments", "/attachments/:rec_id/:(filename).jpg
h.url_for("attachments", rec_id=5, filename="foo")
    => "/attachments/5/foo.jpg"

> 2) Why split between ./public and ./templates? I find it
>    somewhat confusing, it is usually easier to keep images
>    together with the templates which refer to them...

Having a separate templates directory makes sense from an MVC
standpoint because the controller interacts directly with templates
but not with static files.  Too many files in a template directory
makes it hard to keep track of which template goes with which
controller(s).  It's also clean because the controller can
theoretically switch templates, choosing any file in the templates
directory.

>    Is it possible to have static controller serving some
>    files, and template controller handling other files
>    from the same directory hierarchy (using file
>    extensions to choose)? Or maybe some other idiom
>    should be applied?

If you want to combine your templates and static files, adjust the
paths in environment.py to point to the same directory.  To hide
template files in the public directory, add an 'ignore_extensions'
argument  to StaticURLParser in middleware.py.  To prevent controllers
from rendering non-templates... write your render() calls carefully.

However, I suspect that getting with the program and keeping templates
and static files separate will lead to easier maintenance in the long
run.

> 3) What is the typical approach to generate things like
>    table of contents, page-list menu etc. Do people tend
>    to write such things manually? If I would like to
>    scan my package for files and generate indexes automatically,
>    should I use pkg_resources, os, or sth else?

Dynamic applications usually require dynamic indexes, so you write
these like any other dynamic page.  If you have a lot of static files
you want to index, you can use a template to create a static
index.html in websetup.py.  If you want to guarantee the index is up
to date every time the application starts, put the code in
environment.py or middleware.py.  (You'll probably want to put the
actual code in 'lib' module function, and call that.)

> 4) What about search? Can you recommend some library which
>    would allow to implement simple search thorough small
>    site without using google?

Again, dynamic sites usually require custom database searches.

If you want to search your static files, one of the third-party search
packages like pyLucene might help.  If you want to search your
templates, it would probably be additional work because the search
engine would index the raw files, and you'd have to compensate  for
the data values, URL, and hyperlinks.  (Obviously a third-party search
engine can't resolve $h.url_for(...).)

Or you can implement a search a the Apache level, treating the
application as quasi-static files.  I don't know what software you'd
use for this, maybe ht://dig.
Perhaps you can get pyLucene to index the rendered pages rather than
the raw files.

> 5) How would you implement simple constants referred thorough
>    the site in different templates? At the moment I have sth
>    like
>         _const.mako
>           <%def name="author_name()" buffered="True">Marcin Kasperski</%def>
>           ...
>    and in actual pages
>        <%namespace file="_const.html" name="const"/>
>        ...
>        Autor: ${author_name()}
>    But ... using functions for simple constants seems to be
>    overkill. And it seems mako does not handle exporting
>    non-functions, I can write
>        <%! author_name = u'Marcin Kasperski' %>
>    in _const.mako, but I am not able to refer to this variable from
>    other templates.

I put those in the base controller's .__call__ method:

c.author_name = u"Marcin Kaperski".

They'll be passed to the template along with the page-specific variables.

-- 
Mike Orr <[EMAIL PROTECTED]>

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"pylons-discuss" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/pylons-discuss?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to