You are here: Home Blog Topics browser view

browser view

Sep 14, 2012

Plone Hello World Tutorial

by Mike Cullerton — last modified Sep 14, 2012 01:25 AM

A simple introduction to Plone development.

About a month ago, there was a bit of a ruckus on twitter about the state of Plone and documentation and what not.

 

There was a specific request for a Hello World type tutorial introducing Plone development. After talking with Mikko and others on IRC, I put some ideas together and released a first attempt a few days later.

Over the last few weeks, I've put time into adding more sections, reorganizing the layout, and general cleanup. Tonight, I pushed those changes to the developer manual at http://collective-docs.readthedocs.org/en/latest/getstarted/helloworld/index.html

 

At this point it's starting to take shape. There are holes in it, and things I'd like to clean up, but it's a good start.

I'd like to get some feedback. What do folks think? What is missing? Are there any errors?  Really, anything helpful is appreciated.

You can comment here, or reach me @cullerton on twitter.

Thanks,

Mike

Jun 22, 2010

Injecting Plone variables into javascript

by Mike Cullerton — last modified Jun 22, 2010 09:00 AM

I needed access to Plone variables (specifically portal_url) from within javascript. So, I created a browser view and python script that outputs the necessary variables, and registered the view in jsregistry.xml. This turned out to be a rather simple solution.

 

Step 1: Create a class to output the javascript

I created a file in src/my.product/my/product/browser called CommonUtils.py and added a class GlobalJS with a __call__ method that returns a string containing the javascript. __call__ sets the content-type, creates the string containing the variable, and returns the string.

class GlobalJS(BrowserView):
 
    def __call__(self,REQUEST,RESPONSE):
        RESPONSE.setHeader('Content-Type', 'application/javascript')
        js_string = "var portal_url = '%s';" % (self.context.portal_url())
        return js_string

 

Step 2: Create a browser view pointing to the class

In src/my.product/my/product/browser/configure.zcml, I added a browser view pointing to GlobalJS, gave it a name, and added a permission. Note that the name of the file containing GlobalJS is CommonUtils.py

<browser:view 
     for="*"
     name="global_js.js" 
     class=".CommonUtils.GlobalJS"
     permission="zope2.View"
     />

 

Step 3: Register the view in javascript registry

Here, I register the browser view in src/my.product/my/product/profiles/default/jsregistry.xml. I needed access to portal_url before jquery was loaded, so I added the insert-before property. Note that the id is the same as the name registered in the browser view.

<javascript 
    id="global_js.js"
    cacheable="True" compression="safe" cookable="True"
    enabled="True" expression=""  inline="True" insert-before="jquery.js"/>

 

Now, global_js.js is loaded on every page and I have access to portal_url from within javascript.

Hope this helps someone.

 
 
Copyright © 2003-2017 Core Software Group | 303/809-1001 | Fort Collins, Colorado | All rights reserved.