Good Morning,

> -----Original Message-----
> From: trac-users@googlegroups.com On Behalf Of ea.ros...@gmail.com
> Sent: 03 October 2016 17:48
> 
> Hi,
> Effectively following on from creating a new menu item in mainnav (from Basic
> Egg Cooking tutorial) and creating a new page, I need to find a sane way of
> trying to maintain page consistency with Trac.
> ie, generating a page via this method does not pull in any CSS from Trac,
> neither does it pull in any blocks such as the banner or the mainnav bar.
> 
> Is there a normal way to do this? Using templates and presenting the page as
> a template may be a solution, but any example given on the docs only pulls in
> a static page from the server, and not a dynamically created one. There seems
> to be insufficient documentation on handling this - unless someone can point
> me in the right direction for docs?
> 
> ie:
> work from https://trac-hacks.org/wiki/EggCookingTutorial/BasicEggCooking
> change // abuffer = 'Hello world!' // to // abuffer = MyPythonScript() //
> 
> 
> I want to be able to have a block that tells Trac to switch it out to the
> banner/metanav/mainnav etc.
> 
> MyPythonScript turns out standard HTML, obviously I have full control over
> what comes from this, I just can't find info on what Trac wants, and how to
> handle it. Genshi?

Now that you understand the basic structure of a plugin I would recommend 
taking a look at something "similar" to what you want from trac-hacks [1] and 
see how they do it.  From what you write above you just need to stop generating 
whole HTML and, yes, work with the Genshi engine and the Trac response 
mechanisms (otherwise you will start bypassing the cookie handling etc. that 
Trac provides for you).

[1] https://trac-hacks.org/wiki/HackIndex

Note that you will need to implement some of the interfaces to get this all 
working, e.g.:

class MyPlugin(Component):
    """This component provides the <widget> views and declares the
    permissions: MY_PERM_1 and MY_PERM_2.
    """

    implements(INavigationContributor, IRequestHandler,
               IPermissionRequestor, ITemplateProvider)

...and the related (incomplete!) methods:

    # ----------------------------------------------------------------------- #
    # IRequestHandler methods
    def match_request(self, req):
        # match if we end with our root, optionally followed by further path
        # or query parameters...
        return req.path_info.startswith(r'/myplugin')

    # ----------------------------------------------------------------------- #
    def process_request(self, req):
        # MUST have at least ' MY_PERM_1' permission...
        req.perm.require(MY_PERM_1)

        html_template = None
        data = {}

        # `add_notice` adds the temporary banners at the top of the contents
        #add_notice(req, "type(req) = `%s`" % (type(req),))
        #add_notice(req, "req.path_info = `%s`" % (req.path_info,))

        # ...code here...

        # This tuple is for Genshi (template_name, data, content_type)
        # Without `data` the trac layout will not appear.
        return html_template, data, None

    # ----------------------------------------------------------------------- #
    # ---------------------------------------------- IPermissionRequestor --- #
    # ----------------------------------------------------------------------- #

    def get_permission_actions(self):
        # MY_PERM_2 includes MY_PERM_1...
        return (MY_PERM_1,
                (MY_PERM_2, (MY_PERM_1,)))

    # ----------------------------------------------------------------------- #
    # ------------------------------------------------- ITemplateProvider --- #
    # ----------------------------------------------------------------------- #
    # ITemplateProvider methods: Used to add the plugin's templates and htdocs

    def get_templates_dirs(self):
        from pkg_resources import resource_filename
        return [resource_filename(__name__, 'templates')]

    def get_htdocs_dirs(self):
        # '<key>' creates a pseudo-folder for genshi to find your resources
        # e.g.  add_stylesheet(req, '<key>/mystyle.css')
        from pkg_resources import resource_filename
        return [('<key>', resource_filename(__name__, 'htdocs'))]


I hope that helps.

~ Mark C
 

-- 
You received this message because you are subscribed to the Google Groups "Trac 
Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to trac-users+unsubscr...@googlegroups.com.
To post to this group, send email to trac-users@googlegroups.com.
Visit this group at https://groups.google.com/group/trac-users.
For more options, visit https://groups.google.com/d/optout.

Reply via email to